spring+mybatis+mysql 事务配置

xiaoxiao2021-03-01  11

1、首先我这是maven项目,所以你要在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、Spring配置文件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(* * 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="configurationProperties"> <props> <prop key="logImpl">LOG4J</prop> </props> </property> <property name="typeAliasesPackage" value="com.lianshan.pojo" /> <property name="mapperLocations" value="classpath:mysql/**Mapper.xml" /> <property name="plugins"> <list> <bean class="com.github.pagehelper.PageHelper"> <property name="properties" value="dialect=mysql" /> </bean> </list> </property> </bean> <bean id="gooddao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="dao.gooddao" />   <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>

注:本文主要列出事务配置,适用于会配置spring的人观看,初学者请先把spring基础配置搞懂,另外大家请注意不同的数据库,事务配置会有点差异,本文适用于mysql数据库

 

转载请注明原文地址: https://www.6miu.com/read-4150355.html

最新回复(0)