> 文档中心 > 【mybatis plus源码解析】(三)自定义SQL注入器,教你如何自定义扩展BaseMapper接口方法,实现更多查询

【mybatis plus源码解析】(三)自定义SQL注入器,教你如何自定义扩展BaseMapper接口方法,实现更多查询


系列文章目录

【mybatis plus源码解析】(一)mybatis plus执行原理
【mybatis plus源码解析】(二)详解自定义SQL注入器底层原理
【mybatis plus源码解析】(三)自定义SQL注入器,如何自定义扩展BaseMapper方法,实现更多查询


文章目录

  • 系列文章目录
  • 前言
  • 一、创建测试环境,照着官网整就完事了,这里就不说了
  • 二、自定义新方法SelectByUserId,可以实现通过传入userId查询
  • 自定义基础Mapper接口
  • 自定义扩展MySqlInjector sql注入器
  • 配置使用新的sql注入器
  • 这里就配置完成了,来看看使用
    • Mapper类实现我们新的CustomizeMapper接口
    • 项目中调用

前言

前面讲了sql注入器的原理,现在我们尝试自定义扩展一些方法

一、创建测试环境,照着官网整就完事了,这里就不说了

二、自定义新方法SelectByUserId,可以实现通过传入userId查询

注意,这里限制只有实体类有名为userId属性才能注入这个方法,如不满足后续调用会报错

/ * @author resthx * @date 16:19 2022/3/30 */public class SelectByUserId extends AbstractMethod {    public SelectByUserId() { super("selectByUserId");    }    /     * @param name 方法名     * @since 3.5.0     */    public SelectByUserId(String name) { super(name);    }    @Override    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { String sql = "SELECT %s FROM %s WHERE %s=#{%s} %s"; List<TableFieldInfo> fieldList = tableInfo.getFieldList(); boolean isAdd = false; for (TableFieldInfo fieldInfo : fieldList) {     String property = fieldInfo.getProperty();     String name = fieldInfo.getField().getName();     if ("userId".equals(property)){  String column = fieldInfo.getColumn();  SqlSource sqlSource = new RawSqlSource(configuration, String.format(sql,   sqlSelectColumns(tableInfo, false),   tableInfo.getTableName(), column, property,   tableInfo.getLogicDeleteSql(true, true)), Object.class);  return this.addSelectMappedStatementForTable(mapperClass, methodName, sqlSource, tableInfo);     } } return null;    }}

自定义基础Mapper接口类

/ * @author resthx * @date 15:25 2022/3/30 *  自定义扩展基础mapper,附加可以通过userId查询的方式 */public interface CustomizeMapper<T> extends BaseMapper<T>{    /     * 通过用户id查找     * @param userId     * @return     */    List<T> selectByUserId(@Param("userId")Long userId);}

自定义扩展MySqlInjector sql注入器

/ * @author resthx * @date 16:16 2022/3/30 * 自定义扩展sql、注入器 */public class MySqlInjector extends DefaultSqlInjector {    @Override    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) { List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo); //增加自定义方法 methodList.add(new SelectByUserId()); return methodList;    }}

配置使用新的sql注入器

/ * @author jiliugang * @date 16:42 2022/3/30 */@Configurationpublic class MybatisPlusConfig {    @Bean    public MySqlInjector sqlInjector() { return new MySqlInjector();    }}

这里就配置完成了,来看看使用

Mapper类实现我们新的CustomizeMapper接口

/* @author resthx* @description 针对表【ry_order】的数据库操作Mapper* @createDate 2022-03-30 16:13:55* @Entity com.example.mybatisplussqlinjector.domain.RyOrder*/public interface RyOrderMapper extends CustomizeMapper<RyOrder>{}

项目中调用

【mybatis plus源码解析】(三)自定义SQL注入器,教你如何自定义扩展BaseMapper接口方法,实现更多查询
结果
【mybatis plus源码解析】(三)自定义SQL注入器,教你如何自定义扩展BaseMapper接口方法,实现更多查询
测试成功,只要实现新定义这个接口就拥有了通过userId查询的方法