开发者

MyBatis-Plus自定义通用的方法实现

开发者 https://www.devze.com 2023-05-11 10:34 出处:网络 作者: IT贱男
目录一、引言二、自定义方法实现一、引言 大家已知MP给大家提供了很多通用的方法,可以看看MP源码中DefaultSqlInjector这个类,在这个集合当中包含了都是通用方法类,如果想要使用自定义通用方法,也需要添加到这个集
目录
  • 一、引言
  • 二、自定义方法实现

一、引言

大家已知MP给大家提供了很多通用的方法,可以看看MP源码中DefaultSqlInjector这个类,在这个集合当中包含了都是通用方法类,如果想要使用自定义通用方法,也需要添加到这个集合当中。

/**
 * SQL 默认注入器
 *
 * @author hubin
 * @since 2018-04-10
 */
public class DefaultSqlInjector extends AbstractSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList() {
        return Stream.of(
            new Insert(),
            new Delete()python,
            new DeleteByMap(),
            new DeleteById(),
            new DeleteBATchByIds(),
            new Update(),
   开发者_Python         new UpdateById(),
            new SelectById(),
            new SelectBatchByIds(),
            new SelectByMap(),
            new SelectOne(),
            new SelectCount(),
            new SelectMaps(),
            new SelectMapsPage(),
            new SelectObjs(),
            new SelectList(),
            new SelectPage()
        ).collect(toList());
    }
}

二、自定义方法实现

步骤一:创建自定义方法的类,小编这个以删除为例。

/**
 * @Auther: IT贱男
 * @Date: 2019/9/23 16:14
 * @Description: 通用删除全部方法
 */
public class DeleteAllMethod extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        // 自定义sql tableInfo.getTableName() 获取表名
        String sql = "delete from " + tableInfo.getTableName();
        // mapper 接口方法名
        String method = "deleteAll";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        randroideturn addDeleteMappedStatement(mapperClass, method, sqlSource);
    }
}

步骤二:创建注入器,并添加到集合当中。

/**
 * @Auther: IT贱男
 * @Date: 2019/9/23 16:22
 * @Description: 将自定义方法的类添加到注入器
 */
@Component
public class mysqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList() {
        // 这里很重要,先要通过父类方法,获取到原有的集合,不然会自带的通用方法会失效的
        List<AbstractMethod> methodList = super.getMethodList();
        // 添加自定义方法类
        methodList.add(new DeleteAllMethod());
        return methodList;
    }
}

步骤三:在Mapper中加入自定义方法 ,如果同时有好几个Mapper需要用到这个自定义通用方法,这样设计可能比较合理。先创建一个自定义的MyMapper继承BaseMapper,其他的Mapper只需要继承MyMapper就可以使用自定义方法了。

/**
 * @Auther: IT贱男
 * js@Date: 2019/9/23 16:35
 * @Description: 自定义通用Mapper方法
 */
public interface MyMapper<T> extends BaseMapper<T> {
    /**
     * 自定义通用方法
     * @return
     */
    int deleteAll();
}
/**
 * <p>
 * 用户 Mapper 接口
 * </p>
 *
 * @author IT贱男
 * @since 201http://www.devze.com9-06-14
 */
public interface UserMapper extends MyMapper<User> {
    /**
     * 自定Wrapper修改
     *
     * @param userWrapper 条件构造器
     * @param user        修改的对象参数
     * @return
     */
    @SqlParser(filter = true)
    int updateByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper, @Param("user") User user);
}

步骤四:最后测试,小编自定义方法就是删除表中所有的数据,当然只是以删除为例子,实际根据需求而定。

   @Test
    public void delete() {
        int i = userMapper.deleteAll();
        System.out.println(i);
    }

由于时间问题,小编只是大概的讲了一下在MP中,需要使用自定义SQL通用方法的几个实现步骤,以后有时间会来更新详细内容。

到此这篇关于MyBatis-Plus自定义通用的方法实现的文章就介绍到这了,更多相关MyBatis-Plus自定义通用 内容请搜索我们以前的文章或继续javascript浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号