1 寫一個類繼續 AbstractLogicMethodhtml
public class LogicDeleteByWrapper extends AbstractLogicMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { String sql; MySqlMethod sqlMethod = MySqlMethod.LOGIC_DELETE_BY_WRAPPER; sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(), sqlWhereEntityWrapper(tableInfo)); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource); } public String sqlLogicSet(){ String sql ="<trim prefix=\"SET\" suffixOverrides=\",\">\n" + "<if test=\"et.updateBy != null\">update_by=#{et.updateBy},</if>\n" + "<if test=\"et.updateTime != null\">update_time=#{et.updateTime},</if>\n" + "is_deleted = 1\n" + "</trim> "; return sql; }}複製代碼
寫一個類繼承baseMappersql
/** * 邏輯刪除 * @param entity * @param updateWrapper * @return */ int logicDeleteByWrapper(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);複製代碼
寫一個枚舉類bash
public enum MySqlMethod { /** * 根據參數邏輯刪除 */ LOGIC_DELETE_BY_WRAPPER("logicDeleteByWrapper", "根據ID 修改數據", "<script>\nUPDATE %s %s %s\n</script>"), /** * 根據id邏輯刪除 */ LOGIC_DELETE_BY_ID("logicDeleteById", "根據ID 修改數據", "<script>\nUPDATE %s %s WHERE %s=#{%s}\n</script>"); private final String method; private final String desc; private final String sql; MySqlMethod(String method, String desc, String sql) { this.method = method; this.desc = desc; this.sql = sql; } public String getMethod() { return method; } public String getDesc() { return desc; } public String getSql() { return sql; }}複製代碼
最後一步就是注入到beanapp
public class MyLogicSqlInjector extends AbstractSqlInjector { @Override public List<AbstractMethod> getMethodList() { return Stream.of( new LogicDeleteByWrapper() ).collect(Collectors.toList()); } @Override public void injectSqlRunner(Configuration configuration) { new SqlRunnerInjector().inject(configuration); }}複製代碼
@Configuration@MapperScan("com.xxx.xxx.*.dao")public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } @Bean public ISqlInjector sqlInjector() { return new MyLogicSqlInjector(); } /** * SQL執行效率插件 */ @Bean @Profile({"dev","test"})// 設置 dev test 環境開啓 public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); }}複製代碼