MybatisPlus在配置了全局逻辑删除之后, 所有的RUD接口都会自动添加逻辑删除字段限制
该增强工具用于在已经配置了全局逻辑删除之后实现非逻辑删除操作
该增强工具遵循MybatisPlus的**, 只做增强, 不做改变
-
添加仓库
Gradle
repositories { maven { url 'https://www.jitpack.io' } ... }
Maven
<repositories> <repository> <id>jitpack.io</id> <url>https://www.jitpack.io</url> </repository> </repositories>
-
引入依赖
最新版为: 3.4.2-release
Gradle
dependencies { implementation 'com.violetfreesia:mybatisplusplus:latestVersion' }
Maven
<dependency> <groupId>com.violetfreesia</groupId> <artifactId>mybatisplusplus</artifactId> <version>latestVersion</version> </dependency>
-
配置注入器
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.violetfreesia.mybatisplusplus.IllogicalMethodInjector; /** * @author violetfreesia */ @Configuration public class MybatisPlusConfig { @Bean public AbstractSqlInjector illogicMethodInjector() { return new IllogicalMethodInjector(); } }
-
将原来继承
BaseMapper
的接口改为继承自IllogicalBaseMapper
//原来为 public interface UserMapper extends BaseMapper<User> public interface UserMapper extends IllogicalBaseMapper<User> { ... }
-
将原来继承
IService
的接口改为继承自IllogicalBaseService
//原来为 public interface UserService extends IService<User> public interface UserService extends IllogicalBaseService<User> { ... }
-
将原理继承
ServiceImpl
的Service实现类改为继承自IllogicalBaseServiceImpl
//原来为 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService public class UserServiceImpl extends IllogicalBaseServiceImpl<UserMapper, User> implements UserService { ... }
-
使用方法
调用增强工具提供的方法, 只需要在方法名后添加
Illogical
即可, 使用方法与原MybatPlus提供的方法相同 -
使用案例
/** * @author violetfreesia */ @RestController public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/user/{userId}") public User getById(@PathVariable String userId) { // 这里调用的方法就是该增强工具提供的方法 return userService.getByIdIllogical(userId); } }
上面演示的是在全局配置了逻辑删除的情况下, 调用增强工具提供的查询方法, 该方法是可以查询到已经逻辑删除的数据的(未被删除的肯定也是可以的)
-
增强工具提供的方法
该增强工具提供的所有方法名都是在原方法名后添加
Illogical
Sevice CRUD
参见MybatisPlus官网的
Remove
,Update
,Get
,List
,Page
,Count
的所有方法Mapper CRUD
参见MybatisPlus官网的
Delete
,Update
,Select
部分的所有方法