Hibernate

Hibernate is the mapping file that maps POJO to database tables, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.learn.chapter1.pojo.Role" table="t_role">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="roleName" type="string">
<column name="role_name" length="60" not-null="true" />
</property>
<property name="note" type="string">
<column name="note" length="512" />
</property>
</class>
</hibernate-mapping>

As seen above, there is a mapping on POJO and table t_role. CRUD operations are performed over POJO to modify t_role table data, e.g.

Hibernate operating on database via Session: (HibernateSessionDao.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class HibernateSessionDao {
Session session = null;
Transaction tx = null;
try {
// open Session
session = HibernateUtil.getSessionFactory().openSession();
// tx
tx = session.beginTransaction();
// POJO
Role role = new Role();
role.setId(1);
role.setRoleName("rolename1");
role.setNote("note1");
session.save(role);
Role role2 = (Role) session.get(Role.class, 1); // read
role2.setNote("modify note");
System.err.println(role2.getRoleName());
session.delete(role2);
tx.commit();
} catch (Exception ex) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
ex.printStackTrace();
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
}

No SQL involved, because Hibernate would generate SQL based on mapping relations. It’s beneficial in the era of CRM because implementation of business logics would be the priority and then come performances, which is why Hibernate became the major framework in that era.

MyBatis

In mobile era, myBatis becomes the major Java persistence framework. In contrast to Hibernate which eliminate SQL, mybatis doesn’t wipe SQL out; rather, it leaves for programmers to customize SQL without Hibernate’s automated SQL generation. In this way, more precisely defined SQL are used to ameliorate performances. It is more consistent with the requirements of mobile era: “high concurrency”, “big data”, “high-performance”, “high-responsiveness”.

mybatis also needs a mapping file from POJO to database, like Hibernate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.learn.chapter1.mapper.RoleMapper">
<resultMap id="roleMap" type="com.learn.chapter1.pojo.Role">
<id property="id" column="id" />
<result property="roleName" column="role_name" />
<result property="note" column="note" />
</resultMap>

<select id="getRole" resultMap="roleMap">
select id, role_name, note from t_role where id = #{id}
</select>

<delete id="deleteRole" parameterType="int">
delete from t_role where id = #{id}
</delete>

<insert id="insertRole" parameterType="com.learn.chapter1.pojo.Role">
insert into t_role (role_name, note) values (#{roleName}, #{note})
</insert>

<update id="updateRole" parameterType="com.learn.chapter1.pojo.Role">
update t_role set
role_name = #{roleName},
note = #{note}
where id = #{id}
</update>
</mapper>

resultMap element is used to define mapping rules; mybatis would automatically finish mapping under these rules, CRUD map to 4 elements select, delete, insert, update.

Achtung⚠️:

  1. namespace property of mapper element === full qualified name of the interface;
  2. Inside mapper, id of SQL === interface methods;

The 2nd part of mapping file:

1
2
3
4
5
6
7
8
9
package com.learn.chapter1.mapper;
import com.learn.chapter1.pojo.Role;

public interface RoleMapper {
public Role getRole(Integer id);
public int deleteRole(Integer id);
public int insertRole(Role role);
public int updateRole(Role role);
}

Achtung⚠️:

In fact, mapper files consists of two parts, XML mapper and an interface. Both parts are necessary. Divided, each is bootless.

There is no need to write an implementation of this interface.

MybatisSessionDao.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.learn.chapter1.mapper;

import org.apache.ibatis.session.SqlSession;

public class MybatisSessionDao {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtil.getSqlSession();
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1); // read
System.err.println(role.getRoleName());
role.setRoleName("update_role_name");
roleMapper.updateRole(role); // update
Role role2 = new Role();
role2.setNote("note2");
role2.setRoleName("role2");
roleMapper.insertRole(role); // insert
roleMapper.deleteRole(5); // delete
sqlSession.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (sqlSession != null) {
sqlSession.rollback();
}
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}