SSM-整合_01

Mabatis整合Spring

单独使用Mybatis

导入Mybatis依赖jar包

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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);
	}

事务管理

  1. 开启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"/>
	
  1. 在业务方法中添加注解
@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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值