mybatisPlus3.x自动注入sql方法

一、首先在Mapper接口中定义好你需要自定义的方法 ,返回值记得用Integer代替int类型

public interface EmployeeMapper extends BaseMapper<Employee> {
        Integer deleteById(Integer id);
}

二、在工具类中新建一个class类,命名为 LogicSqlInjector,然后继承AbstractSqlInjector类,并且实现其方法,并把上面定义的方法加进去,如下:

public class LogicSqlInjector extends AbstractSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList() {
        List<AbstractMethod> list = new ArrayList<>();
        //获取父类的所有方法
        list.addAll(super.getMethodList());
        //将自己的方法注入进去
        list.add(new deleteById());
        return list;
    }
}

三、在工具类中新建一个class类,命名为刚才的方法名,deleteById,继承AbstractLogicMethod类,并实现其方法,如下

public class deleteById extends AbstractLogicMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, 
                         Class<?> modelClass, TableInfo tableInfo) {

        String sql = "DELETE FROM "+ tableInfo.getTableName()+" where id = #{id} " ;
        String method = "deleteById";
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addSelectMappedStatement(mapperClass,method, sqlSource, Map.class, tableInfo);
    }
}

备注:可以参考第二点中其他Logic方法的实现过程,在这个自定义类中可以直接套用其他方法的模板。

此代码块中的String sql为自己定义的sql语句, String method为定义的方法名,其他都是父类中自带的参数,只要把sql和method写进来就行了。

tableInfo中含有当前操作表的任何信息,这是mp对于数据库字段的映射,比如在employee表中操作,此时tableInfo.getTableName()获取到的值就是employee,具体的源码可以按住ctrl点进去看。

四、去ApplicationContext.xml配置文件中,将自定义的类注入进全局变量

  <!-- ****************** 定义mybatisplus全局配置 ****************** -->
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <!-- 注入自定义全局sql注入操作 -->
        <property name="sqlInjector" ref="logicSqlInjector"/>
</bean>
  
    <!--自定义注入器-->
    <bean id="logicSqlInjector" class="com.atguigu.mp.utils.mybatisplus.injector.LogicSqlInjector"/>
   

class的路径是刚才在工程下创建的LogicSqlInjector的地址

 

五、将mybatisplus全局配置注入进sqlSessionFactory

<!-- ****************** 定义mybatisplus全局配置 ****************** -->
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <!-- 注入自定义全局sql注入操作 -->
        <property name="sqlInjector" ref="logicSqlInjector"/>
    </bean>

    <!-- ****************** 需要放进全局配置中的组件 ****************** -->
    <!--自定义注入器-->
    <bean id="logicSqlInjector" class="com.atguigu.mp.utils.mybatisplus.injector.LogicSqlInjector"/>
   

六、测试

@Test
    public void testdelall(){
        Integer result =  employeeMapper.deleteById(3);
        System.out.println("result = " + result);
    }

七、结果

这只是快速上手的方法,具体源码小伙伴们自己看

或者参考这位大神的文章https://www.cnblogs.com/liuyangfirst/p/9744011.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值