MybatisPlus 之 Sql 注入器
我们已经知道,在MP中,通过AbstractSqlInjector将BaseMapper中的方法注入到了Mybatis容器,这样这些方法才可以正常执行。
那么,如果我们需要扩充BaseMapper中的方法,有该如何实现呢?
下面我们以扩展findAll方法为例进行学习。
1、编写MyBaseMapper
/*通用mapper接口,以后创建其他mapper接口时,不再继承BaseMapper,而是继承MyBaseMapper*/
public interface MyBaseMapper<T> extends BaseMapper<T> {/*查询所有用户*/public List<User> findAll();
}
其他的Mapper都可以继承该Mapper,这样实现了统一的扩展。
如:
public interface UserMapper extends MyBaseMapper<User> {/*自定义findById方法*/public User findById(Long id);
}
2、编写MySqlInjector
如果直接继承AbstractSqlInjector的话,原有的BaseMapper中的方法将失效,所以我们选择继承DefaultSqlInjector进行扩展。
/*自定义sql注入器*/
public class MySqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList() {List<AbstractMethod> methodList = super.getMethodList();// 扩充自定义方法methodList.add(new FindAll());return methodList;}
}
3、编写FindAll
public class FindAll extends AbstractMethod {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {String sql = "select * from " + tableInfo.getTableName();SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);return this.addSelectMappedStatement(mapperClass, "findAll", sqlSource, modelClass, tableInfo);}
}
4、注册到Spring容器
/*自定义的sql注入器*/@Beanpublic MySqlInjector mySqlInjector(){return new MySqlInjector();}
5、测试
@Test
public void testFindAll(){List<User> users = this.userMapper.findAll();for (User user : users) {System.out.println(user);}
}
输出的SQL:
[main] [com.lagou.mp.mapper.UserMapper.findAll]-[DEBUG] ==> Preparing: select
* from tb_user
[main] [com.lagou.mp.mapper.UserMapper.findAll]-[DEBUG] ==> Parameters:
[main] [com.lagou.mp.mapper.UserMapper.findAll]-[DEBUG] <== Total: 10
至此,我们实现了全局扩展SQL注入器。
MybatisPlus 之 Sql 注入器相关推荐
- 记一次mybatis-plus自定义sql注入器不起作用的问题
之前自己写过一个关于mybatis-plus多表联查的组件,原理是用到了mybatis-plus扩展的口(sql注入),即继承DefaultSqlInjector /** @author chenga ...
- mybatis-plus的sql注入器实现自定义全局sql操作原理解析
自定义自己的通用方法可以实现接口ISqlInjector,也可以继承抽象类 AbstractSqlInjector.注入通用方法 SQL 语句,然后继承 BaseMappe添加自定义方法,全局配置sq ...
- MyBatis-Plus的sql注入器
一:创建sql注入方法类,这个类需要继承AbstractMethod类,重写里面的injectMappedStatement方法.在这个方法中,完成四个步骤: 1.定义sql语句 2.定义方法名 3. ...
- MyBatis-Plus - 一篇带你解决自定义 SQL 注入器失效必杀技
问题分析 Invalid bound statement (not found) 如果你看到这一篇,说明你也是遇到这个问题的人(废话),我们在上一篇(MyBatis-Plus - 一篇带你玩转自定义 ...
- mybatisPlus 自定义sqlSessionFactory sql注入器失效 Invalid bound statement (not found): insertBatchSomeColumn
这是重写后的sqlsessionFactory @Bean("sqlSessionFactory")public SqlSessionFactory sqlSessionFacto ...
- 第 3 章 MybatisPlus 注入 SQL 原理分析
第 3 章 MybatisPlus 注入 SQL 原理分析 思考问题 我们编写的 UserMapper 继承了 BaseMapper<T>,就拥有了基本的增删改查功能,这是因为 BaseM ...
- Mybatis Plus 自定义SqlInjector sql注入器
1.自定义sql注入器GeneralMybatisPlusSqlInjector package com.javasgj.springboot.mybatisplus.config;import ja ...
- mybatis plus SQL注入器 及 InsertBatchSomeColumn LogicDeleteByIdWithFill AlwaysUpdateSomeColumnById装载器
github地址https://github.com/heng1234/mybatis_plus 基于https://blog.csdn.net/qq_39313596/article/details ...
- MybatisPlus:SQL语句打印、SQL分析、自定义主键值策略填充(IdType.INPUT)、动态表名、多租户、枚举、类型处理器、连表自定义SQL(使用wrapper)
文章目录 1. 简单使用以及配置 - 带分页配置 2. 用法 2.0 Wrapper属性 2.1 @TableId - 自定义主键生成策略 2.2 @TableField - 自定义字段值填充 2.3 ...
最新文章
- BTree,B-Tree,B+Tree,B*Tree
- MySQL:硬盘在24 * 7工作中罢工了,我该怎么办?
- mongodb 导出 带条件_将 MongoDB 导出成 csv
- mysql aggregate_SQL语句之Aggregate函数
- html4基础,HTML 基础 4
- npz文件转为npy_Numpy_快速操作数组 4.4 数组的文件输入输出
- windows2003管理组创建
- 《大数据之路:阿里巴巴大数据实践》-第1篇 数据技术篇 -第3章数据同步
- python课本图片_python爬虫当当网python书籍图片
- Android官方BottomNavigationView添加Badge(角标),全部tab显示文字自动铺满
- 数据挖掘学习:站在巨人的肩膀上
- 电动汽车的新问题,保险公司赔不起,车主养不起
- 在VisualBasic6.0中实现0.5数值修约
- 计算机网络高级教程.pdf,网络技术-计算机网络(高级教程).pdf
- 基于多传感器的AUV控制系统
- E575: viminfo: Illegal starting char in line: hello=Hello\ \!\How\ are\ you\ \?
- QT中获取选中的radioButton的两种方法
- 安全多方计算之BGW算法
- element ui 表格内容 合计
- scipy.special.expit