曾经我们写的删除代码都是物理删除。
逻辑删除:删除转变为更新
update user set deleted=1 where id = 1 and deleted=0
查找: 追加 where 条件过滤掉已删除数据,如果使用 wrapper.entity 生成的 where 条件也会自动追加该字段
查找:
select id,name,deleted from user where deleted=0
CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`uemail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '邮箱',
`deleted` int DEFAULT '0' COMMENT '删除与否',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE user ADD column `deleted` int DEFAULT '0' COMMENT '删除与否';
配置com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig
mybatis-plus:
global-config:
db-config:
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
@TableLogic
实体类字段上加上@TableLogic
注解
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
@TableField(select = false)
private Integer age;
@TableField(value = "uemail")
private String email;
@TableLogic
private Integer deleted;
}
@Test
public void testLogicDeleted(){
int ret = userMapper.deleteById(3L);
System.out.println("删除受影响行数为:"+ret);
}
==> Preparing: UPDATE user SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 3(Long)
<== Updates: 1
@Test
public void testLogicDeletedSelect(){
QueryWrapper wrapper = new QueryWrapper<>();
List user = userMapper.selectList(wrapper);
System.out.println(user);
}
==> Preparing: SELECT id,name,uemail AS email,deleted FROM user WHERE deleted=0
==> Parameters:
<== Total: 3
开发中,比如
CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`uemail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '邮箱',
`deleted` int DEFAULT '0' COMMENT '删除与否',
`sex` int DEFAULT '1' COMMENT '性别',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
public enum FieldFill {
DEFAULT,//不处理
INSERT,//插入查出
UPDATE,//更新处理
INSERT_UPDATE;//插入和更新处理
}
@TableField(value = "create_time",fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "update_time",fill = FieldFill.UPDATE)
private Date updateTime;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
setFieldValByName("createTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
setFieldValByName("updateTime",new Date(),metaObject);
}
}
@Test
public void testFillInsert(){
User entity = new User();
entity.setEmail("erew");
entity.setAge(19);
entity.setName("小红");
entity.setSex(SexEnum.MAN);
int insert = userMapper.insert(entity);
System.out.println("受影响行数为:"+insert);
}
==> Preparing: INSERT INTO user ( name, age, uemail, sex, create_time ) VALUES ( ?, ?, ?, ?, ? )
==> Parameters: 小红(String), 19(Integer), erew(String), 1(Integer), 2022-12-03 11:34:41.169(Timestamp)
<== Updates: 1
@Test
public void testFillUpdate(){
User entity = new User();
entity.setId(15L);
entity.setEmail("erew");
entity.setAge(19);
entity.setName("小红");
entity.setSex(SexEnum.MAN);
int insert = userMapper.updateById(entity);
System.out.println("受影响行数为:"+insert);
}
==> Preparing: UPDATE user SET name=?, age=?, uemail=?, sex=?, update_time=? WHERE id=? AND deleted=0
==> Parameters: 小红(String), 19(Integer), erew(String), 1(Integer), 2022-12-03 11:37:10.716(Timestamp), 15(Long)
<== Updates: 1
MyBatisPlus 是使用 ISqlInjector 接口负责SQL注入工作
public void inspectInject(MapperBuilderAssistant builderAssistant, Class> mapperClass) {
Class> modelClass = ReflectionKit.getSuperClassGenericType(mapperClass, Mapper.class, 0);
if (modelClass != null) {
String className = mapperClass.toString();
Set mapperRegistryCache = GlobalConfigUtils.getMapperRegistryCache(builderAssistant.getConfiguration());
if (!mapperRegistryCache.contains(className)) {
TableInfo tableInfo = TableInfoHelper.initTableInfo(builderAssistant, modelClass);
List methodList = this.getMethodList(mapperClass, tableInfo);
if (CollectionUtils.isNotEmpty(methodList)) {
methodList.forEach((m) -> {
m.inject(builderAssistant, mapperClass, modelClass, tableInfo);
});
} else {
this.logger.debug(mapperClass.toString() + ", No effective injection method was found.");
}
mapperRegistryCache.add(className);
}
}
}
methodList.forEach((m) -> {
m.inject(builderAssistant, mapperClass, modelClass, tableInfo);
});
this.injectMappedStatement(mapperClass, modelClass, tableInfo);
public interface MyBaseMapper extends BaseMapper {
List listAll();
}
@Repository
public interface UserMapper extends MyBaseMapper {
}
@Component
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List getMethodList(Class> mapperClass, TableInfo tableInfo) {
//默认的mybatis-plus 的注入方法
List methodList = super.getMethodList(mapperClass, tableInfo);
//添加自己的 listAll 类
methodList.add(new ListAll());
return methodList;
}
}
public class ListAll extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
String sqlMethod = "listAll";
String sql = "select * from "+tableInfo.getTableName();
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass,sqlMethod,sqlSource,tableInfo);
}
}
@Test
public void testInjector(){
List users = userMapper.listAll();
System.out.println(users);
}
==> Preparing: select * from user
==> Parameters:
<== Total: 13
MybatisPlusInterceptor
该插件是核心插件,目前代理了 `Executor#query` 和 `Executor#update` 和`StatementHandler#prepare` 方法
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
})
@Component
public class MyInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("拦截前操作");
//做拦截操作
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
System.out.println("装配插件");
//装配插件
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
System.out.println("设置属性");
//属性设置
}
}
@Configuration
@MapperScan("cn.sycoder.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
当要更新一条记录的时候,希望这条记录没有被别人更新
乐观锁实现方式:
update user set age = 123123,version = version+1 where id = 16 and version = 0;
@Configuration
@MapperScan("cn.wjcoder.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
CREATE TABLE `user` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`uemail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '邮箱',
`deleted` int DEFAULT '0' COMMENT '删除与否',
`sex` int DEFAULT '1' COMMENT '性别',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`version` int DEFAULT NULL COMMENT '乐观锁控制',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
@Version
private Integer version;
@Test
public void testVersion(){
User user = userMapper.selectById(16L);
user.setName("云哥666");
int count = userMapper.updateById(user);
System.out.println("受影响行数:"+count);
}
==> Preparing: UPDATE user SET name=?, uemail=?, sex=?, create_time=?, update_time=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: 云哥666(String), [email protected](String), 1(Integer), 2022-12-03 11:34:41.0(Timestamp), 2022-12-03 17:44:05.425(Timestamp), 3(Integer), 16(Long), 2(Integer)
<== Updates: 1
针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
@Configuration
@MapperScan("cn.sycoder.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}
@Test
public void testDeleteAll(){
QueryWrapper wrapper = new QueryWrapper<>();
int count = userMapper.delete(wrapper);
System.out.println("受影响行数:"+count);
}
简化开发流程,提高开发效率,让程序写很少的代码就能实现开发
org.springframework.boot
spring-boot-starter-parent
2.7.5
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
mysql
mysql-connector-java
com.baomidou
mybatis-plus-boot-starter
3.5.2
com.baomidou
mybatis-plus-generator
3.5.2
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
org.freemarker
freemarker
2.3.31
io.springfox
springfox-swagger-ui
2.10.1
io.springfox
springfox-swagger2
2.10.1
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://localhost:3306/mybatis-plus", "root", "123456")
.globalConfig(builder -> {
builder.author("sy") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("F:\\03-Spring\\MyBatisPlus\\homework\\mybatis-plus-02\\src\\main\\java\\"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("cn") // 设置父包名
.moduleName("sycoder") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, "F:\\03-Spring\\MyBatisPlus\\homework\\mybatis-plus-02\\src\\main\\resources\\cn\\sycoder")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("user"); // 设置需要生成的表名
// .addTablePrefix("t_", "c_"); // 设置过滤表前缀
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}