Mybatis Spring: integrating mybatis and Spring mode 1: SqlSessionTemplate

Mybatis Spring will help you integrate mybatis code seamlessly into Spring. It will allow mybatis to participate in Spring's transaction management, create mapper and SqlSession and inject them into bean s, and convert mybatis's exception to Spring's DataAccessException.

  1. Preparation, entity class, mapper interface, mybatis configuration file
public class User {
    private int id;
    private String username;
    private String password;
}
public interface UserMapper {
    public List<User> selectUser();
}
<mapper namespace="com.spring.mapper.UserMapper">
    <select id="selectUser" resultType="user">select * from databasename.user;</select>
</mapper>
<!--mybatis-config.xml configuration file-->
<configuration>
    <typeAliases>
        <package name="com.spring.pojo"/>
    </typeAliases>
</configuration>
  1. Write data source configuration
    <!--DataSource: Use Spring Data source replacement for Mybatis Configuration c3p0 dbcp druid
    //Using JDBC provided by Spring
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/databasename?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--binding Mybatis configuration file-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/spring/mapper/*.xml"/>
    </bean>
    <!--SqlSessionTemplate:It's what we use sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--Only constructor injection can be used sqlSessionFactory,Because he didn't set Method-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
  1. sqlSessionFactory
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--binding Mybatis configuration file-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/spring/mapper/*.xml"/>
    </bean>
  1. sqlSessionTemplate
<! -- sqlsessiontemplate: the sqlsession we use -- >
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <! -- only constructors can be used to inject sqlSessionFactory, because it has no set method -- >
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
  1. Need to add implementation class to interface
public class UserMapperImpl implements UserMapper { 

    private SqlSessionTemplate sqlSession;	
    public void setSqlSession(SqlSessionTemplate sqlSession) {
		this.sqlSession = sqlSession;
	}
	public List<User> selectUser() {
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		return mapper.selectUser();
	}
}
  1. Inject your own implementation class into Spring
<bean id="userMapper" class="com.UserMapperImpl"> 
	<property name="sqlSession" ref="sqlSession"/> 
</bean>
  1. test
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}
  1. Project dependence
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>

Tags: Programming Mybatis Spring xml JDBC

Posted on Sun, 29 Mar 2020 07:04:56 -0700 by mhsjrotc