实现步骤:
- application.yml中进行配置:
mybatis-plus:
global-config:
db-config:
#逻辑未删除
logic-not-delete-value: 0
#逻辑已删除
logic-delete-value: 1
- 建立一个配置类包,在包里创建一个配置类MybatisPlusConfig
package com.mp.first.configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
// @Bean
// public PaginationInterceptor paginationInterceptor(){
// return new PaginationInterceptor();
// }
// 3.1.1以下版本需要此配置,以上版本不需要此配置
// @Bean
// public ISqlInjector sqlInjector(){
// return new LogicSqlInjector();
// }
}
- 在实体类上逻辑删除字段添加注解@TableLogic
// 逻辑删除标识(0.未删除,1.已删除)
// 下面注解只对当前实体有限,局部的,一般都采用全局策略设置在yml中,怕乱了
// @TableLogic(value = "逻辑未删除",delval = "逻辑已删除")
@TableLogic
private int deleted;
- 测试删除方法
package com.mp.first;
import com.mp.first.dao.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
@Autowired
private UserMapper userMapper;
// UPDATE user SET deleted=1 WHERE id=? AND deleted=0
@Test
public void deleteById() {
int rows = userMapper.deleteById(5);
System.out.println(rows);
}
}
- 测试查询方法
// SELECT id,name,age,email,manager_id,create_time,update_time,version,deleted FROM user WHERE deleted=0
@Test
public void select() {
List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);
}
- 测试修改方法
// UPDATE user SET age=?, version=? WHERE id=? AND deleted=0
@Test
public void updateById() {
User user = new User();
user.setAge(26);
user.setId(2L);
int rows = userMapper.updateById(user);
System.out.println("影响行数:"+rows);
}
优化
查询语句时,sql中不显示逻辑删除标识deleted字段
- 在字段上添加注解@TableField(select = false)
@TableLogic
// 查询时,查询sql语句中不显示此删除标识字段
@TableField(select = false)
private int deleted;
- 测试一下
// SELECT id,name,age,email,manager_id,create_time,update_time,version,deleted FROM user WHERE deleted=0
// SELECT id,name,age,email,manager_id,create_time,update_time,version FROM user WHERE deleted=0
@Test
public void select() {
List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);
}
自定义的语句不会自动添加逻辑删除限定
- 自定义一个方法
package com.mp.first.dao;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mp.first.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
//import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user ${ew.customSqlSegment}")
List<User> mySelectList(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
// @Select("select * from user ${ew.customSqlSegment}")
// List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
//
// IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER) Wrapper<User> wrapper);
}
- 测试一下,自定义的方法确实不会自动加上逻辑删除限定条件;那怎么办?
// select * from user WHERE (age > ?)
@Test
public void mySelect() {
List<User> list = userMapper.mySelectList(
Wrappers.<User>lambdaQuery().gt(User::getAge,25));
list.forEach(System.out::println);
}
针对上面自定义方法不会自动添加限定条件的情况,针对两种场景下的解决办法:
- 场景一是自己手动在代码的Wrapper中加上这个过滤条件
// select * from user WHERE (age > ?)
// select * from user WHERE (age > ? AND deleted = ?)
@Test
public void mySelect() {
List<User> list = userMapper.mySelectList(
Wrappers.<User>lambdaQuery().gt(User::getAge,25).eq(User::getDeleted,0));
list.forEach(System.out::println);
}
- 还有一种场景是,没有使用Wrapper的自定义sql语句,可以把限定条件写在sql语句中,这里要特别注意下。