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

8 篇文章 4 订阅
7 篇文章 4 订阅

系列文章目录

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



前言

前面讲了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
 */
@Configuration
public 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>{

}

项目中调用

在这里插入图片描述
结果
在这里插入图片描述
测试成功,只要实现新定义这个接口就拥有了通过userId查询的方法

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值