Why MyBatis

Exclusive offer: get 50% off this eBook here
Java Persistence with MyBatis 3

Java Persistence with MyBatis 3 — Save 50%

A practical guide to MyBatis, a simple yet powerful Java Persistence Framework! with this book and ebook

$14.99    $7.50
by K. Siva Prasad Reddy | July 2013 | Open Source

In this article written by K. Siva Prasad Reddy, who has also authored the article Java Persistence with MyBatis 3, we will understand why MyBatis is useful.

There are many Java-based persistence frameworks, however MyBatis became popular because of the following reasons:

  • It Eliminates a lot of JDBC boilerplate code
  • It has a low learning curve
  • It works well with legacy databases
  • It embraces SQL
  • It provides support for integration with Spring and Guice frameworks
  • It provides support for integration with third-party cache libraries
  • It induces better performance

 

(For more resources related to this topic, see here.)

Eliminates a lot of JDBC boilerplate code

Java has a Java DataBase Connectivity (JDBC) API to work with relational databases. But JDBC is a very low-level API, and we need to write a lot of code to perform database operations.

Let us examine how we can implement simple insert and select operations on a STUDENTS table using plain JDBC.

Assume that the STUDENTS table has STUD_ID, NAME, EMAIL, and DOB columns.

The corresponding Student JavaBean is as follows:

