0 概述
在实际工作中,很多项目都是采用spring容器来管理对象。本文主要讲述mybatis和spring整合过程。
1 整合过程
首先要知道 Mybatis原始dao开发流程,具体见Mybatis原始的dao开发。
所谓的整合也就是:
- spring 容器通过单例模式来管理SqlSessionFactory,使用SqlSessionFactory创建SqlSession。
- 持久层mapper代理对象生成与管理
<!-- mybatis 整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
spring 和Mybatis 整合后配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--使用mybatis 自带的连接池-->
<bean id="pooledDataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--配置 sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:sqlmap/*Mapper.xml" />
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!--单个Mapper 代理对象生成-->
<!--<bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!--<property name="mapperInterface" value="com.hsc.dao.UserMapper"></property>-->
<!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>-->
<!--</bean>-->
<!--mapper 批量扫描 生成mapper 的Mapper代理对象-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hsc.dao"></property>
</bean>
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=mysql
jdbc.password=test
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="com.hsc.dao.UserMapper">
<sql id="Base_Column_List">
`id`,`userId`,`name`
</sql>
<select id="findUserInfoById" parameterType="java.lang.Long" resultType="com.hsc.entity.User">
SELECT <include refid="Base_Column_List" /> FROM user WHERE id=#{userId}
</select>
</mapper>
测试程序:
package com.hsc.study;
import com.hsc.dao.BookMapper;
import com.hsc.dao.UserMapper;
import com.hsc.entity.Book;
import com.hsc.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by hsc on 17/7/22.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
BookMapper bookMapper = applicationContext.getBean("bookMapper", BookMapper.class);
User user = userMapper.findUserInfoById(1);
Book book=new Book();
book.setName("mysql");
bookMapper.insert(book);
System.out.println("id"+book.getId());
System.out.printf(user.toString());
}
}
2 代码
代码:code