栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2.springboot整合mybatis-plus及事务配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2.springboot整合mybatis-plus及事务配置

1.导入依赖jar
    
    
            
            com.alibaba
            druid-spring-boot-starter
            1.1.14
    
    
    
            mysql
            mysql-connector-java
            8.0.20
    
      
    
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
    
    
    
            org.projectlombok
            lombok
            true
    
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
@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

public interface UserMapper extends baseMapper {

    Object selectBy(String id);

}

UserMapper.xml






    


5.添加事务
5.1在service层添加@Transactional注解
@Service
@Transactional
public class UserService {

    
}
5.2配置xml事务

1、在pom.xml项目中导入springbootAop的依赖

        
        
            org.springframework.boot
            spring-boot-starter-aop
        

2、在项目中创建config和项目同级,在项目pom.xml中把config文件打包打到项目中,确保启动项目能扫描到

3、在pom.xml中 dependencies 标签下面的添加

 
        
            
                src/main/resources
                
                    ***
                
            
           
        
    

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中配置事务



    


    
    

    
    
        
            
            
            
            
            
            

            
            
            
            
            
            
            
            
            
            
            
            
            

            
            
        
    

    
        
        

        
        

       

    


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号