整合Mybatis
  a)导包:
    i.Spring:基本包、aop、aspects、jdbc、tx、test;
    ii.Mybatis:mybatis-3.4.6
    iii.整合包:mybatis-spring-1.3.2
    iv.三方包:
      1.aopalliance
      2.aspectj.weaver
      3.c3p0-0.9.5.2
      4.mchange-commons-java-0.2.11
      5.mysql-connector-java-5.1.46-bin
      6.ojdbc7
  b)创建项目结构(package):bean、service、mapper、test;
  c)创建配置文件:sqlMapperConfig、applicaitonContext
      2、创建测试用例:使用Mapper扫描开发,转账;
      3、在service中加入事务:利用Spring-aop事务解决转账异常问题;

  

  未添加事务前

  

package com.Gary.bean;

public class Account {

    private Integer id;
private String name;
private Double money; //转账金额
private Double tranferMoney; public Double getTranferMoney() {
return tranferMoney;
}
public void setTranferMoney(Double tranferMoney) {
this.tranferMoney = tranferMoney;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
} }

Account.java

package com.Gary.mapper;

import com.Gary.bean.Account;

//账户mapper接口
public interface AccountMapper { //操作数据库扣款和加款 //扣款
void subMoney(Account pay); //加款
void addMoney(Account collect); }

AccountMapper.java

<?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.Gary.mapper.AccountMapper"> <update id="subMoney" parameterType="Account">
update account set money = money - #{tranferMoney} where id = #{id}
</update> <update id="addMoney" parameterType="Account">
update account set money = money + #{tranferMoney} where id = #{id}
</update> </mapper>

AccountMapper.xml

package com.Gary.service;

public interface AccountService {

    //转账方法
void tranferAccount(); }

AccountService.java

package com.Gary.service;

import javax.annotation.Resource;

import com.Gary.bean.Account;
import com.Gary.mapper.AccountMapper; public class AccountServiceImpl implements AccountService{ @Resource(type = AccountMapper.class)
private AccountMapper mapper; @Override
public void tranferAccount() { Double tranferMoney = 100d; Account pay = new Account();
pay.setId(1);
pay.setTranferMoney(tranferMoney);
//先扣款
mapper.subMoney(pay); Account collect = new Account();
collect.setId(2);
collect.setTranferMoney(tranferMoney); //加款
mapper.addMoney(collect); } }

AccountServiceImpl.java

package com.Gary.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.Gary.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MapperTest { @Resource(name = "accountService")
private AccountService as; @Test
public void Test1() {
as.tranferAccount(); } }

MapperTest.java

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.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"> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties"/> <!-- 配置 dataSource -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- mybatis -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> <!-- mapper工厂 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.Gary.mapper"/>
</bean> <!-- service -->
<bean name="accountService" class="com.Gary.service.AccountServiceImpl"> </bean> </beans>

applicationContext.xml

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_spring
jdbc.user=root
jdbc.password=123456

db.properties

<?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>
<typeAliases>
<package name="com.Gary.bean"/>
</typeAliases>
</configuration>

sqlMapConfig

  Spring中加入事务 

    a) 配置事务核心管理器: DataSourceTransactionManager;

  <!-- 需要事务核心管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

    b) 配置事务通知 tx:Advice;

    <!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>

    c) 配置aop;

    <!-- 配置aop -->
<aop:config>
<aop:pointcut expression="execution(* com.Gary.service.*ServiceImpl.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.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"> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties"/> <!-- 配置 dataSource -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- mybatis -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> <!-- mapper工厂 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.Gary.mapper"/>
</bean> <!-- service -->
<bean name="accountService" class="com.Gary.service.AccountServiceImpl">
</bean> <!-- 需要事务核心管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- 配置aop -->
<aop:config>
<aop:pointcut expression="execution(* com.Gary.service.*ServiceImpl.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config> </beans>

applicationContext.xml

package com.Gary.bean;

public class Account {

    private Integer id;
private String name;
private Double money; //转账金额
private Double tranferMoney; public Double getTranferMoney() {
return tranferMoney;
}
public void setTranferMoney(Double tranferMoney) {
this.tranferMoney = tranferMoney;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
} }

Account.java

package com.Gary.mapper;

import com.Gary.bean.Account;

//账户mapper接口
public interface AccountMapper { //操作数据库扣款和加款 //扣款
void subMoney(Account pay); //加款
void addMoney(Account collect); }

AccountMapper.java

<?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.Gary.mapper.AccountMapper"> <update id="subMoney" parameterType="Account">
update account set money = money - #{tranferMoney} where id = #{id}
</update> <update id="addMoney" parameterType="Account">
update account set money = money + #{tranferMoney} where id = #{id}
</update> </mapper>

