zoukankan      html  css  js  c++  java
  • MybatisPlus的sql注入器使用!


    本文代码样例均已上传至Gitee:https://gitee.com/tqbx/springboot-samples-learn

    基本使用

    1. 创建方法的类,继承AbstractMethod。
    /**
     *
     * 删除全部
     * @author Summerday
     */
    public class DeleteAll extends AbstractMethod {
        @Override
        public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    
            // 执行sql,动态sql参考类SqlMethod
            String sql = "delete from " + tableInfo.getTableName();
            // mapper方法接口名一致
            String method = "deleteAll";
    
            SqlSource sqlSource = 
                languageDriver.createSqlSource(configuration, sql, modelClass);
            return this.addDeleteMappedStatement(mapperClass,method,sqlSource);
        }
    }
    
    1. 创建注入器,既可以继承DefaultSqlInjector,也可以实现ISqlInjector接口。
    /**
     * 自定义sql注入
     * @author Summerday
     */
    @Component
    public class MySqlInjector extends DefaultSqlInjector {
    
        @Override
        public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    
            List<AbstractMethod> methodList = super.getMethodList(mapperClass);
            //增加自定义方法
            methodList.add(new DeleteAll());
            methodList.add(new FindOne());
    
            /*
             * 以下 3 个为内置选装件
             * 头 2 个支持字段筛选函数
             */
            // 例: 不要指定了 update 填充的字段
            methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
            methodList.add(new AlwaysUpdateSomeColumnById());
            methodList.add(new LogicDeleteByIdWithFill());
            return methodList;
        }
    }
    
    1. 如果想要所有的mapper都拥有自定义的方法,可以自定义接口继承BaseMapper接口,我们的业务接口就可以继承自定义的baseMapper接口了。
    /**
     * 自定义baseMapper接口
     * @author Summerday
     */
    public interface MyBaseMapper<T> extends BaseMapper<T> {
    
        //自定义方法,删除所有,返回影响行数
        int deleteAll();
    
        // 根据id找到
        T findOne(Serializable id);
    }
    
    @Mapper
    public interface UserMapper extends MyBaseMapper<User> {
    }
    
    1. 测试接口
    @RunWith(SpringRunner.class)
    @SpringBootTest
    class InjectTest {
    
        @Resource
        UserMapper userMapper;
    
        @Test
        void inject(){
            int rows = userMapper.deleteAll();
            System.out.println(rows);
        }
    
        @Test
        void findOne(){
            User one = userMapper.findOne(1L);
            System.out.println(one);
        }
    }
    

    选装件

    1. InsertBatchSomeColumn:批量新增数据,自选字段insert

    2. LogicDeleteByIdWithFill:根据id逻辑删除数据,并带字段填充功能

    3. AlwaysUpdateSomeColumnById:根据 ID 更新固定的那几个字段(但是不包含逻辑删除)

            /*
             * 以下 3 个为内置选装件
             * 头 2 个支持字段筛选函数
             */
            // 例: 不要指定了 update 填充的字段  排除逻辑删除,和"age"字段
            methodList.add(new InsertBatchSomeColumn(
                i -> i.getFieldFill() != FieldFill.UPDATE 
                && !i.isLogicDelete() 
                && !"age".equals(i.getColumn())));
            // 筛选字段
            methodList.add(new AlwaysUpdateSomeColumnById(
                i -> !"age".equals(i.getColumn())));
            methodList.add(new LogicDeleteByIdWithFill());
    
  • 相关阅读:
    方向ajax(http long request实现实时通信)
    HTTP防盗链与反防盗链
    linux开启过程详解
    自动化运维工具----saltstack安装及配置
    python----网络编程(TCP通讯)
    windows----bat方式实现ftp推送
    shell----ftp推送
    python----FTP文件拉取(new)
    haproxy----四层负载均衡
    python----时间转换
  • 原文地址:https://www.cnblogs.com/summerday152/p/13878050.html
Copyright © 2011-2022 走看看