Mybatis framework 7: integrating Spring in three ways

Required jar package:

 

Create a new lib folder and put it into the jar package. After build path -- > add to build path:

 

Original Dao development example:

Under src: create a new core configuration file sqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- Setting aliases -->
    <typeAliases>
        <package name="org.dreamtech.mybatis.pojo" />
    </typeAliases>
    <!-- Set scan package -->
    <mappers>
        <package name="org.dreamtech.mybatis.mapper"/>
    </mappers>

</configuration>

 

Create a new Spring core configuration file: applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <context:property-placeholder location="classpath:db.properties" />

    <!-- Database connection pool -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- Mybatis Factory -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- Location of core profile -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    </bean>

    <!-- Dao Original Dao -->
    <bean id="userDao" class="org.dreamtech.mybatis.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
    </bean>

</beans>

 

New database configuration file: db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=12345

 

log4j profile (not required):

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

Interface:

package org.dreamtech.mybatis.dao;

public interface UserDao {

    public void insertUser();
    
}

 

Implementation class:

package org.dreamtech.mybatis.dao;

import org.mybatis.spring.support.SqlSessionDaoSupport;

/**
 * Original Dao development
 * 
 * @author YiQing
 *
 */
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    //Add user (code omitted)
    public void insertUser() {
        //this.getSqlSession().insert();
    }

}

 

 

Mapper dynamic agent development:

New package, class, mapping file:

 

applicationContext.xml is configured as follows:

    <!-- Mapper Dynamic agent development -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
        <property name="mapperInterface" value="org.dreamtech.mybatis.mapper.UserMapper" />
    </bean>

 

UserMapper:

package org.dreamtech.mybatis.mapper;

import org.dreamtech.mybatis.pojo.User;

public interface UserMapper {

    // adopt ID Query a user
    public User findUserById(Integer id);

}

 

UserMapper.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="org.dreamtech.mybatis.mapper.UserMapper">
    <!-- adopt ID Query a user -->
    <select id="findUserById" parameterType="Integer" resultType="User">
        select * from user where id = #{v}
    </select>
</mapper>

 

Test class:

package org.dreamtech.mybatis.junit;

import org.dreamtech.mybatis.mapper.UserMapper;
import org.dreamtech.mybatis.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JunitTest {

    @Test
    public void testMapper() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = (UserMapper) ac.getBean("userMapper");
        User user = mapper.findUserById(10);
        System.out.println(user.getUsername());
    }
}

 

POJO: User class

package org.dreamtech.mybatis.pojo;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

 

 

Mapper dynamic agent scanning development:

This is an enhanced version of Mapper dynamic agent development:

There are disadvantages in Mapper dynamic agent development: too many mappers, too many configuration files

So I thought, can I scan a package automatically?

 

applicationContext.xml is configured as follows:

    <! -- mapper dynamic agent development scan -- >
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <! -- basic package -- >
        <property name="basePackage" value="org.dreamtech.mybatis.mapper" />
    </bean>

 

Note: no need to re inject the plant

 

The test class can be changed slightly:

Because there is no ID, directly change to UserMapper class

package org.dreamtech.mybatis.junit;

import org.dreamtech.mybatis.mapper.UserMapper;
import org.dreamtech.mybatis.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JunitTest {

    @Test
    public void testMapper() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = ac.getBean(UserMapper.class);
        User user = mapper.findUserById(10);
        System.out.println(user.getUsername());
    }
}

 

Conclusion: usually we choose the third, but we can't avoid the first two

Keywords: Java Mybatis xml Spring JDBC

Added by rowman on Fri, 10 Jan 2020 19:49:24 +0200