mybatisPlus自定义sql注入器

1.需要继承BaseMapper并定义自己想要的公共方法

public interface MyBaseMapper extends BaseMapper {
    List findAll();
}

2.定义自定义方法类

public class FindAll extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
        String sql = "select * from " + tableInfo.getTableName();
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, mapperClass);
        return this.addSelectMappedStatementForOther(mapperClass, "findAll", sqlSource, modelClass);
    }
}

3.自定义sqlInjector,需继承AbstractSqlInjector类,扩充前需要把默认的方法全部取出来,否则会报参数绑定失败

public class MySqlInjector extends AbstractSqlInjector {
    @Override
    public List getMethodList(Class mapperClass) {
        //扩充自定义方法,
        List methods=new DefaultSqlInjector().getMethodList(mapperClass);
        methods.add(new FindAll());
        return methods;
    }
}

4.将MySqlInjector注入到配置中

@EnableTransactionManagement
@Configuration
public class MybatisConfiguration {

    @Bean
    public MySqlInjector mySqlInjector() {
        return new MySqlInjector();
    }
}

配置完成,然后用Mapper继承MyBaseMapper,直接调用定义的方法

你可能感兴趣的:(java,mysql,java)