mybtis plus 3.2.0 SQL注入器的配置

一.创建定义方法的类

public class MyMethod extends AbstractMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { //自定义sql String sql = "delete from "+tableInfo.getTableName(); //mapper接口的方法名 String method = "deleteAllData"; SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return addDeleteMappedStatement(mapperClass,method,sqlSource); }}

二.自定义SQL注入器

@Componentpublic class MyInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new MyMethod()); return methodList; }}

三.在你的Mapper类中添加刚才定义的method

@Repositorypublic interface UserMapper extends BaseMapper<User> { int deleteAllData();}

四.测试

@Test public void testInject(){ userMapper.deleteAllData(); }

运行结果:

 

相关文章