2.springboot整合mybatis-plus及事务配置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: p.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("p.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.String());
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);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd"> <mapper namespace="p.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、在l项⽬中导⼊springbootAop的依赖
<!--springboot与aop集成jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、在项⽬中创建config和项⽬同级,在项⽬l中把config⽂件打包打到项⽬中,确保启动项⽬能扫描到
3、在l中 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><!&ndash;公共资源⽂件 –>
<directory>../config</directory>
<includes>
<include&</include>
<include&l</include>
<include&l</include>
<include>messages_zh_CN.properties</include>
</includes>
</resource>-->springboot aop
</resources>
</build>
4、在springboot的启动类中加⼊扫描注解 @ImportResource(locations = {"classpath:spring-*.xml"})
@SpringBootApplication
@MapperScan("p.dao")
@ImportResource(locations ={"classpath:spring-*.xml"}) public class DomeApplication {
public static void main(String[] args){
SpringApplication.run(DomeApplication.class);
}
在spring-aop-xml中配置事务
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论