Monday, July 10, 2017

Mybatis Spring Integration

This post will give a quick introduction into integrating MyBatis and Spring with a very basic application. I will describe only the integration using XML based configuration. As always with MyBatis, this starts with setting up SqlSessionFactory from Spring. Instead of configuring the datasource and mapping in mybatis-config.xml, all configuration will be setup in Spring configuration. The only additional configuration will be the Mapper.xml file.

Application Setup

Following is the directory structure I had for this project.
+---MybatisSpring
|   |   build.gradle
|   \---src
|       +---main
|       |   +---java
|       |   |   +---beans
|       |   |   |       HousingData.java
|       |   |   |       
|       |   |   +---main
|       |   |   |       MybatisSpringTest.java
|       |   |   |       
|       |   |   +---mapper
|       |   |   |       HousingDataMapper.java
|       |   |   |       
|       |   |   \---service
|       |   |           TestService.java
|       |   |           
|       |   \---resources
|       |       |   applicationContext.xml
|       |       |   
|       |       \---mapper
|       |               HousingDataMapper.xml
|       |               
|       \---test
|           \---java

Configuring Mybatis with Spring XML Configuration

SqlSessionFactory can be configured in Spring using org.mybatis.spring.SqlSessionFactoryBean as shown below
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
</bean>
The SQL Mapper can be configured in Spring XML configuration as below
<bean id="housingDataMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
 <property name="mapperInterface" value="mapper.HousingDataMapper" />
 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

HousingDataMapper.xml
<?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="mapper.HousingDataMapper">
 <select id="selectAllData" resultType="beans.HousingData">
  select * from housing_data
 </select>
</mapper>
MybatisSpringTest.java
package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.HousingData;
import service.TestService;

public class MybatisSpringTest {

 public static void main(String[] args) {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  MybatisSpringTest test = ctx.getBean("mybatisSpringTest", MybatisSpringTest.class);
  test.printData();

 }

 private TestService testService = null;

 private void printData() {
  for(HousingData data: testService.getAllData()) {
   System.out.println(data.getBuilt());
  }

 }

 public TestService getTestService() {
  return testService;
 }

 public void setTestService(TestService testService) {
  this.testService = testService;
 }

}
TestService.java
package service;

import java.util.List;

import beans.HousingData;
import mapper.HousingDataMapper;

public class TestService {
 HousingDataMapper housingDataMapper;

 public String echo(String str) {
  return str.toUpperCase();
 }

 public List getAllData() {
  return housingDataMapper.selectAllData();
 }

 public HousingDataMapper getHousingDataMapper() {
  return housingDataMapper;
 }

 public void setHousingDataMapper(HousingDataMapper housingDataMapper) {
  this.housingDataMapper = housingDataMapper;
 }

}
HousingData.java
package beans;

public class HousingData {
 private int id;
 private int value;
 private int rooms;
 private String structureType;
 private int built;

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

 public int getRooms() {
  return rooms;
 }

 public void setRooms(int rooms) {
  this.rooms = rooms;
 }

 public String getStructureType() {
  return structureType;
 }

 public void setStructureType(String structureType) {
  this.structureType = structureType;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getBuilt() {
  return built;
 }

 public void setBuilt(int built) {
  this.built = built;
 }

}
HousingDataMapper.java
package mapper;

import java.util.List;

import beans.HousingData;

public interface HousingDataMapper {

 public List selectAllData();

}
build.gradle
apply plugin:'application'
mainClassName = "main.MybatisSpringTest"
applicationName = 'MybatisSpringTest'

repositories {
    jcenter()
}

dependencies {
  compile group: 'org.springframework', name: 'spring-core', version: '4.3.9.RELEASE'
 compile group: 'org.springframework', name: 'spring-context', version: '4.3.9.RELEASE'
 compile group: 'org.springframework', name: 'spring-beans', version: '4.3.9.RELEASE'
 compile group: 'org.springframework', name: 'spring-tx', version: '4.3.9.RELEASE'
 compile group: 'org.springframework', name: 'spring-orm', version: '4.3.9.RELEASE'
 compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
    compile group: 'org.mybatis', name: 'mybatis', version: '3.2.2'
 compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
 compile group: 'org.mybatis', name: 'mybatis', version: '3.4.4'
 compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.42'
 
    // Use JUnit test framework
    compile 'junit:junit:4.12'
}

2 comments:

  1. A easy and exciting blog about java learning. Please comment your opinions and share..
    http://foundjava.blogspot.in

    ReplyDelete
  2. Hi...

    Could you please share applicationcontext.xml as well.

    ReplyDelete

Popular Posts