您好,登录后才能下订单哦!
本篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。