Wednesday, July 11, 2012


MyBatis 3 - Spring integration tutorial


As a first step of this tutorial, Spring MVC 3 CRUD example with MyBatis 3, we will define a MyBatis service that will help us to perform CRUD operation on database.

We have a domain class for User and a database table to store the User information on database. We will use xml configuration model for our example to define SQL commands that will perform CRUD operation.





Our Domain class
01package com.raistudies.domain;
02
03import java.io.Serializable;
04
05public class User implements Serializable{
06
07    private static final long serialVersionUID = 3647233284813657927L;
08
09    private String id;
10    private String name = null;
11    private String standard = null;
12    private String age;
13    private String sex = null;
14
15    //setter and getter have been omitted to make the code short
16
17    @Override
18    public String toString() {
19        return "User [name=" + name + ", standard=" + standard + ", age=" + age
20        ", sex=" + sex + "]";
21    }
22}
We have five properties in our domain class called User for which have to provide database services.

Our Database Table

Following is our database table:
1CREATE TABLE `user` (
2    `id` varchar(36) NOT NULL,
3    `namevarchar(45) DEFAULT NULL,
4    `standard` varchar(45) DEFAULT NULL,
5    `age` varchar(45) DEFAULT NULL,
6    `sex` varchar(45) DEFAULT NULL,
7    PRIMARY KEY (`id`)
8) ENGINE=InnoDB DEFAULT CHARSET=utf8
Creating interface for CRUD operations

For defining the CRUD database operation using MyBatis 3, we have to specify the methods that will be used to perform CRUD operation. Following is the interface for our example:
01package com.raistudies.persistence;
02
03import java.util.List;
04
05import com.raistudies.domain.User;
06
07public interface UserService {
08
09    public void saveUser(User user);
10    public void updateUser(User user);
11    public void deleteUser(String id);
12    public List getAllUser();
13}
We have four methods here to perform operations create,update , delete and get from database.

XML Mapping file for UserService interface
01xml version="1.0" encoding="UTF-8"?>
02
04
05<mapper namespace="com.raistudies.persistence.UserService">
06
07    <resultMap id="result" type="user">
08        <result property="id" column="id"/>
09        <result property="name" column="name"/>
10        <result property="standard" column="standard"/>
11        <result property="age" column="age"/>
12        <result property="sex" column="sex"/>
13    resultMap>
14
15    <select id="getAllUser" parameterType="int" resultMap="result">
16        SELECT id,name,standard,age,sex
17        FROM user;
18    select>
19
20    <insert id="saveUser" parameterType="user">
21        INSERT INTO user (id,name,standard,age,sex)
22        VALUE (#{id},#{name},#{standard},#{age},#{sex})
23    insert>
24
25    <update id="updateUser" parameterType="user">
26        UPDATE user
27        SET
28        name = #{name},
29        standard = #{standard},
30        age = #{age},
31        sex = #{sex}
32        where id = #{id}
33    update>
34
35    <delete id="deleteUser" parameterType="int">
36        DELETE FROM user
37        WHERE id = #{id}
38    delete>
39mapper>
You will see a lot of things new here:

The mapping file will contain element  to define the SQL statement for the services. Here the property “namespace” defines the interface for which this mapping file has been defined.

 tag defines that the operation is of type insert. The value of “id” property specifies the function name for which the SQL statement is been defined. Here it is “saveUser“. The property “parameterType” defines the parameter of the method is of which type. We have used alias for the class User here. The alias will be configured in MyBatis Configuration file later. Then, we have to define the SQL Statement. #{id} defines that the property “id” of class User will be passed as a parameter to the SQL query.

 tag is used to specify the mapping between the User class and user table. id of is a unique name to the mapping definition. Under this tag, we define the different properties and which column is bounded to which property.

1 comment:

Arvind said...

Nice Article. Good explanation to integrate spring and MyBatis