spring+mybatis的事务配置
public class Emp {
private String id;
private String name;
private String sex;
private int age;
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
import java.util.List;
import java.util.Map;
import com.lixing.scm.entity.Emp;
public interface EmpMapper {
void insertEmp(Emp emp);
List<Emp> getAllEmp();
Emp getById(String id);
void deleteEmp(String id);
void updateEmp(Map<String,Object> map);
}
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lixing.scm.test.mapper.EmpMapper">
<parameterMap type="com.lixing.scm.entity.Emp" id="parameterMapEmp">
<parameter property="id"/>
<parameter property="name"/>
<parameter property="sex"/>
<parameter property="age"/>
<parameter property="phone"/>
</parameterMap>
<resultMap type="com.lixing.scm.entity.Emp" id="resultMapEmp">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
</resultMap>
<insert id="insertEmp" parameterMap="parameterMapEmp">
INSERT INTO emp(id,name,sex,age,phone)
VALUES(?,?,?,?,?)
</insert>
<select id="getAllEmp" resultMap="resultMapEmp">
SELECT * FROM emp
</select>
<select id="getById" parameterType="String" resultMap="resultMapEmp">
SELECT * FROM emp
WHERE id=#{value}
</select>
<delete id="deleteEmp" parameterType="String">
DELETE FROM emp
WHERE id=#{value}
</delete>
<update id="updateEmp" parameterType="java.util.Map">
UPDATE emp
SET name=#{name},sex=#{sex},age=#{age},phone=#{phone}
WHERE id=#{id}
</update>
</mapper>
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- -->
<context:annotation-config />
<context:component-scan base-package="com.lixing.scm.test.*" />
<!-- jdbc.propertis Directory -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>
<bean id="MyDataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="MyDataSource" />
</bean>
<!-- ScanMapperFiles -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lixing.scm.test.mapper" />
</bean>
<!-- ================================事务相关控制================================================= -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="MyDataSource"></property>
</bean>
<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc" expression="execution(public * com.lixing.scm.test.service.*.*(..))" /> <!--把事务控制在Service层-->
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>
<!-- 以下为自定义Bean-->
<bean id="empDao" class="com.lixing.scm.test.dao.impl.EmpDaoImpl"
autowire="byName" />
<bean id="empService" class="com.lixing.scm.test.service.impl.EmpServiceImpl" autowire="byName"/>
</beans>
import java.util.List;
import java.util.Map;
import com.lixing.scm.entity.Emp;
public interface EmpDao {
void insertEmp(Emp emp);
List<Emp> getAllEmp();
Emp getById(String id);
void deleteEmp(String id);
void updateEmp(Map<String, Object> map);
}
import java.util.List;
import java.util.Map;
import com.lixing.scm.entity.Emp;
import com.lixing.scm.test.dao.EmpDao;
import com.lixing.scm.test.mapper.EmpMapper;
public class EmpDaoImpl implements EmpDao {
private EmpMapper empMapper; //在此处注入一个empMapper
//这个empMapper由 Spring自动生成 //不需要我们自己手工去定义
@Override
public void insertEmp(Emp emp) {
this.empMapper.insertEmp(emp);
throw new RuntimeException("Error"); //测试抛出RuntimeException //异常查看数据库是否存在记录
}
@Override
public void deleteEmp(String id) {
this.empMapper.deleteEmp(id);
}
@Override
public List<Emp> getAllEmp() {
return this.empMapper.getAllEmp();
}
@Override
public Emp getById(String id) {
return this.empMapper.getById(id);
}
@Override
public void updateEmp(Map<String, Object> map) {
this.empMapper.updateEmp(map);
}
public EmpMapper getEmpMapper() {
return empMapper;
}
public void setEmpMapper(EmpMapper empMapper) {
this.empMapper = empMapper;
}
}
Service层接口:EmpService.java
import com.lixing.scm.entity.Emp;
public interface EmpService {
void insertEmp(Emp emp);
}
import com.lixing.scm.entity.Emp;
import com.lixing.scm.test.dao.EmpDao;
import com.lixing.scm.test.service.EmpService;
public class EmpServiceImpl implements EmpService {
private EmpDao empDao;
@Override
public void insertEmp(Emp emp) {
empDao.insertEmp(emp);
}
public EmpDao getEmpDao() {
return empDao;
}
public void setEmpDao(EmpDao empDao) {
this.empDao = empDao;
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lixing.scm.entity.Emp;
import com.lixing.scm.test.service.EmpService;
public class TestEmpService {
@Test
public void testTrasaction(){
Emp emp=new Emp();
emp.setId("00000003");
emp.setName("某某某");
emp.setAge(50);
emp.setSex("男");
emp.setPhone("566666");
ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
EmpService service=ctx.getBean(EmpService.class);
service.insertEmp(emp);
}
}
spring+mybatis的事务配置的更多相关文章
- Spring+MyBatis双数据库配置
Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...
- Spring AOP及事务配置三种模式详解
Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...
- Spring+MyBatis实践—工程配置
初次实践:Spring+MyBatis技术搭建框架,采用Bootstrap前端开源框架. 简介: MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所 ...
- Spring管理 hibernate 事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...
- Spring声明式事务配置详解
Spring支持编程式事务管理和声明式的事务管理. 编程式事务管理 将事务管理代码嵌到业务方法中来控制事务的提交和回滚 缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码 声明式事务管理 一般情 ...
- spring提供的事务配置--纯注解
spring提供的事务--纯注解 模拟转账业务 ,出错需要事务回滚,没错正常执行 事务和数据库技术都是spring的内置提供的 --------dao包--------------- IAccoun ...
- spring+mybatis+springmvc的配置
1.web.xml的配置 <?xml version="1.0" encoding="UTF-8"?><web-app version=&qu ...
- Spring+Mybatis+Maven 整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="by ...
- Spring Mybatis多数据源配置范例
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
随机推荐
- Monkey源代码分析之事件源
上一篇文章<Monkey源代码分析之执行流程>给出了monkey执行的整个流程.让我们有一个概貌,那么往后的文章我们会尝试进一步的阐述相关的一些知识点. 这里先把整个monkey类的结构图 ...
- Android Studio 2.3版本 Run项目不能自动启动APP的问题 (转)
参考: http://blog.csdn.net/lucasey/article/details/61071377 Android Studio 升级到2.3版本后 运行项目后,只是安装上了,而APP ...
- CSDN--字体颜色--markdown
在写blog时,想高亮某些字,但是发现markdown更改字体颜色不像word里那么方便,于是查了一下,要用一下代码进行更改字体颜色,还可以更改字体大小,还有字体格式 <font 更改语法> ...
- Android SQLite基本用法
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- uitableview滚动到最后一行
本文转载至 http://mrjeye.iteye.com/blog/1278521 - (void)scrollTableToFoot:(BOOL)animated { NSInteger s = ...
- 1. lvs+keepalived 高可用群集
一. keepalived 工具介绍 1.专为lvs 和HA 设计的一款健康检查工具 2.支持故障自动切换 3.支持节点健康状态检查 二. keepalived 实现原理剖析 keepalived ...
- 线性同余方程组模板( x=r0(mod m0) )
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- poj2816
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29799 Accepted: 12090 De ...
- 【BZOJ3743】[Coci2015]Kamp 树形DP
[BZOJ3743][Coci2015]Kamp Description 一颗树n个点,n-1条边,经过每条边都要花费一定的时间,任意两个点都是联通的. 有K个人(分布在K个不同的点)要集中到一个点举 ...
- Hive高级
HiveServer2 概述: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview2 客户端: https:// ...