spring+mybatis+druid+mysql+maven事务配置
1、首先pom.xml文件里面需要用到的jar配置:
<!-- spring事务,包含了@Transactional标注 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.9</version>
</dependency>
<!-- mysqljdbc驱动 ,这个是数据库连接池需要的-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
<!-- 事务:DataSourceTransactionManager -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<!-- 事务aop:config -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<!-- 事务SqlSessionFactoryBean -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!-- 要使用到mybatis,必须要用mybatis这个包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
2、config.properties内容
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.133:3306/salt?characterEncoding=utf-8
jdbc.user=root
jdbc.password=root1234
3、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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!--指定spring注入层 -->
<context:component-scan base-package="com.hskj" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
</bean>
<!-- ================================事务相关控制================================================= -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></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.hskj.service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="MyBatis-Configuration.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.hskj.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
4、MyBatis-Configuration.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>
<mappers>
<mapper resource="UserDaoMapper.xml"/>
</mappers>
</configuration>
5、UserDaoMapper.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.hskj.mapper.UserMapper">
<parameterMap type="com.hskj.domain.User" id="parameterMapUser">
<parameter property="id"/>
<parameter property="name"/>
</parameterMap>
<resultMap type="com.hskj.domain.User" id="resultMapUser">
<result property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<insert id="insertUser" parameterMap="parameterMapUser">
INSERT INTO user(id,name)
VALUES(?,?)
</insert>
<select id="countAll" resultType="int">
select count(*) c from user;
</select>
<select id="getAllUser" resultMap="resultMapUser">
SELECT * FROM user
</select>
<select id="getById" parameterType="String" resultMap="resultMapUser">
SELECT * FROM user
WHERE id=#{value}
</select>
<delete id="deleteUser" parameterType="String">
DELETE FROM user
WHERE id=#{value}
</delete>
<update id="updateUser" parameterType="java.util.Map">
UPDATE user
SET name=#{name}
WHERE id=#{id}
</update>
</mapper>
6、数据库建表语句:
----------------------------
Table structure for `user`
----------------------------
DROP
TABLE
IF EXISTS `
user
`;
CREATE
TABLE
`
user
` (
`id`
varchar
(20)
NOT
NULL
,
`
name
`
varchar
(20)
default
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB
DEFAULT
CHARSET=gb2312;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT
INTO
`
user
`
VALUES
(
'001'
,
'方舟子'
);
INSERT
INTO
`
user
`
VALUES
(
'002'
,
'小红'
);
INSERT
INTO
`
user
`
VALUES
(
'003'
,
'樱木滑到'
);
INSERT
INTO
`
user
`
VALUES
(
'005'
,
'桃木'
);
INSERT
INTO
`
user
`
VALUES
(
'1'
,
'张三'
);
INSERT
INTO
`
user
`
VALUES
(
'2'
,
'李四'
);
7、代码层次结构:
src/main/java
|--com.hskj.dao
|--UserDao.java内容如下:
package com.hskj.dao;
import java.util.List;
import java.util.Map;
import com.hskj.domain.User;
public interface UserDao {
public int countAll();
public void insertUser(User user);
public List<User> getAllUser();
public User getById(String id);
public void deleteUser(String id);
public void updateUser(Map<String,Object> map);
}
src/main/java
|--com.hskj.daoImpl
|--UserDaoImpl.java内容如下:
package com.hskj.daoImpl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hskj.dao.UserDao;
import com.hskj.domain.User;
import com.hskj.mapper.UserMapper;
@Service("userDao")
public class UserDaoImpl implements UserDao{
@Autowired
private UserMapper userMapper;
public int countAll() {
return this.userMapper.countAll();
}
public void insertUser(User user) {
this.userMapper.insertUser(user);
}
public List<User> getAllUser() {
return this.userMapper.getAllUser();
}
public User getById(String id) {
return this.userMapper.getById(id);
}
public void deleteUser(String id) {
this.userMapper.deleteUser(id);
}
public void updateUser(Map<String, Object> map) {
this.userMapper.updateUser(map);
}
}
src/main/java
|--com.hskj.domain
|--User.java内容如下:
package com.hskj.domain;
public class User {
private String id;
private String name;
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 User() {
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
}
src/main/java
|--com.hskj.mapper
|--UserMapper.java内容如下:
package com.hskj.mapper;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.hskj.domain.User;
@Service("userMapper")
public interface UserMapper {
public int countAll();
public void insertUser(User user);
public List<User> getAllUser();
public User getById(String id);
public void deleteUser(String id);
public void updateUser(Map<String,Object> map);
}
src/main/java
|--com.hskj.service
|--UserService.java内容如下:
package com.hskj.service;
import java.util.Map;
import com.hskj.domain.User;
public interface UserService {
public int countAll();
public void insertUser(User user);
public void update_insert(Map map,User user);
}
src/main/java
|--com.hskj.serviceImpl
|--UserServiceImpl.java内容如下:
package com.hskj.serviceImpl;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hskj.dao.UserDao;
import com.hskj.domain.User;
import com.hskj.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public int countAll() {
return this.userDao.countAll();
}
public void insertUser(User user) {
this.userDao.insertUser(user);
throw new RuntimeException("Error");
}
public void update_insert(Map map,User user) {
this.userDao.updateUser(map);
this.userDao.insertUser(user);
throw new RuntimeException("Error");
}
}
spring+mybatis+druid+mysql+maven事务配置的更多相关文章
- 微服务配置中心实战:Spring + MyBatis + Druid + Nacos
在结合场景谈服务发现和配置中我们讲述了 Nacos 配置中心的三个典型的应用场景,包括如何在 Spring Boot 中使用 Nacos 配置中心将数据库连接信息管控起来,而在“原生”的 Spring ...
- spring+mybatis的多源数据库配置实战
前言: 关于spring+mybatis的多源数据库配置, 其实是个老生常谈的事情. 网上的方案出奇的一致, 都是借助AbstractRoutingDataSource进行动态数据源的切换. 这边再无 ...
- 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?
Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...
- 事务隔离级别与传播机制,spring+mybatis+atomikos实现分布式事务管理
1.事务的定义:事务是指多个操作单元组成的合集,多个单元操作是整体不可分割的,要么都操作不成功,要么都成功.其必须遵循四个原则(ACID). 原子性(Atomicity):即事务是不可分割的最小工作单 ...
- Spring事务隔离级别与传播机制详解,spring+mybatis+atomikos实现分布式事务管理
原创说明:本文为本人原创作品,绝非他处转载,转账请注明出处 1.事务的定义:事务是指多个操作单元组成的合集,多个单元操作是整体不可分割的,要么都操作不成功,要么都成功.其必须遵循四个原则(ACID). ...
- spring+mybatis+atomikos 实现JTA事务
1. 选择哪种transaction manager? 在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务. ...
- Spring+mybatis+struts框架整合的配置具体解释
学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...
- 在Spring+MyBatis组合中使用事务
通过Spring和MyBatis的组合,给出一个较为详细的实例 代码清单:配置Spring+MyBatis测试环境 <?xml version='1.0' encoding='UTF-8' ? ...
- spring boot + swagger + mysql + maven
1.首先编写 yaml 文件,创建项目所需的接口,在swagger.io官网上生成 spring boot项目: 2.由于生成的spring boot项目是公共类的所以还需要修改成所需的项目名称,主要 ...
随机推荐
- 实验一Java开发环境的熟悉
实验一Java开发环境的熟悉 实验内容 •命令行下Java程序开发 •IDEA下Java程序开发.调试 •练习(通过命令行和Eclipse两种方式实现,在Eclipse下练习调试程序) •实现凯撒密码 ...
- BizTalk动手实验(十)业务活动监控(BAM)演示
1 课程简介 通过本课程熟悉业务活动监控(BAM)的使用及各组件的配置. (本环境为Windows 2008 32位操作系统环境 + Visual Studio 2010 + BizTalk 210) ...
- PHP第三方登录 -- 微博登录
进化史 博客园 首页 新随笔 联系 管理 订阅 随笔- 9 文章- 0 评论- 0 php 实现qq第三方登录 学习之前,请大家先看一下oAuth协议. 首先呢,我们进入QQ互联的官方网站 ht ...
- Unity学习疑问记录之layer问题
在Sprite Render中有个Sorting Layer,这里可以建层,而Inspector窗口中也有个layer,也可以新建层,这2者有什么不一样呢? layer主要通过光线投射来选择性地忽略碰 ...
- centos7 systemctl命令
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 实例: 启动nfs服务:systemctl start nfs-server.s ...
- [curator] Netflix Curator 使用
curator简介 Netflix curator 是Netflix公司开源的一个Zookeeper client library,用于简化zookeeper客户端编程,包含一下几个模块: curat ...
- 手动启动mongodb和nodejs程序
最近做单片机去了,以前用的mongodb和nodejs没有配置成服务,居然忘了如何手动启动.在此记录下 一.手动启动mongodb 1.进入cmd 2.在dos下打开mongodb路径E:\mongo ...
- Struts 404 The requested resource is not available
出现这种错误一般是struts.xml配置错误,重点针对<action> 的class属性的全名 因为struts感觉应该是先加载所有的配置文件,如果配置文件有错误的话,所有的资源就都无效 ...
- VBA的打开关闭保存另存为等事件无法正常跑的原因
打开执行的代码需要写在thisworkbook的open事件下!!!!!如下图: VBA中事件分为三种:工作簿事件,工作表事件,窗体.控件事件. 工作簿事件发生在特定的工作簿中,如Open(打开工作簿 ...
- 分享大家一个背景为下雪的JQuery
<html><head> <meta charset="utf-8"> <meta content="IE=edge,chrom ...