SSM整合MyBatis-Plus3.x之sql注入器使用方法,就是拓展BaseMapper

1.首先定义继承BaseMapper的接口,在里面自定义一个自己需要的方法

public interface MyBaseMapper extends BaseMapper {
    Integer deleteAll();
}

2.创建定义方法的类

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);
    }
}

 

3.创建注入器

public class MySqlInjector extends DefaultSqlInjector {

    /**
     * 如果只需增加方法,保留MP自带方法
     * 可以super.getMethodList() 再add
     */
    @Override
    public List getMethodList(Class mapperClass) {
        List methodList = super.getMethodList(mapperClass);
        methodList.add(new DeleteAll());
        return methodList;
    }

}

4.配置sql注入器


        

5.测试

public class TestMP {

    private ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    private EmployeeMapper employeeMapper = context.getBean("employeeMapper", EmployeeMapper.class);

    @Test
    public void testExpandMapperSql(){
        employeeMapper.deleteAll();
    }
}

结果:表数据被删除

你可能感兴趣的:(JAVA)