您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關springboot中如何使用mybatis-plus表單更新null值,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
使用mybatis-plus進行數據庫交互,默認開啟null不更新設置,在新增數據后,編輯頁面將字段值清除后(date類型,int類型,為避免默認值傳入,model全部使用包裝類型初始化為null)無法將null值更新至數據庫
通過UpdateWrapper的set方法強制字段為null值
定義基礎類BaseModel,增加屬性updateFieldList,model繼承此類
public class BaseModel {@TableField(exist = false)@JSONField(serialize = false)private List<String> updateFieldList;public List<String> getUpdateFieldList() {return updateFieldList; }public void setUpdateFieldList(List<String> updateFieldList) {this.updateFieldList = updateFieldList; } }
自定自定義注解UpdateRequestBody替代RequestBody
@ResponseBody@PostMapping("/update") public Object update(@UpdateRequestBody AccountDO entity) { Result result = null; try { accountService.update(entity); result = Result.okResult(); } catch (Exception e) { result = Result.errorResult(); } return result; } @ResponseBody@PostMapping("/batchUpdate") public Object batchUpdate(@UpdateRequestBody List<AccountDO> entityList) { Result result = null; try { accountService.batchUpdate(entityList); result = Result.okResult(); } catch (Exception e) { result = Result.errorResult(); } return result; }
自定義HandlerMethodArgumentResolver對前臺json至后臺Model轉換的攔截,需要對List類型的Model集合進行支持,獲取前臺提交json對應定義Model中有同名屬性的,進行加入打待更新字段列表updateFieldList
public class BaseModelMethodArgumentResolver implements HandlerMethodArgumentResolver {@Overridepublic boolean supportsParameter(MethodParameter methodParameter) {if (methodParameter.hasParameterAnnotation(UpdateRequestBody.class)) {//集合if(List.class.isAssignableFrom(methodParameter.getParameterType())){ ParameterizedType parameterizedType = (ParameterizedType) methodParameter.getGenericParameterType(); Class clazz = (Class)parameterizedType.getActualTypeArguments()[0];//取出List中的真實對象類型if(BaseModel.class.isAssignableFrom(clazz)){return true; }else{return false; } }else{//單個對象if(BaseModel.class.isAssignableFrom(methodParameter.getParameterType())){return true; }else{return false; } } }return false; }@Overridepublic Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { String body = "";try { HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class); body = CommonUtil.getBodyString(request); } catch (Exception e) { e.printStackTrace(); }if(List.class.isAssignableFrom(methodParameter.getParameterType())){ List resultList = new ArrayList(); ParameterizedType parameterizedType = (ParameterizedType) methodParameter.getGenericParameterType(); Class clazz = (Class)parameterizedType.getActualTypeArguments()[0]; List<Field> fs = Arrays.asList(clazz.getDeclaredFields()); JSONArray array = JSONArray.parseArray(body);for (Object temp:array) { JSONObject obj = JSONObject.parseObject(temp.toString()); List<String> updateFieldList = new ArrayList<>();for (String key : obj.keySet()) {for (Field filed : fs) {if (key.toLowerCase().equals(filed.getName().toLowerCase())) { updateFieldList.add(key.toLowerCase());continue; } } } obj.put("updateFieldList",updateFieldList); resultList.add(obj); }return JSONArray.parseArray(resultList.toString(),clazz); }else{ Class clazz = methodParameter.getParameterType(); List<String> updateFieldList = new ArrayList<>(); List<Field> fs = Arrays.asList(clazz.getDeclaredFields()); JSONObject obj = JSON.parseObject(body);for (String key : obj.keySet()) {for (Field filed : fs) {if (key.toLowerCase().equals(filed.getName().toLowerCase())) { updateFieldList.add(key.toLowerCase());continue; } } } obj.put("updateFieldList",updateFieldList);return JSON.parseObject(obj.toString(),clazz); } } }
將自定義BaseModelMethodArgumentResolver 加入到配置中去
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { resolvers.add(new BaseModelMethodArgumentResolver()); } }
進行自定義UpdateWrapper構造
public <T extends BaseModel> UpdateWrapper getUpdateWrapper(T entity){ UpdateWrapper<T> updateWrapper = new UpdateWrapper<T>();try{ Class clazz = entity.getClass(); List<Field> fs = Arrays.asList(clazz.getDeclaredFields()); List<String> updateFieldList = entity.getUpdateFieldList(); if(updateFieldList!=null){ for (String updateFiled:updateFieldList ) { for (Field field:fs) { field.setAccessible(true); if(field.getName().toLowerCase().equals(updateFiled)){ Object fieldValue = field.get(entity); updateWrapper.set(fieldValue==null,field.getName(),null); continue; } } } } return updateWrapper; }catch (Exception e){ e.printStackTrace(); }return updateWrapper; }
注:對標記TableField忽略注解的字段可以優化,反射可以應用初始化時掃描加入緩存進行優化
以上就是springboot中如何使用mybatis-plus表單更新null值,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。