原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://kinglixing.blog.51cto.com/3421535/723870
在网上查了MyBatis+Spring的结合,真的是太多太多了,可是没有几个代码是完整的..这两项整合花了我两天时间,终于被我整合完成...其实也很简单,原因:JAR包的问题...

由于Ibatis被改名为MyBatis,所以,网上很多都是有关Ibatis而MyBatis却很少很少...
本文以MyBatis3.0.6 + Spring3.0.6为例结合(一定要这个版本才行):
定义一个实体类:Emp.java

package com.lixing.scm.entity;

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

  定义实体内操作接口:EmpMapper.java

package com.lixing.scm.test.mapper;

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

  定义实体类操作接口的映射文件:EmpMapper.xml

<?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.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>

  

Spring3.0.6定义:applicationContext.xml

<?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-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>

  DAO接口:EmpDAO.java

package com.lixing.scm.test.dao;

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

  DAO接口实现类:EmpDaoImpl.java

package com.lixing.scm.test.dao.impl;

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

package com.lixing.scm.test.service;

import com.lixing.scm.entity.Emp;

public interface EmpService {
void insertEmp(Emp emp);
}

  Service层接口实现类:EmpServiceImpl.java

package com.lixing.scm.test.service.impl;

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

  测试类:TestEmpService.java

import org.junit.Test;
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);
}
}

  本文出自 “李新博客” 博客,请务必保留此出处http://kinglixing.blog.51cto.com/3421535/723870

MyBatis+Spring 事务管理的更多相关文章

  1. spring事务管理学习

    spring事务管理学习 spring的事务管理和mysql自己的事务之间的区别 参考很好介绍事务异常回滚的文章 MyBatis+Spring 事务管理 spring中的事务回滚例子 这篇文章讲解了@ ...

  2. MyBatis6:MyBatis集成Spring事务管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事务管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基 ...

  3. MyBatis(5):MyBatis集成Spring事务管理(上)

    单独使用MyBatis对事务进行管理 前面MyBatis的文章有写过相关内容,这里继续写一个最简单的Demo,算是复习一下之前MyBatis的内容吧,先是建表,建立一个简单的Student表: 1 2 ...

  4. Spring+JTA+Atomikos+mybatis分布式事务管理

    我们平时的工作中用到的Spring事务管理是管理一个数据源的.但是如果对多个数据源进行事务管理该怎么办呢?我们可以用JTA和Atomikos结合Spring来实现一个分布式事务管理的功能.了解JTA可 ...

  5. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  6. SSM(spring mvc+spring+mybatis)学习路径——1-2、spring事务管理

    目录 1-2 Spring事务管理 概念介绍 事务回顾 事务的API介绍 Spring 事务管理 转账案例 编程式事务管理 声明式事务管理 使用XML配置声明式事务 基于tx/aop 使用注解配置声明 ...

  7. Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等

    这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...

  8. SpringMVC+MyBatis整合——事务管理

    项目一直没有做事务管理,这几天一直在想着解决这事,今天早上终于解决了.接下来直接上配置步骤. 我们项目采用的基本搭建环境:SpringMVC.MyBatis.Oracle11g.WebLogic10. ...

  9. Spring 事务管理原理探究

    此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...

随机推荐

  1. TDirectory.GetFiles获取指定目录下的文件

    使用函数: System.IOUtils.TDirectory.GetFiles 所有重载: class function GetFiles(const Path: string): TStringD ...

  2. 2016030401 - java性能优化建议

    转载自:http://www.open-open.com/lib/view/open1399884636989.html#_label20 1.没有必要时不要使用静态变量 使用静态变量的目的是提高程序 ...

  3. C++版 Chip8游戏模拟器

    很早就想写个FC模拟器,但真是一件艰难的事情.. 所以先写个Chip8模拟器,日后再继续研究FC模拟器. Chip8只有35条指令,属于RISC指令集,4k内存,2k显存,16个寄存器(其中15个通用 ...

  4. Codeforces Round #316 div2

    一场充满血腥hack之战!!! Problem_A: 题意: n个候选人在m个城市进行投票,每个城市选出票数最多的一个候选人为城市候选人,如果票数相同,则取编号小的候选人. 再从这m个城市候选人中选出 ...

  5. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路

    Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上经过的小岛依次出 ...

  6. EasyUI 树形菜单tree 定义图标

    { "id":1, "text":"Folder1", "iconCls":"icon-save", ...

  7. Android LogCat 日志记录

    日志级别列表如下(从低到高): V — Verbose (lowest priority) D — Debug I — Info W — Warning E — Error F — Fatal S — ...

  8. 【BZOJ 3926】 [Zjoi2015]诸神眷顾的幻想乡 (广义SAM)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 974  Solved: 573 Descriptio ...

  9. struts2文件下载出现Can not find a java.io.InputStream with the name的错误

    今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...

  10. 最简单的CRC32源码---逐BIT法

    CRC其实也就那么回事,却在网上被传得神乎其神.单纯从使用角度来说,只需要搞明白模二除法,再理解一些偷懒优化的技巧,就能写出自己的CRC校验程序. 下面的代码完全是模拟手算过程的,效率是最低的,发出来 ...