Packt Publishing “Java Persistence With MyBatis3” published

Hurray…My first book Java Persistence with MyBatis3 is published. I would like to thank Packt Publishers for giving me this opportunity to write on my favorite framework MyBatis.

For most of the software applications data persistence is a key and important aspect. In Java land we have many ways of implementing persistence layer starting from low level JDBC to fancy ORM frameworks.
JDBC is too low level API and needs to write a lot of boilerplate code. On the other hand we have full fledged ORM frameworks like JPA(Hibernate, EclipseLink etc) which hides the complexity of working with SQL directly by letting developers work with Objects and generate SQL based on the RDBMS(Dialect) being used. But each approach has its own set of pros and cons, there is no one size fits all solutions. There are many large applications that are using Hibernate successfully and there are many other applications which got screwed up by using Hibernate/JPA incorrectly. It is not the problem with JPA/Hibernate, it is simply because JPA/Hibernate may not be best fit for those applications or developers don’t understood them properly.

MyBatis Tutorial : Part4 – Spring Integration

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

MyBatis-Spring is a sub-project of MyBatis and provides Spring integration support which drastically simplifies the MyBatis usage. For those who are familiar with Spring’s way of Dependency Injection process, using MyBatis-Spring is a very simple.

First let us see the process of using MyBatis without Spring.

MyBatis Tutorial: Part 3 – Mapping Relationships

In this post let us see how to use MyBatis ResultMap configuration to map relationships.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

To illustrate we are considering the following sample domain model:

There will be Users and each User may have a Blog and each Blog can contain zero or more posts.

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

In this post I will explain how to perform CRUD operations using MyBatis Annotation support without need of Queries configuration in XML mapper files.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

Step#1: Create a table BLOG and a java domain Object Blog.

CREATE TABLE  blog 
(
      blog_id int(10) unsigned NOT NULL auto_increment,
      blog_name varchar(45) NOT NULL,
      created_on datetime NOT NULL,
      PRIMARY KEY  (blog_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
package com.sivalabs.mybatisdemo.domain;

import java.util.Date;

public class Blog 
{
     private Integer blogId;
     private String blogName;
     private Date createdOn;
     
     //Seeters and getters
     
     @Override
     public String toString() {
      return "Blog [blogId=" + blogId + ", blogName=" + blogName
        + ", createdOn=" + createdOn + "]";
     }
}

Step#2: Create UserMapper.java interface with SQL queries in Annotations.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis is an SQL Mapper tool which greatly simplifies the database programing when compared to using JDBC directly.

MyBatis Tutorial: Part1 – CRUD Operations

MyBatis Tutorial: Part-2: CRUD operations Using Annotations

MyBatis Tutorial: Part 3 – Mapping Relationships

MyBatis Tutorial : Part4 – Spring Integration

Step1: Create a Maven project and configure MyBatis dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.sivalabs</groupId>
 <artifactId>mybatis-demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>mybatis-demo</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
     <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.10</version>
   <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.1.1</version>
  </dependency>
  <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.21</version>
     <scope>runtime</scope>
  </dependency>
 </dependencies>
</project>

Step#2: Create the table USER and a Java domain Object User as follows: