使用 sql注入器自定義通用方法。sql
1、建立自定義方法的類,並繼承AbstractMethodapp
/** * @author beth * @data 2019-10-23 20:39 */ public class DeleteAllMethod extends AbstractMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { // 執行的sql String sql = " delete from " + tableInfo.getTableName(); // mapper 接口方法名 String method = "deleteAll"; SqlSource sqlSource = languageDriver.createSqlSource(configuration,sql,modelClass); return addDeleteMappedStatement(mapperClass,method,sqlSource); } }
2、建立注入器ide
/** * @author beth * @data 2019-10-23 20:47 */ @Component public class MySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new DeleteAllMethod()); return methodList; } }
3、調用自定義方法測試
/** * <p> * 用戶表 Mapper 接口 * </p> * * @author beth<></> * @since 2019-10-13 */ @Repository public interface UserInfoMapper extends BaseMapper<UserInfo> { /** * 刪除全部 * @return */ int deleteAll(); }
4、測試code
/** * 查詢動態表數據 */ @Test public void deleteAll () { int rows = userInfoMapper.deleteAll(); System.out.println("影響行數"+rows); }
5、通用自定義方法繼承
/** * @author beth * @data 2019-10-23 23:09 */ public interface MyMapper<T> extends BaseMapper<T> { /** * 刪除全部 * @return */ int deleteAll(); /** * 批量新增 * @param list * @return */ int insertBatchSomeColumn(List<T> list); /** * 根據條件刪除 * @param entity * @return */ int deleteByWithFill(T entity); /** * 固定某些字段必定會修改 * @param entity * @return */ int AlwaysUpdateSomeColumnById(@Param(Constants.ENTITY) T entity); }
6、繼承自定義通用mapper接口
@Repository public interface UserInfoMapper extends MyMapper<UserInfo> {
7、在注入器中加入自定義方法get
/** * @author beth * @data 2019-10-23 20:47 */ @Component public class MySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new DeleteAllMethod()); //Mybatis Plus自定義方法,即選裝件 methodList.add(new InsertBatchSomeColumn(t->t.isLogicDelete())); methodList.add(new LogicDeleteByIdWithFill()); methodList.add(new AlwaysUpdateSomeColumnById(t->!t.getColumn().equals("name"))); return methodList; } }
8、測試it
/** * @author beth * @data 2019-10-23 20:56 */ @RunWith(SpringRunner.class) @SpringBootTest public class SqlInjectorTest { @Autowired private UserInfoMapper userInfoMapper; /** * 查詢動態表數據 */ @Test public void deleteAll () { int rows = userInfoMapper.deleteAll(); System.out.println("影響行數"+rows); } /** * 批量新增 */ @Test public void insertBatch() { UserInfo userInfo1 = new UserInfo(); userInfo1.setUsername("張三"); userInfo1.setAge(33); userInfo1.setParentId("543533453"); UserInfo userInfo2 = new UserInfo(); userInfo2.setUsername("李四"); userInfo2.setAge(22); userInfo2.setParentId("543533453"); List<UserInfo> asList = Arrays.asList(userInfo1,userInfo2); int rows = userInfoMapper.insertBatchSomeColumn(asList); System.out.println("影響行數"+rows); } /** * 根據條件刪除 */ @Test public void deleteByWithFill () { UserInfo userInfo1 = new UserInfo(); userInfo1.setId("1312312424"); userInfo1.setAge(33); int rows = userInfoMapper.deleteByWithFill(userInfo1); System.out.println("影響行數"+rows); } /** * 根據條件刪除 */ @Test public void AlwaysUpdateSomeColumnById () { UserInfo userInfo1 = new UserInfo(); userInfo1.setId("1312312424"); userInfo1.setAge(33); int rows = userInfoMapper.AlwaysUpdateSomeColumnById(userInfo1); System.out.println("影響行數"+rows); } } ```