使用分页所需依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.1</version>
</dependency>
配置详情
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@MapperScan(basePackages = "cn.mbz.carsale.mapper")
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
@Primary
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页支持(添加了一个分页插件)
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//乐观锁支持的插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
使用自动填充所需依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.4.1</version>
</dependency>
配置详情
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//如果插入数据的时候遇到 createTime 则填充当前时间
this.setFieldValByName("createTime", new Date(), metaObject);
//如果插入数据的时候遇到 updateTime 则填充当前时间
this.setFieldValByName("updateTime", new Date(), metaObject);
//如果插入数据的时候遇到 deleted 则填充1
this.setFieldValByName("deleted", 1, metaObject);
//如果插入数据的时候遇到 status 则填充1
this.setFieldValByName("status", "1", metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
//如果更新数据的时候遇到 updateTime 则填充当前时间
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
例如,如下:实体类配置
/**
* 逻辑删除
* 0表示删除 1表示正常
*/
@TableLogic(value = "1", delval = "0")
//自动填充
@TableField(fill = FieldFill.INSERT)
private Integer deleted;