Mabatis整合Spring
单独使用Mybatis
导入Mybatis依赖jar包
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190903122311449.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hwZW5nYw==,size_16,color_FFFFFF,t_70)
MybatisConfig.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>
<!--引入外部properties文件 -->
<properties resource="db.properties"></properties>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,事务控制由mybatis管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池,由mybatis管理-->
<dataSource type="POOLED">
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
<property name="driver" value="${driver}"/>
<property name="poolMaximumActiveConnections" value="${maxActive}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper class="com.xian.dao.CustomerMapper"/>
</mappers>
</configuration>
Mapper映射接口与xml
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190903120848145.png)
public interface CustomerMapper {
void save(Customer customer) ;
}
<?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.xian.dao.CustomerMapper">
<!-- 添加客户 -->
<insert id="save" parameterType="com.xian.domain.Customer">
INSERT INTO t_customer(
NAME, gender, telephone, address)
VALUES(
#{name}, #{gender}, #{telephone}, #{address}
)
</insert>
</mapper>
测试类
@Test
public void test() throws IOException {
//1. 创建SQLSessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 加载配置文件
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
//2. 创建sqlSessionFactory
SqlSessionFactory factory = builder.build(is);
//3. 打开SQLSession
SqlSession sqlSession = factory.openSession();
//4. 获取Mapper接口的对象
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
//5. 操作
Customer customer = new Customer();
customer.setName("小张");
customer.setTelephone("020-333333");
customer.setGender('男');;
customer.setAddress("天河城");
customerMapper.save(customer);
// 6. 提交事务
sqlSession.commit();
/// 7. 关闭资源
sqlSession.close();
}
有Mapper实现类的整合
导入依赖的jar
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190903122408115.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hwZW5nYw==,size_16,color_FFFFFF,t_70)
MapperImpl
public class CustomerMapperImpl extends SqlSessionDaoSupport
implements CustomerMapper {
@Override
public void save(Customer customer) {
SqlSession sqlSession = this.getSqlSession();
sqlSession.insert("save", customer);
//不需要事务的提交
}
}
Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!--创建DateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="url" value="${db.url}"/>
<property name="driverClassName" value="${db.driver}"/>
<property name="maxActive" value="${db.maxActive}"></property>
</bean>
<!-- 创建一个SqlSessFactory对象 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载SQL映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 创建CustomerMapperImpl对象, 注入SQLSessionFactory -->
<bean id="customerMapper" class="com.xian.impl.CustomerMapperImpl">
<!-- 关联sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
测试
@Test
public void test() {
//1. 加载Spring配置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 获取对象
CustomerMapper customerMapper = (CustomerMapper) ac.getBean("customerMapper");
//3. 调用方法
Customer customer = new Customer();
customer.setName("小李");
customer.setTelephone("020-333333");
customer.setGender('男');;
customer.setAddress("天河城");
customerMapper.save(customer);
}
没有MapperImpl的整合
更改配置applicationContxt.xml
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!--创建DateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="url" value="${db.url}"/>
<property name="driverClassName" value="${db.driver}"/>
<property name="maxActive" value="${db.maxActive}"></property>
</bean>
<!-- 创建一个SqlSessFactory对象 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载SQL映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置Mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperFactoryBean" id="customerMapper">
<!-- 关联Mapper接口 -->
<property name="mapperInterface" value="com.xian.dao.CustomerMapper"></property>
<!-- 关联SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
测试
@Test
public void test() {
//1. 加载Spring配置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 获取对象
CustomerMapper customerMapper = (CustomerMapper) ac.getBean("customerMapper");
//3. 调用方法
Customer customer = new Customer();
customer.setName("小李");
customer.setTelephone("020-333333");
customer.setGender('男');;
customer.setAddress("天河城");
customerMapper.save(customer);
}
Mapper接口扫描
配置Mapper接口的扫面appplicationContext.xml
<!-- 读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!--创建DateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="url" value="${db.url}"/>
<property name="driverClassName" value="${db.driver}"/>
<property name="maxActive" value="${db.maxActive}"></property>
</bean>
<!-- 创建一个SqlSessFactory对象 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载SQL映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置Mapper接口的扫描
每个Mapper接口在Spring容器中id名称为id的类名首字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口所在包的路径 -->
<property name="basePackage" value="com.xian.dao"></property>
</bean>
测试
@Test
public void test() {
//1. 加载Spring配置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 获取对象
CustomerMapper customerMapper = (CustomerMapper) ac.getBean("customerMapper");
//3. 调用方法
Customer customer = new Customer();
customer.setName("小李");
customer.setTelephone("020-333333");
customer.setGender('男');;
customer.setAddress("天河城");
customerMapper.save(customer);
}
事务管理
- 开启spring事务管理
<!-- 开启Spring的IOC基于注解的扫描 -->
<context:component-scan base-package="com.xian.*"></context:component-scan>
<!-- 开启Spring事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
- 在业务方法中添加注解
@Transactional()
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public void saveCustomer(Customer customer) {
customerMapper.save(customer);
//模拟异常
int i = 100/0;
customerMapper.save(customer);
}
}