package com.mybatis3.domain; import java.util.Date; public class Student { private Integer studId; private String name; private String email; private Date dob; // setters and getters }

The following StudentService.java program implements the SELECT and INSERT operations on the STUDENTS table using JDBC.

public Student findStudentById(int studId) { Student student = null; Connection conn = null; try{ //obtain connection conn = getDatabaseConnection(); String sql = "SELECT * FROM STUDENTS WHERE STUD_ID=?"; //create PreparedStatement PreparedStatement pstmt = conn.prepareStatement(sql); //set input parameters pstmt.setInt(1, studId); ResultSet rs = pstmt.executeQuery(); //fetch results from database and populate into Java objects if(rs.next()) { student = new Student(); student.setStudId(rs.getInt("stud_id")); student.setName(rs.getString("name")); student.setEmail(rs.getString("email")); student.setDob(rs.getDate("dob")); } } catch (SQLException e){ throw new RuntimeException(e); }finally{ //close connection if(conn!= null){ try { conn.close(); } catch (SQLException e){ } } } return student; } public void createStudent(Student student) { Connection conn = null; try{ //obtain connection conn = getDatabaseConnection(); String sql = "INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(?,?,?,?)"; //create a PreparedStatement PreparedStatement pstmt = conn.prepareStatement(sql); //set input parameters pstmt.setInt(1, student.getStudId()); pstmt.setString(2, student.getName()); pstmt.setString(3, student.getEmail()); pstmt.setDate(4, new java.sql.Date(student.getDob().getTime())); pstmt.executeUpdate(); } catch (SQLException e){ throw new RuntimeException(e); }finally{ //close connection if(conn!= null){ try { conn.close(); } catch (SQLException e){ } } } } protected Connection getDatabaseConnection() throws SQLException { try{ Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection ("jdbc:mysql://localhost:3306/test", "root", "admin"); } catch (SQLException e){ throw e; } catch (Exception e){ throw new RuntimeException(e); } }

There is a lot of duplicate code in each of the preceding methods, for creating a connection, creating a statement, setting input parameters, and closing the resources, such as the connection, statement, and result set.

MyBatis abstracts all these common tasks so that the developer can focus on the really important aspects, such as preparing the SQL statement that needs to be executed and passing the input data as Java objects.

In addition to this, MyBatis automates the process of setting the query parameters from the input Java object properties and populates the Java objects with the SQL query results as well.

Now let us see how we can implement the preceding methods using MyBatis:

  1. Configure the queries in a SQL Mapper config file, say StudentMapper.xml.

    <select id="findStudentById" parameterType="int" resultType=" Student"> SELECT STUD_ID AS studId, NAME, EMAIL, DOB FROM STUDENTS WHERE STUD_ID=#{Id} </select> <insert id="insertStudent" parameterType="Student"> INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB) VALUES(#{studId},#{name},#{email},#{dob}) </insert>

  2. Create a StudentMapper interface.

    public interface StudentMapper { Student findStudentById(Integer id); void insertStudent(Student student); }

  3. In Java code, you can invoke these statements as follows:

    SqlSession session = getSqlSessionFactory().openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); // Select Student by Id Student student = mapper.selectStudentById(1); //To insert a Student record mapper.insertStudent(student);

That's it! You don't need to create the Connection, PrepareStatement, extract, and set parameters and close the connection by yourself for every database operation. Just configure the database connection properties and SQL statements, and MyBatis will take care of all the ground work.

Don't worry about what SqlSessionFactory, SqlSession, and Mapper XML files are.

Along with these, MyBatis provides many other features that simplify the implementation of persistence logic.

  • It supports the mapping of complex SQL result set data to nested object graph structures
  • It supports the mapping of one-to-one and one-to-many results to Java objects
  • It supports building dynamic SQL queries based on the input data

Low learning curve

One of the primary reasons for MyBatis' popularity is that it is very simple to learn and use because it depends on your knowledge of Java and SQL. If developers are familiar with Java and SQL, they will fnd it fairly easy to get started with MyBatis.

Works well with legacy databases

Sometimes we may need to work with legacy databases that are not in a normalized form. It is possible, but diffcult, to work with these kinds of legacy databases with fully-fedged ORM frameworks such as Hibernate because they attempt to statically map Java objects to database tables.

MyBatis works by mapping query results to Java objects; this makes it easy for MyBatis to work with legacy databases. You can create Java domain objects following the object-oriented model, execute queries

Embraces SQL

Full-fedged ORM frameworks such as Hibernate encourage working with entity objects and generate SQL queries under the hood. Because of this SQL generation, we may not be able to take advantage of database-specifc features. Hibernate allows to execute native SQLs, but that might defeat the promise of a database-independent persistence.

The MyBatis framework embraces SQL instead of hiding it from developers. As MyBatis won't generate any SQLs and developers are responsible for preparing the queries, you can take advantage of database-specifc features and prepare optimized SQL queries. Also, working with stored procedures is supported by MyBatis.

Supports integration with Spring and Guice frameworks

MyBatis provides out-of-the-box integration support for the popular dependency injection frameworks Spring and Guice; this further simplifes working with MyBatis.

Supports integration with third-party cache libraries

MyBatis has inbuilt support for caching SELECT query results within the scope of SqlSession level ResultSets. In addition to this, MyBatis also provides integration support for various third-party cache libraries, such as EHCache, OSCache, and Hazelcast.

Better performance

Performance is one of the key factors for the success of any software application. There are lots of things to consider for better performance, but for many applications, the persistence layer is a key for overall system performance.

  • MyBatis supports database connection pooling that eliminates the cost of creating a database connection on demand for every request.
  • MyBatis has an in-built cache mechanism which caches the results of SQL queries at the SqlSession level. That is, if you invoke the same mapped select query, then MyBatis returns the cached result instead of querying the database again.
  • MyBatis doesn't use proxying heavily and hence yields better performance compared to other ORM frameworks that use proxies extensively.

There are no one-size-fits-all solutions in software development. Each application has a different set of requirements, and we should choose our tools and frameworks based on application needs. In the previous section, we have seen various advantages of using MyBatis. But there will be cases where MyBatis may not be the ideal or best solution.If your application is driven by an object model and wants to generate SQL dynamically, MyBatis may not be a good ft for you. Also, if you want to have a transitive persistence mechanism (saving the parent object should persist associated child objects as well) for your application, Hibernate will be better suited for it.

Installing and configuring MyBatis

We are assuming that the JDK 1.6+ and MySQL 5 database servers have been installed on your system. The installation process of JDK and MySQL is outside the scope of this article.

At the time of writing this article, the latest version of MyBatis is MyBatis 3.2.2. Even though it is not mandatory to use IDEs, such as Eclipse, NetBeans IDE, or IntelliJ IDEA for coding, they greatly simplify development with features such as handy autocompletion, refactoring, and debugging. You can use any of your favorite IDEs for this purpose.

This section explains how to develop a simple Java project using MyBatis:

  • By creating a STUDENTS table and inserting sample data
  • By creating a Java project and adding mybatis-3.2.2.jar to the classpath
  • By creating the mybatis-config.xml and StudentMapper.xml configuration files
  • By creating the MyBatisSqlSessionFactory singleton class
  • By creating the StudentMapper interface and the StudentService classes
  • By creating a JUnit test for testing StudentService

Summary

In this article, we discussed about MyBatis and the advantages of using MyBatis instead of plain JDBC for database access.

Resources for Article :


Further resources on this subject:


Java Persistence with MyBatis 3 A practical guide to MyBatis, a simple yet powerful Java Persistence Framework! with this book and ebook
Published: June 2013
eBook Price: $14.99
Book Price: $29.99
See more
Select your format and quantity:

About the Author :


K. Siva Prasad Reddy

K. Siva Prasad Reddy is a Senior Software Engineer living in Hyderabad, India, and having more than seven years of experience in developing enterprise applications with Java and JavaEE technologies. Siva is a Sun Certified Java Programmer and has a lot of experience in server-side technologies such as Java, JavaEE, Spring, Hibernate, MyBatis, JSF, PrimeFaces, and WebServices (SOAP/REST). Siva is also the author of Java Persistence with MyBatis 3, Packt Publishing.

Siva normally shares the knowledge he has acquired on his blog at www.sivalabs.in. If you want to find out more about his work, you can follow him on Twitter (@sivalabs) and GitHub (https://github.com/sivaprasadreddy).

Books From Packt


Android NDK Beginner’s Guide
Android NDK Beginner’s Guide

 JBoss ESB Beginner’s Guide
JBoss ESB Beginner’s Guide

 java-7-new-features-cookbook
Java-7-new-Features-Cookbook

Java EE 6 with GlassFish 3 Application Server
Java EE 6 with GlassFish 3 Application Server

JBoss AS 7 Development
JBoss AS 7 Development

 Java EE Development with Eclipse
Java EE Development with Eclipse

Oracle Advanced PL/SQL Developer Professional Guide
Oracle Advanced PL/SQL Developer Professional Guide

  Learning SQL Server 2008 Reporting Services
Learning SQL Server 2008 Reporting Services


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
V
D
r
d
p
g
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software