整合之前回忆一下spring和mybatis分别做了什么:
spring 通过注解/xml配置,实现AOP和DI
DI: 接口实现类中, 将接口私有化,从容器中读取,而不是new一个
UserDao userDao=new UserDaoImpl()
//改写成
private UserDao userDao
IoC:获取类时,从容器中获取
UserDao userDao = new UserDaoImpl();
//改成
UserDao userDao = applicationConfig.getBean("userDao");
AOP:将一些常用的操作,编写成切面,插入到程序中,通过监听实现
Before
After
...
mybatis 通过config.xml mapper.xml ,实体类,serviceImpl,来映射数据库数据,并且通过对实体类的进行serviceImpl的操作,来调用mapper内容,对数据库数据进行操作
class People{
private String name;
....getting/setting/toString;
}
class PeopleService{
private People people;
void addPeople(){
}
}
class PeopleServiceImpl{
private People people;
void addPeople(){
}
....
}
Peoplemapper.xml
insert xxxxxx
config.xml
Peoplemapper.xml
jdbc
....
两者整合起来可以做什么,我还不是很清楚。。。
——————————————————————————————————————
整合
一/,导入mybatis-spring的包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
二/ 编辑applicationContent.xml,加入sqlSessionFactory
<bean id="sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref = "dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
mybatis-config.xml中environment部分可以注释掉了
<environments default="mysql">
<!-- 配置id为mysql的数据库环境 -->
<environment id = "mysql">
<transactionManager type="JDBC"/>
<dataSource type = "POOLED">
<property name = "driver" value = "${jdbc.driver}"/>
<property name = "url" value = "${jdbc.url}"/>
<property name = "username" value = "${jdbc.username}"/>
<property name = "password" value = "${jdbc.password}"/>
</dataSource>
</environment>
</environments>
三/ 与mybatis方法用的差不多。
原理:创建DAO和DAOIMPL,在daoImpl中注入sqlSessionFactory,创建sqlSession来操作。
提供:mybatis-spring包提供了SqlSessionTemplate和SqlSessionDaoSupport来实现。
具体内容:
1. SqlSessionTemplate:负责当前SqlSession和当前Spring事务时相关的,并管理SqlSession(关闭,提交,回滚)
2. SqlSessionDaoSupport作为DAO的基类来使用,提供方法 getSqlSession()
四/代码实现
Customer,CustomerDao,customerMapper.xml,mybatis-config.xml没变化。
CustomerDaoImpl修改如下
public class CustomerImpl
extends SqlSessionDaoSupport
implements CustomerDao{
public Customer findCustomerById(Integer id){
return this.getSqlSession().selectOne("com.itheima.mapper.CustomerMapper.findCustomerById",id);
}
}
以上代码需要SqlSessionFactory来创建sqlSession,这里通过注入来实现,修改applicationContext.xml
<bean id = "customerDao" class = "com.itheima.dao.impl.CustomerImpl">
<property name="sqlSessionFactory" ref ="sqlSessionFactory"/>
</bean>
以上代码将sqlSessionFactory注入到CustomerImpl中,这里的注入sqlSessionFactory是父类SqlSessionDaoSupport的一个私有属性。
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
if (this.sqlSessionTemplate == null || sqlSessionFactory != this.sqlSessionTemplate.getSqlSessionFactory()) {
this.sqlSessionTemplate = createSqlSessionTemplate(sqlSessionFactory);
}
}
(property元素用于调用bean示例中的setter方法,完成属性赋值)
用xml方式整合
原则
去除dao和daoImpl的class
在applicationContext.xml中增加bean
<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.itheima.mapper.CustomerMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--可以自行给bean加property?-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--指定核心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
在mapper.xml同路径下新增mapper.java
package com.itheima.mapper;
import com.itheima.po.Customer;;
public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}
public class DaoTest {
@Test
public void findCustometByIdMapperTest(){
ApplicationContext act = new
ClassPathXmlApplicationContext("applicationContext.xml");
CustomerMapper customerMapper = act.getBean(CustomerMapper.class);
Customer customer = customerMapper.findCustomerById(1);
System.out.print(customer);
}
}
基于MapperScannerConfigurer的整合
不需要编写DaoImpl实现类,不需要添加DaoImpl的bean。
增加一个扫描mapper的路径,直接读
<bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"/>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
用mapperScannerConfigurer+事务+service处理数据(比用mapper多一层,用service做一些业务处理)
这边增删改都要用事务(具体是数据库的事务的ACID原则,不多讲)
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
//注解注入CustomerMapper
@Autowired
private CustomerMapper customerMapper;
//添加客户
@Override
public void addCustomer(Customer customer) {
this.customerMapper.addCustomer(customer);
int i=1/0; //模拟添加操作后系统突然出现的异常问题
}
}
mapper中增加对应的方法(略)
applicationContext中增加扫描包
<context:component-scan base-package="com.itheima.service" />