核心:将mybatis集成到spring中
1、先看mybatis-config.xml中的代码
不难看出mybatis-config.xml文件中只有数据库连接信息和mapper映射信息
<?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>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="url" value="${url}"/>
<property name="driver" value="${driver}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/RoleMapper.xml"></mapper>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
所以对于mybatis-config.xml文件集成到spring中在spring中只需要做两件事:
(1)、数据库连接信习
(2)、mapper映射信息
2、再看.java文件 要做三件事(创建对象的事情都交给ioc容器来做吧 ——-spring)
(3)先要加载mybatis-config.xml才能创建sqlSessionFactory
Reader resourceAsReader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
//应为是创建sqlSessionFactory使用mybatis-config.xml的字符流
//所以集成mybatis-config.xml要做的两件事也是在创建sqlSessionFactory环节
(4)通过sqlSessionFactory.openSession()创建sqlSession
SqlSession session = sqlSessionFactory.openSession();
(5)通过sqlSession.getMapper(UserMapper.class)创建UserMapper对象调用UserMapper的方法
UserMapper userMapper = session.getMapper(UserMapper.class)
dao层:UserMapper接口 UserMapper.xml
service层:UserService接口 和UserServiceImpl
整合后的mybatis-config.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>
没有东西了!!!!!!!!!!
</configuration>
整合后的applicationConfig.xml(无实现类无扫描mapper接口自定义创建UserMapper实现)
<?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:contex="<http://www.springframework.org/schema/context>"
xsi:schemaLocation="<http://www.springframework.org/schema/beans> <http://www.springframework.org/schema/beans/spring-beans.xsd> <http://www.springframework.org/schema/context> <https://www.springframework.org/schema/context/spring-context.xsd>">
<contex:component-scan base-package="cn.smbms"/>
//外部引入db.properties 配置成数据源
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
</bean>
----------------------此时mybatis-config.xml中的数据库连接信息已经被取代----------------------
<!--通过数据源和mapper用factoryBean创建factory sqlSessionFactoryBuild.buid(mybatis-config.xml)-->
<!-- mybatis-config.xml配置数据源和扫描mapper路径 此时已经在spring中配置过了-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"/>-->
<property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/**/*.xml</value>
</list>
</property>
</bean>
------------------------------此时mybatis-config.xmlmapper信息已经被集成----------------
-------------------------------此时已经创建了sqlsessionfactory---------------------------
<!--用sqlsessionTemplate这个类的构造器 且sqlSessionFactory作为参数创建sqlsession-->
<!--sqlSessionFactory.openSession()-->
<!-- public SqlSessionTemplate(sqlSessionFactory){
SqlSession = sqlSessionFactory.openSession()
}-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
--------------------------此时已经创建了sqlsession---------------------------------
<!--sqlsession.getMapper(UserMapper.class)-->
<!--
MapperFactoryBean{
method(sqlsession,userMAapper){
mapper = sqlsession.getMapper(UserMapper.class)
return mapper;
}
}
-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.smbms.dao.user.UserMapper"></property>
<property name="sqlSessionTemplate" ref="sqlSession"/>-->
</bean>
----------------------------此时已经创建了userMapper------------------------
<bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>
</beans>
----------------------------------将创建的userMapper注入------------------------
整合后的applicationConfig.xml(无实现类有扫描mapper接口自定义创建UserMapper实现)
<?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:contex="<http://www.springframework.org/schema/context>"
xsi:schemaLocation="<http://www.springframework.org/schema/beans> <http://www.springframework.org/schema/beans/spring-beans.xsd> <http://www.springframework.org/schema/context> <https://www.springframework.org/schema/context/spring-context.xsd>">
<contex:component-scan base-package="cn.smbms"/>
//外部引入db.properties 配置成数据源
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
</bean>
----------------------此时mybatis-config.xml中的数据库连接信息已经被取代----------------------
<!--通过数据源和mapper用factoryBean创建factory sqlSessionFactoryBuild.buid(mybatis-config.xml)-->
<!-- mybatis-config.xml配置数据源和扫描mapper路径 此时已经在spring中配置过了-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"/>-->
<property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/**/*.xml</value>
</list>
</property>
</bean>
------------------------------此时mybatis-config.xmlmapper信息已经被集成----------------
-------------------------------此时已经创建了sqlsessionfactory---------------------------
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.smbms.dao"></property>
</bean>
-----------------------MapperScannerConfigure扫描包下接口创建对象------------------------
</beans>
**service层:**
package cn.smbms.service.user;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public int deleteByPrimaryKey(Long id) {
return userMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(User row) {
return userMapper.insert(row);
}
@Override
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public List<User> selectAll() {
return userMapper.selectAll();
}
@Override
public int updateByPrimaryKey(User row) {
return userMapper.updateByPrimaryKey(row);
}
}