mybatis plus 自动填充数据 & 乐观锁& 分页查询

1. 配置文件


#设置数据源mysql
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://152.136.27.48:3306/d_tina?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456

mybatis-plus:
  #mybatisPlus mapper xml文件地址
  mapper-locations:  classpath*:mapper/*.xml
  # mybaits-plus type-aliases 文件地址
  type-aliases-package: com.example.mybatisplus.entity
  # 驼峰下划线转换
  global-config:
    db-column-underline: true
#    配置mybatis plus 打印sql语句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 8001


2. pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        

        
        
            mysql
            mysql-connector-java
        

        
        
            org.projectlombok
            lombok
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3. 配置类

MybatisPusConfig 插件配置

@EnableTransactionManagement
@Configuration
public class MybatisPusConfig {


    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }

    //逻辑删除插件
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

    //乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

}

MybatisPlusHandler 自动填充数据设置

@Component
public class MybatisPlusHandler implements MetaObjectHandler {

     //insertFill方法 在mp执行添加操作的时候运行
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("gmtCreate",new Date(),metaObject);
        this.setFieldValByName("gmtModified",new Date(),metaObject);
        //添加乐观锁 默认值是1
        this.setFieldValByName("version",1,metaObject);
        //添加逻辑删除 默认值是0 (0 是不删除 1是删除)
        this.setFieldValByName("delFlag", 0, metaObject);
    }

    //updateFill方法在mp执行修改操作的时候运行
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
    
}

4. 实体类

@Data
@TableName(value = "user")
@NoArgsConstructor//无参构造函数
@AllArgsConstructor //有参构造函数
public class User {

    @TableId(type = IdType.ID_WORKER_STR)//配置雪花算法
    private String id;

    private String name;

    private Integer gender;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;//创建时间

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;//修改时间

    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;

    @TableField(fill = FieldFill.INSERT)
    @TableLogic
    private Integer delFlag;
   
}

4. MybatisPlusApplicationTests 测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class MybatisPlusApplicationTests {

    @Resource
    private UserDao userDao;

    @Test //测试逻辑删除
    public void test2(){
        int rows= userDao.deleteById("1230006285945135106");
        System.out.println("rows的值为: "+rows);
    }

    @Test //测试乐观锁
    public void test(){
        User user = userDao.selectById("1230006285945135106");
        user.setName("lily更新");
        int rows = userDao.updateById(user);
        System.out.println("rows的值为: "+rows);
    }

    @Test//测试添加
    public void testInSert(){
        User user = new User();
        user.setName("lily");
        user.setGender(0);
        Integer rows = userDao.insert(user);
        System.out.println("rows的值为: "+rows);
    }

    @Test//测试查询
    public void testSelect() {
        Page page = new Page<>(1,5);
        IPage userIPage = userDao.selectPage(page, null);
        System.out.println("userIPage的值为"+":"+userIPage.toString());

    }

}

你可能感兴趣的:(mybatis plus 自动填充数据 & 乐观锁& 分页查询)