zoukankan      html  css  js  c++  java
  • 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,
      `namevarchar(20) default NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
     
    -- ----------------------------
    -- Records of user
    -- ----------------------------
    INSERT INTO `userVALUES ('001''方舟子');
    INSERT INTO `userVALUES ('002''小红');
    INSERT INTO `userVALUES ('003''樱木滑到');
    INSERT INTO `userVALUES ('005''桃木');
    INSERT INTO `userVALUES ('1''张三');
    INSERT INTO `userVALUES ('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");
    }
    }

  • 相关阅读:
    BZOJ 3744 Gty的妹子序列
    BZOJ 3872 Ant colony
    BZOJ 1087 互不侵犯
    BZOJ 1070 修车
    BZOJ 2654 tree
    BZOJ 3243 向量内积
    1003 NOIP 模拟赛Day2 城市建设
    CF865D Buy Low Sell High
    CF444A DZY Loves Physics
    Luogu 4310 绝世好题
  • 原文地址:https://www.cnblogs.com/man-li/p/4453083.html
Copyright © 2011-2022 走看看