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

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

作者: 刘小刀tina | 来源:发表于2020-02-19 17:46 被阅读0次

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

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<User> page = new Page<>(1,5);
        IPage<User> userIPage = userDao.selectPage(page, null);
        System.out.println("userIPage的值为"+":"+userIPage.toString());

    }

}

相关文章

网友评论

      本文标题:mybatis plus 自动填充数据 & 乐观锁& 分页查询

      本文链接:https://www.haomeiwen.com/subject/shaafhtx.html