DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > MyBatis’ Wicked Statement Builders

MyBatis’ Wicked Statement Builders

Lukas Eder user avatar by
Lukas Eder
·
Jun. 07, 12 · Java Zone · Interview
Like (0)
Save
Tweet
3.83K Views

Join the DZone community and get the full member experience.

Join For Free

now here’s one of the most wicked api’s i’ve seen in a while!

mybatis is well-known as a database abstraction framework on top of jdbc, allowing for externalising sql into files, loading them at appropriate places in your java code. for those of you who like this approach, you may be used to statements similar to this one here, taken from the mybatis documentation :

<select id="selectperson" parametertype="int" resulttype="hashmap">
  select * from person where id = #{id}
</select>

this is mybatis alright, you think? you haven’t seen everything. mybatis also has statement builders . not just any type of statement builders. they make your java code look like sql. check this out:

private string selectpersonsql() {
  begin(); // clears threadlocal variable
  select("p.id, p.username, p.password, p.full_name");
  select("p.last_name, p.created_on, p.updated_on");
  from("person p");
  from("account a");
  inner_join("department d on d.id = p.department_id");
  inner_join("company c on d.company_id = c.id");
  where("p.id = a.id");
  where("p.first_name like ?");
  or();
  where("p.last_name like ?");
  group_by("p.id");
  having("p.last_name like ?");
  or();
  having("p.first_name like ?");
  order_by("p.id");
  order_by("p.full_name");
  return sql();
}

… jeez, that is one of the funniest ideas i’ve ever seen. a threadlocal to keep the sql statement currently being rendered!! and an api just … err … adding strings to an internal stringbuilder? wow.

you can decide for yourself whether you like this or not. but you can’t say these guys aren’t creative ;-) be sure to read the full docs to learn also about how to write insert, update and delete:

http://www.mybatis.org/core/statement-builders.html

Database MyBatis sql Java (programming language) Strings Abstraction (computer science) Documentation Framework

Published at DZone with permission of Lukas Eder. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Which Backend Frameworks Are Impacting Web App Development Immensely?
  • Design Patterns for Microservices
  • Learn the Weekly Rituals You Should Master as a Software Project Manager
  • Low Code and No Code: The Security Challenge

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo