JAVA全系列 教程
3762个小节阅读:7091.9k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
MyBatisPlus方法是有限的,我们可以使用SQL注入器自定义全局方法,注入到全局中,这样所有的Mapper类都能使用该方法,接下来我们自定义一个deleteAll方法。
创建注入方法类,继承AbstractMethod
xxxxxxxxxx
// 注入方法类
public class DeleteAll extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
//定义sql语句
String sql = "delete from " + tableInfo.getTableName();;
//方法名
String method = "deleteAll";
//构建SqlSource,它负责将Sql传递到数据库
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration,sql,modelClass);
//构建删除方法
return this.addDeleteMappedStatement(mapperClass,method,sqlSource);
}
}
创建SQL自动注入器,继承AbstractSqlInjector
xxxxxxxxxx
@Component
public class MySqlInject extends AbstractSqlInjector {
// 注入自定义方法集合
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methods = new ArrayList<>();
methods.add(new DeleteAll());
return methods;
}
}
注销防止全表更新与删除插件
xxxxxxxxxx
// 注册插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
// interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
在Mapper接口中定义deleteAll方法
xxxxxxxxxx
public interface StudentMapper extends BaseMapper<Student> {
void deleteAll();
}
测试deleteAll方法
xxxxxxxxxx
@Test
public void testDeleteAll() {
studentMapper.deleteAll();
}
1. 在MyBatis中,使用SQL注入器功能,SQL自动注入器需要继承
A AbstractInjector
B AbstractSql
C AbstractSqlInjector
D SqlInjector
2. 在MyBatis中,使用SQL注入器功能,自定义方法类需要继承
A Method
B AbstractMethod
C MyMethod
D MethodHandler
1=>C 2=>B