MyBatis-Plus自定义通用的方法实现
一、引言
大家已知MP给大家提供了很多通用的方法,可以看看MP源码中DefaultSqlInjector这个类,在这个集合当中包含了都是通用方法类,如果想要使用自定义通用方法,也需要添加到这个集合当中。
/**
* SQL 默认注入器
*
* @author hubin
* @since 2018-04-10
*/
public class DefaultSqlInjector extends AbstractSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
return Stream.of(
new Insert(),
new Delete(),
new DeleteByMap(),
new DeleteById(),
new DeleteBatchByIds(),
new Update(),
new UpdateById(),
new SelectById(),
new SelectBatchByIds(),
new SelectByMap(),
new SelectOne(),
new SelectCount(),
new SelectMaps(),
new SelectMapsPage(),
new SelectObjs(),
new SelectList(),
new SelectPage()
).collect(toList());
}
}二、自定义方法实现
步骤一:创建自定义方法的类,小编这个以删除为例。
/**
* @Auther: IT贱男
* @Date: 2019/9/23 16:14
* @Description: 通用删除全部方法
*/
public class DeleteAllMethod extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
// 自定义sql tableInfo.getTableName() 获取表名
String sql = "delete from " + tableInfo.getTableName();
// mapper 接口方法名
String method = "deleteAll";
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addDeleteMappedStatement(mapperClass, method, sqlSource);
}
}步骤二:创建注入器,并添加到集合当中。
/**
* @Auther: IT贱男
* @Date: 2019/9/23 16:22
* @Description: 将自定义方法的类添加到注入器
*/
@Component
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList() {
// 这里很重要,先要通过父类方法,获取到原有的集合,不然会自带的通用方法会失效的
List<AbstractMethod> methodList = super.getMethodList();
// 添加自定义方法类
methodList.add(new DeleteAllMethod());
return methodList;
}
}步骤三:在Mapper中加入自定义方法 ,如果同时有好几个Mapper需要用到这个自定义通用方法,这样设计可能比较合理。先创建一个自定义的MyMapper继承BaseMapper,其他的Mapper只需要继承MyMapper就可以使用自定义方法了。
/**
* @Auther: IT贱男
* @Date: 2019/9/23 16:35
* @Description: 自定义通用Mapper方法
*/
public interface MyMapper<T> extends BaseMapper<T> {
/**
* 自定义通用方法
* @return
*/
int deleteAll();
}
/**
* <p>
* 用户 Mapper 接口
* </p>
*
* @author IT贱男
* @since 2019-06-14
*/
public interface UserMapper extends MyMapper<User> {
/**
* 自定Wrapper修改
*
* @param userWrapper 条件构造器
* @param user 修改的对象参数
* @return
*/
@SqlParser(filter = true)
int updateByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper, @Param("user") User user);
}步骤四:最后测试,小编自定义方法就是删除表中所有的数据,当然只是以删除为例子,实际根据需求而定。
@Test
public void delete() {
int i = userMapper.deleteAll();
System.out.println(i);
}由于时间问题,小编只是大概的讲了一下在MP中,需要使用自定义SQL通用方法的几个实现步骤,以后有时间会来更新详细内容。
到此这篇关于MyBatis-Plus自定义通用的方法实现的文章就介绍到这了,更多相关MyBatis-Plus自定义通用 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
IntelliJ IDEA 2020.1.2激活工具下载及破解方法免费可用至2089年(强烈推荐)
这篇文章主要介绍了IntelliJ IDEA 2020.1.2激活工具下载及破解方法免费可用至2089年(强烈推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09
Java爬虫范例之使用Htmlunit爬取学校教务网课程表信息
htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源实现。今天我们用这款分析工具来爬取学校教务网课程表信息2021-11-11
java 通过发送json,post请求,返回json数据的方法
下面小编就为大家分享一篇java 通过发送json,post请求,返回json数据的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-03-03
spring boot启动时mybatis报循环依赖的错误(推荐)
今天小编抽时间给大家分享spring boot启动时mybatis报循环依赖的错误,非常不错,具有参考借鉴价值,需要的朋友参考下吧2017-12-12


最新评论