MyBatis is one of the top notch ORM tools available. The MyBatis data mapper framework makes it easier to use a relational database with object-oriented applications. If you are familiar with SQL, the learning curve for myBatis is pretty much zero. If you want complete control of the schema, queries and wanted to write complex queries then myBatis is the best option.

Without DI concepts of spring, setting up and using myBatis in a Java project is very cumbersome and redundant. Spring IOC along with jars developed for the Spring-myBatis integration has made working with myBatis effortless.

Here I will walk you through the myBatis configuration using Spring annotations.

Required jars:

  • spring
  • mybatis-3.0.4
  • mybatis-spring-1.0.0

Spring Context.xml:

<tx:annotation-driven />

<context:component-scan base-package=“com.mybatis.persistence” />

<bean id=“myBatisDataSource” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>

<property name=“driverClassName” value=“com.mysql.jdbc.Driver” />

<property name=“url” value=“jdbc:mysql://localhost:3306/mybatis” />

<property name=“username” value=“test” />

<property name=“password” value=“test” />

</bean>

<bean id=” myBatisTransactionManager”

class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<qualifier value=“myBatisTransactionManager”/>

<property name=“dataSource” ref=“myBatisDataSource” />

</bean>

<bean id=“myBatisSqlSessionFactory” class=“org.mybatis.spring.SqlSessionFactoryBean”>

<property name=“dataSource” ref=“myBatisDataSource” />

</bean>

<bean id=“myBatisScanner” class=“org.mybatis.spring.mapper.MapperScannerConfigurer”>

<property name=“basePackage” value=“com.mybatis.persistence.mapper”/>

</bean>

SqlSessionFactoryBean – Implements Spring’s FactoryBean. Spring will build a SqlSessionFactory for you at application startup and store it with the name myBatisSqlSessionFactory.

MapperScannerConfigurer – Scans the basePackage location for the mapping interfaces and xml mapping files.

Service Class:

@Transactional(“myBatisTransactionManager”)
public class MyBatisManager {
@Autowired
private EmployeeMapper EmployeeMapper;
public void add(id,name,address,isDeleted)
throws EmployeeException {
// TODO Auto-generated method stub
Map<String, Object> param = new HashMap<String, Object>();
param.put("ID", id);
param.put("NAME", name);
param.put("ADDRESS", address);
param.put("IS_DELETED",isDeleted);
EmployeeMapper.add(param);
}
}

By using the @Transactional annotation, Spring creates a DB transaction and closes it on successful completion or on error. A best practice is to use this facility in the service layer. Calling the interface methods triggers a call to the SQL queries that have an id which is the same as the method name.

Things to take care of:

  • Interfaces and the xml mapper files should be places under same package structure.

-In the example, I placed both the files in com.mybatis.persistence.mapper package

  • Both the interface and the xml mapper file should have the same name.

-In the example its EmployeeMapper.java and EmployeeMapper.xml

  • Id for the sql query in the mapper file should be exactly the same as method name in the interface.

 

Example mapper interface:

public interface EmployeeMapper {
public void add(Map<String, Object> params);
public void update(Map<String, Object> params);
public employee getById(long id);
public void delete(Map<String, Object> params);
}

Xml mapping example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="com.mybatis.persistence.mapper.EmployeeMapper">
<insert id="add" parameterType="map">
INSERT INTO
EMP (
ID,
NAME,
ADRESS,
IS_DELETED
)
VALUES (
#{ID,jdbcType=VARCHAR},
#{NAME, jdbcType=VARCHAR},
#{ADDRESS,
jdbcType=VARCHAR},
#{IS_DELETED, jdbcType=TINYINT}
)
</insert>
<select id="getById" resultMap="employee">
SELECT * FROM EMP WHERE ID=#{ID,jdbcType=VARCHAR}
</select>
<resultMap type=" com.mybatis.persistence.model.employee" id="employee">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="address" column=" address"/>
<result property="is_deleted" column="is_deleted"/>
</resultMap>
</mapper>

* Several other examples for xml mapper files can be found online.

* If you are using Oracle sql server, do not forget to include ojdbc jar.

Share This