展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

自动填充功能

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.18</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.21.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
  • 自动填充处理器
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 执行插入操作时,如果operator字段为null,则自动填充Jetty
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, "operator", String.class, "Jetty");
    }

    /**
     * 执行修改操作时,如果operator字段为null,则自动填充Tom
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "operator", String.class, "Tom");
    }
}
  • 实体类
@Data
@TableName(value = "user")
@NoArgsConstructor
@AllArgsConstructor
public class User {
    
    private Long id;
    
    private String name;
    
    private Integer age;
    
    private String email;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String operator;
}
  • 测试
@Slf4j
@SpringBootTest
public class AutoFillTest {

    @Resource
    private UserMapper userMapper;

    @Test
    public void test() {
        /**
         * 插入1条数据
         * INSERT INTO user ( id, name, age, email, operator ) VALUES ( ?, ?, ?, ?, ? )
         * 1549587446338551810(Long), Tom(String), 1(Integer), tom@qq.com(String), Jetty(String)
         */
        User user = new User(null, "Tom", 1, "tom@qq.com", null);
        userMapper.insert(user);
        log.info("query user:{}", userMapper.selectById(user.getId()));
        /**
         * 查看
         * SELECT id,name,age,email,operator FROM user WHERE id=?
         * 1(Long)
         * User(id=1, name=Jone, age=18, email=test1@baomidou.com, operator=test)
         */
        User beforeUser = userMapper.selectById(1L);
        log.info("before user:{}", beforeUser);
        /**
         * 修改
         * UPDATE user SET name=?, age=?, email=?, operator=? WHERE id=?
         * Jone(String), 12(Integer), test1@baomidou.com(String), test(String), 1(Long)
         */
        beforeUser.setAge(12);
        userMapper.updateById(beforeUser);
        log.info("query user:{}", userMapper.selectById(1L));
    }
}
posted @ 2022-07-20 11:01  DogLeftover  阅读(16)  评论(0编辑  收藏  举报