AccountMapper.xml

package com.Gary.service;

public interface AccountService {

    //转账方法
void updateTranferAccount(); }

AccountService.java

package com.Gary.service;

import javax.annotation.Resource;

import com.Gary.bean.Account;
import com.Gary.mapper.AccountMapper; public class AccountServiceImpl implements AccountService{ @Resource(type = AccountMapper.class)
private AccountMapper mapper; @Override
public void updateTranferAccount() { Double tranferMoney = 100d; Account pay = new Account();
pay.setId(1);
pay.setTranferMoney(tranferMoney);
//先扣款
mapper.subMoney(pay); //添加异常
int i=1/0; Account collect = new Account();
collect.setId(2);
collect.setTranferMoney(tranferMoney); //加款
mapper.addMoney(collect); } }

AccountServiceImpl.java

package com.Gary.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.Gary.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MapperTest { @Resource(name = "accountService")
private AccountService as; @Test
public void Test1() {
as.updateTranferAccount(); } }

MapperTest.java

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.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"> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties"/> <!-- 配置 dataSource -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- mybatis -->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> <!-- mapper工厂 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.Gary.mapper"/>
</bean> <!-- service -->
<bean name="accountService" class="com.Gary.service.AccountServiceImpl">
</bean> <!-- 需要事务核心管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- 配置aop -->
<aop:config>
<aop:pointcut expression="execution(* com.Gary.service.*ServiceImpl.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config> </beans>

applicationContext.xml

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_spring
jdbc.user=root
jdbc.password=123456

db.properties

<?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>
<typeAliases>
<package name="com.Gary.bean"/>
</typeAliases>
</configuration>

sqlMapConfig.xml

JavaWeb_(Spring框架)整合Mybatis加入事务操作数据库的更多相关文章

  1. spring 框架整合mybatis的源码分析

    问题:spring 在整合mybatis的时候,我们是看不见sqlSessionFactory,和sqlsession(sqlsessionTemplate 就是sqlsession的具体实现)的,这 ...

  2. Spring框架整合Mybatis项目

    第一步:导入相关依赖jar包 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybat ...

  3. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  4. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  5. JavaWeb_(Spring框架)Spring整合Hibernate

    Dao层类要继承HibernateDaoSupport.java父类 原先使用Hibernate框架hibernate.cfg.xml配置数据库 <hibernate-configuration ...

  6. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  7. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  8. 整合Spring框架和MyBatis框架

    ------------------------siwuxie095                                 整合 Spring 框架和 MyBatis 框架         ...

  9. Spring MVC整合Mybatis 入门

    本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spri ...

随机推荐

  1. 【SQL Server性能优化】删除大量数据的方法比较

    原文:[SQL Server性能优化]删除大量数据的方法比较 如果你要删除表中的大量数据,这个大量一般是指删除大于10%的记录,那么如何删除,效率才会比较高呢? 而如何删除才会对系统的影响相对较小呢? ...

  2. Go part 5 结构体,方法与接收器

    结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...

  3. Go Select使用

    原文:https://golangbot.com/pointers/ 作者:Nick Coghlan 译者:Noluye 什么是 select? select 语句用于在多个发送/接收信道操作中进行选 ...

  4. 如何解决Win10不能新建项目的问题?

    在Win10系统中,我们有时候会遇到右键菜单中没有新建项的情况,这时我们就无法进行文件的新建,非常耽误我们使用电脑.下面雨后清风就来分享一下Win10系统右键菜单没有新建项的解决方法. Win10系统 ...

  5. 日志实时查看、轮询、统计、监控工具 Loggrove

    Loggrove 是对日志文件进行 阅读.轮询.关键词匹配.监控告警.图表展示 的 Web 服务. 下载地址 https://github.com/olajowon/loggrove 超轻组件 Pyt ...

  6. httpd-2.4源码编译

    APR     APR(Apache portable Run-time libraries,Apache可移植运行库) 主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库.在早 ...

  7. 2018/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】

    问题 A: A + B 普拉斯 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下:  现在我们用01来构成这些数字 当宝儿姐输入A + B 时(log10(A)<50,log10 ...

  8. JAVA遇见HTML——Servlet篇:Servlet基础

    代码实现: HelloServlet package servlet; import java.io.IOException; import java.io.PrintWriter; import j ...

  9. Java集合--ArrayList,LinkedList性能分析

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308900.html 第1部分 List概括 先回顾一下List的框架图 (01) List 是一个接口 ...

  10. Python2和Python3中新式类、经典类(旧式类)的区别

    https://www.jianshu.com/p/6f9d99f7ad54 里面最后一张图应该输出   This is from C