1.导入依赖jar
<!-- 连接池 -->
<dependency>
<!--自动配置-->
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!--mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- 提供get set -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.配置yaml文件
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mybatis-plus:
mapper-locations: classpath:mybatis/*.xml
type-aliases-package: com.zrp.po
configuration:
#这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
#---------本工程专属配置-------------------
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: qwe@123
#连接池
druid:
defaultAutoCommit: false
defaultReadOnly: false
#初始化大小
initialSize: 2
#最大值
maxActive: 5
#最小值
minIdle: 2
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 18000
#配置一个连接在池中最小生存的时间
minEvictableIdleTimeMillis: 600000
maxEvictableIdleTimeMillis: 1200000
poolPreparedStatements: true
maxOpenPreparedStatements: 100
testOnBorrow: true
testOnReturn: true
testWhileIdle: true
validationQuery: select 1
removeAbandonedOnBorrow: true
removeAbandoned: true
removeAbandonedOnMaintenance: true
removeAbandonedTimeout: 300
maxWaitMillis: 3000
keepalive: true
phyMaxUseCount: 2000
#'wall'用于防火墙,SpringBoot中没有log4j,我改成了log4j2
filters: stat,log4j2,config
3.在SpringBoot启动类加扫描注解@MapperScan
/**
* @author 赵锐鹏
* @date 2021/11/1 0001
*/
@SpringBootApplication
@MapperScan("com.zrp.dao")
@RestController
public class DomeApplication {
public static void main(String[] args) {
SpringApplication.run(DomeApplication.class);
}
@Autowired
private UserMapper userMapper;
@GetMapping("/test")
public Object test() {
User user = userMapper.selectById("1");
Object users = userMapper.selectBy("1");
System.out.println(users.toString());
return user;
}
}
4.生成dao和po和mapper
UserPo
@Data
@TableName("tbl_base_user")
public class User {
@TableId
private int userId;
private String userName;
private int age;
}
UserDao
/**
* @author 赵锐鹏
* @date 2021/11/1 0001
*/
public interface UserMapper extends BaseMapper<User> {
Object selectBy(String id);
}
UserMapper.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.zrp.dao.UserMapper">
<select id="selectBy" resultType="java.lang.Object">
SELECT * FROM tbl_base_user WHERE USER_ID = #{id}
</select>
</mapper>
5.添加事务
5.1在service层添加@Transactional注解
@Service
@Transactional
public class UserService {
}
5.2配置xml事务
1、在pom.xml项目中导入springbootAop的依赖
<!--springboot与aop集成jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、在项目中创建config和项目同级,在项目pom.xml中把config文件打包打到项目中,确保启动项目能扫描到
3、在pom.xml中 dependencies 标签下面的添加
<build>
<resources>
<resource><!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 -->
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<!-- 打包是从根目录 config为项目src同级的config ../config根项目同级的文件 -->
<directory>../config</directory>
<filtering>true</filtering>
<includes>
<!-- config下面所有文件全部打到包中去 -->
<include>**/*</include>
</includes>
</resource>
<!-- <resource><!–公共资源文件 –>
<directory>../config</directory>
<includes>
<include>banner.txt</include>
<include>mybatis-configuration.xml</include>
<include>application-aop-service.xml</include>
<include>messages_zh_CN.properties</include>
</includes>
</resource>-->
</resources>
</build>
4、在springboot的启动类中加入扫描注解 @ImportResource(locations = {"classpath:spring-*.xml"})
@SpringBootApplication
@MapperScan("com.zrp.dao")
@ImportResource(locations = {"classpath:spring-*.xml"})
public class DomeApplication {
public static void main(String[] args) {
SpringApplication.run(DomeApplication.class);
}
在spring-aop-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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.xsd"
default-lazy-init="false">
<!--***************** 应用本文件的模块必须依赖: spring-boot-starter-aop ******-->
<!-- Spring默认不支持@Aspect风格的切面声明,通过如下配置开启@Aspect支持 -->
<aop:aspectj-autoproxy expose-proxy="true" />
<!-- 统一事务配置规则 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 查询类方法readonly=true -->
<tx:method name="query*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception"/>
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception"/>
<tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception"/>
<!-- 更新类方法readonly=false -->
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="create*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="lock*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="unLock*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="change*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="reset*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="recover*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="java.lang.Exception"/>
<!-- 其他方法readonly=true -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<!-- 全局切入点:service包及子包下所有以ServiceImpl结尾的类的方法 -->
<aop:pointcut id="transaction_log_exceptionPointCut"
expression="execution(public * com.zrp.service..*(..))"/>
<!-- 织入全局事务 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="transaction_log_exceptionPointCut" order="2" />
<!-- <!– 织入全局日志 –>
<aop:aspect ref="logAspect" order="1">
<!– 全局统一日志 –>
<aop:around method="doAround" pointcut-ref="transaction_log_exceptionPointCut"/>
<!– 全局统一异常
<aop:after-throwing method="doAfterThrowing" pointcut-ref="transaction_log_exceptionPointCut" throwing="ex"/>
–>
</aop:aspect>-->
</aop:config>
</beans>