From 278740af85740a0bb35ea7f4d56ea304e7abe7c2 Mon Sep 17 00:00:00 2001 From: godchao Date: Fri, 19 Jul 2019 18:01:03 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=8E=A5=E5=8F=A3=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msg/controller/MessageController.java | 2 +- .../example/controller/SysUserController.java | 72 +++++-------------- 2 files changed, 19 insertions(+), 55 deletions(-) diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java index 471f03a..e11e8ab 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diboot.components.msg.entity.Message; import com.diboot.components.msg.service.MessageService; import com.diboot.components.msg.vo.MessageVO; -import com.diboot.core.binding.manager.RelationsBinder; +import com.diboot.core.binding.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.service.BaseService; import com.diboot.core.vo.JsonResult; diff --git a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index d4410bb..d396518 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java @@ -1,6 +1,7 @@ package com.diboot.example.controller; import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diboot.core.binding.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; @@ -62,7 +63,6 @@ public class SysUserController extends BaseCrudRestController { return new JsonResult(Status.OK, userVoList).bindPagination(pagination); } - /*** * 创建Entity * @return @@ -71,12 +71,8 @@ public class SysUserController extends BaseCrudRestController { @PostMapping("/") public JsonResult createEntity(@RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ - boolean success = sysUserService.createSysUser(entity); - if(success){ - return new JsonResult(Status.OK); - }else{ - return new JsonResult(Status.FAIL_OPERATION); - } + //TODO: 执行注册流程 + return super.createEntity(entity, result, modelMap); } @@ -89,21 +85,8 @@ public class SysUserController extends BaseCrudRestController { @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ - // Model属性值验证结果 - if(result.hasErrors()) { - return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); - } - if(modelMap.get(ERROR) != null){ - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } - entity.setId(id); - boolean success = sysUserService.updateSysUser(entity); - if(success){ - return new JsonResult(Status.OK); - }else{ - return new JsonResult(Status.FAIL_OPERATION); - } + return super.updateEntity(entity, result, modelMap); } /*** @@ -127,12 +110,7 @@ public class SysUserController extends BaseCrudRestController { */ @DeleteMapping("/{id}") public JsonResult deleteModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ - boolean success = sysUserService.deleteSysUser(id); - if(success){ - return new JsonResult(Status.OK); - }else{ - return new JsonResult(Status.FAIL_OPERATION ); - } + return super.deleteEntity(id); } /*** @@ -173,35 +151,21 @@ public class SysUserController extends BaseCrudRestController { * 校验用户名是否重复 * */ @GetMapping("/checkUsernameRepeat") - public JsonResult checkUsernameRepeat(@RequestParam("id") Long id,@RequestParam("username") String username, HttpServletRequest request){ - if(V.notEmpty(username)){ - QueryWrapper wrapper = new QueryWrapper(); - wrapper.lambda().eq(SysUser::getUsername, username); - List sysUserList = sysUserService.getEntityList(wrapper); - if(V.isEmpty(id)){//新建时 - if(V.notEmpty(sysUserList)){ - return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); - } - }else{//更新时 - SysUser sysUser = sysUserService.getEntity(id); - if(V.notEmpty(sysUser)){ - if(V.notEmpty(sysUserList)){ - if(sysUserList.size() >= 2){ - return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); - }else if(!(sysUser.getId().equals(sysUserList.get(0).getId()))){ - return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); - } - } - }else{ - if(V.notEmpty(sysUserList)){ - return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); - } - } - } - + public JsonResult checkUsernameRepeat(@RequestParam(required = false) Long id,@RequestParam String username, HttpServletRequest request){ + if(V.isEmpty(username)){ + return new JsonResult(Status.OK); + } + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(SysUser::getUsername, username); + if (id != null){ + queryWrapper.ne(SysUser::getId, id); } - return new JsonResult(Status.OK); + List sysUserList = sysUserService.getEntityList(queryWrapper); + if (V.isEmpty(sysUserList)){ + return new JsonResult(Status.OK, "用户名可用"); + } + return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); } From 125fc5dc181c1588ab81f5e02385d7f6f91ea7d4 Mon Sep 17 00:00:00 2001 From: godchao Date: Fri, 19 Jul 2019 18:47:44 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=B8=AD=E8=AE=BE=E7=BD=AE=E5=AF=86=E7=A0=81=E5=AF=B9=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E8=BF=9B=E8=A1=8C=E5=8A=A0=E5=AF=86=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=9BBaseCrudController=E7=B1=BB=E4=B8=AD=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E3=80=81=E6=9B=B4=E6=96=B0=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E7=BD=AE=E5=92=8C=E5=90=8E=E7=BD=AE=E6=93=8D=E4=BD=9C=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BaseCrudRestController.java | 400 ++++++++++-------- .../example/config/SpringMvcConfig.java | 1 - .../example/controller/SysUserController.java | 29 +- .../com/diboot/example/entity/SysUser.java | 27 +- .../java/com/diboot/shiro/entity/SysUser.java | 8 - 5 files changed, 259 insertions(+), 206 deletions(-) diff --git a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java index fb12772..8b86e7e 100644 --- a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java +++ b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java @@ -27,191 +27,251 @@ import java.util.Map; */ @RestController public abstract class BaseCrudRestController extends BaseController { - private static final Logger log = LoggerFactory.getLogger(BaseCrudRestController.class); + private static final Logger log = LoggerFactory.getLogger(BaseCrudRestController.class); - /** - * 获取service实例 - * @return - */ - protected abstract BaseService getService(); + /** + * 获取service实例 + * + * @return + */ + protected abstract BaseService getService(); - /*** - * 获取某资源的集合 - *

- * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR - *

- * @param request - * @return JsonResult - * @throws Exception - */ - protected JsonResult getEntityList(HttpServletRequest request, Wrapper queryWrapper) throws Exception { - // 查询当前页的数据 - List entityList = getService().getEntityList(queryWrapper); - // 返回结果 - return new JsonResult(Status.OK, entityList); - } - - /*** - * 获取某资源的集合 - *

- * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR - *

- * @param request - * @return JsonResult - * @throws Exception - */ - protected JsonResult getEntityListWithPaging(HttpServletRequest request, Wrapper queryWrapper) throws Exception { - // 构建分页 - Pagination pagination = buildPagination(request); - // 查询当前页的数据 - List entityList = getService().getEntityList(queryWrapper, pagination); - // 返回结果 - return new JsonResult(Status.OK, entityList).bindPagination(pagination); - } - - /*** - * 获取某VO资源的集合 - *

- * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR - *

- * @param request - * @return JsonResult - * @throws Exception - */ - protected JsonResult getVOListWithPaging(HttpServletRequest request, Wrapper queryWrapper, Class clazz) throws Exception { - // 构建分页 - Pagination pagination = buildPagination(request); - // 查询当前页的数据 - List voList = getService().getViewObjectList(queryWrapper, pagination, clazz); - // 返回结果 - return new JsonResult(Status.OK, voList).bindPagination(pagination); - } - - /*** - * 根据id获取某资源对象 - * @param id - * @return JsonResult - * @throws Exception - */ - protected JsonResult getEntity(Long id) throws Exception { - Object entity = getService().getEntity(id); - return new JsonResult(Status.OK, entity); + /*** + * 获取某资源的集合 + *

+ * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR + *

+ * @param request + * @return JsonResult + * @throws Exception + */ + protected JsonResult getEntityList(HttpServletRequest request, Wrapper queryWrapper) throws Exception { + // 查询当前页的数据 + List entityList = getService().getEntityList(queryWrapper); + // 返回结果 + return new JsonResult(Status.OK, entityList); } - /*** - * 创建资源对象 - * @param entity - * @param result - * @return JsonResult - * @throws Exception - */ - protected JsonResult createEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception { + /*** + * 获取某资源的集合 + *

+ * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR + *

+ * @param request + * @return JsonResult + * @throws Exception + */ + protected JsonResult getEntityListWithPaging(HttpServletRequest request, Wrapper queryWrapper) throws Exception { + // 构建分页 + Pagination pagination = buildPagination(request); + // 查询当前页的数据 + List entityList = getService().getEntityList(queryWrapper, pagination); + // 返回结果 + return new JsonResult(Status.OK, entityList).bindPagination(pagination); + } + + /*** + * 获取某VO资源的集合 + *

+ * url参数示例: /dictionary/list?_pageSize=20&_pageIndex=1&_orderBy=itemValue&type=GENDAR + *

+ * @param request + * @return JsonResult + * @throws Exception + */ + protected JsonResult getVOListWithPaging(HttpServletRequest request, Wrapper queryWrapper, Class clazz) throws Exception { + // 构建分页 + Pagination pagination = buildPagination(request); + // 查询当前页的数据 + List voList = getService().getViewObjectList(queryWrapper, pagination, clazz); + // 返回结果 + return new JsonResult(Status.OK, voList).bindPagination(pagination); + } + + /*** + * 根据id获取某资源对象 + * @param id + * @return JsonResult + * @throws Exception + */ + protected JsonResult getEntity(Long id) throws Exception { + Object entity = getService().getEntity(id); + return new JsonResult(Status.OK, entity); + } + + /*** + * 创建资源对象 + * @param entity + * @param result + * @return JsonResult + * @throws Exception + */ + protected JsonResult createEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception { // Model属性值验证结果 - if(result != null && result.hasErrors()) { + if (result != null && result.hasErrors()) { return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - if(modelMap.get(ERROR) != null){ - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } + if (modelMap.get(ERROR) != null) { + return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + } + + // 执行创建资源前的操作 + this.beforeCreate(entity, modelMap); + if (modelMap.get(ERROR) != null) { + return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + } + // 执行保存操作 boolean success = getService().createEntity(entity); - if(success){ - // 组装返回结果 - Map data = new HashMap<>(2); - data.put(PARAM_ID, entity.getId()); - return new JsonResult(Status.OK, data); - } - else{ - log.warn("创建操作未成功,model="+entity.getClass().getSimpleName()); - // 组装返回结果 - return new JsonResult(Status.FAIL_OPERATION); + if (success) { + // 执行创建成功后的操作 + this.afterCreated(entity, modelMap); + // 组装返回结果 + Map data = new HashMap<>(2); + data.put(PARAM_ID, entity.getId()); + return new JsonResult(Status.OK, data); + } else { + log.warn("创建操作未成功,model=" + entity.getClass().getSimpleName()); + // 组装返回结果 + return new JsonResult(Status.FAIL_OPERATION); } } - /*** - * 根据ID更新资源对象 - * @param entity - * @param result - * @return JsonResult - * @throws Exception - */ - protected JsonResult updateEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception{ - // Model属性值验证结果 - if(result.hasErrors()) { - return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); - } - if(modelMap.get(ERROR) != null){ - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } - // 执行保存操作 - boolean success = getService().updateEntity(entity); - if(success){ - // 组装返回结果 - Map data = new HashMap<>(2); - data.put(PARAM_ID, entity.getId()); - return new JsonResult(Status.OK, data); + /*** + * 根据ID更新资源对象 + * @param entity + * @param result + * @return JsonResult + * @throws Exception + */ + protected JsonResult updateEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception { + // Model属性值验证结果 + if (result.hasErrors()) { + return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - else{ - log.warn("更新操作失败,model="+entity.getClass().getSimpleName()+", id="+entity.getId()); - // 返回操作结果 - return new JsonResult(Status.FAIL_OPERATION); + if (modelMap.get(ERROR) != null) { + return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); } - } - - /*** - * 根据id删除资源对象 - * @param id - * @return - * @throws Exception - */ - protected JsonResult deleteEntity(Serializable id) throws Exception{ - if(id == null) { - return new JsonResult(Status.FAIL_INVALID_PARAM, "请选择需要删除的条目!"); - } - // 是否有权限删除 - BaseEntity model = (BaseEntity) getService().getEntity(id); - // 执行删除操作 - String error = beforeDelete(model); - if(error != null){ - // 返回json - return new JsonResult(Status.FAIL_OPERATION, error); - } - // 执行删除操作 - boolean success = getService().deleteEntity(id); - if(success){ - log.info("删除操作成功,model="+model.getClass().getSimpleName()+", id="+id); - // 组装返回结果 - Map data = new HashMap<>(2); - data.put(PARAM_ID, model.getId()); - return new JsonResult(Status.OK, data); - } - else{ - log.warn("删除操作未成功,model="+model.getClass().getSimpleName()+", id="+id); - return new JsonResult(Status.FAIL_OPERATION); - } - } - /** - * 自动转换为VO并绑定关联关系 - * @param entityList - * @param voClass - * @param - * @return - */ - protected List convertToVoAndBindRelations(List entityList, Class voClass){ - // 转换为VO - List voList = RelationsBinder.convertAndBind(entityList, voClass); - return voList; - } + // 执行更新资源前的操作 + this.beforeUpdate(entity, modelMap); + if (modelMap.get(ERROR) != null) { + return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + } - //============= 供子类继承重写的方法 ================= - /*** - * 是否有删除权限,如不可删除返回错误提示信息,如 Status.FAIL_NO_PERMISSION.label() - * @param entity - * @return - */ - protected String beforeDelete(BaseEntity entity){ - return null; - } + // 执行保存操作 + boolean success = getService().updateEntity(entity); + if (success) { + // 执行更新成功后的操作 + this.afterUpdated(entity, modelMap); + // 组装返回结果 + Map data = new HashMap<>(2); + data.put(PARAM_ID, entity.getId()); + return new JsonResult(Status.OK, data); + } else { + log.warn("更新操作失败,model=" + entity.getClass().getSimpleName() + ", id=" + entity.getId()); + // 返回操作结果 + return new JsonResult(Status.FAIL_OPERATION); + } + } + + /*** + * 根据id删除资源对象 + * @param id + * @return + * @throws Exception + */ + protected JsonResult deleteEntity(Serializable id) throws Exception { + if (id == null) { + return new JsonResult(Status.FAIL_INVALID_PARAM, "请选择需要删除的条目!"); + } + // 是否有权限删除 + BaseEntity model = (BaseEntity) getService().getEntity(id); + // 执行删除操作 + String error = beforeDelete(model); + if (error != null) { + // 返回json + return new JsonResult(Status.FAIL_OPERATION, error); + } + // 执行删除操作 + boolean success = getService().deleteEntity(id); + if (success) { + log.info("删除操作成功,model=" + model.getClass().getSimpleName() + ", id=" + id); + // 组装返回结果 + Map data = new HashMap<>(2); + data.put(PARAM_ID, model.getId()); + return new JsonResult(Status.OK, data); + } else { + log.warn("删除操作未成功,model=" + model.getClass().getSimpleName() + ", id=" + id); + return new JsonResult(Status.FAIL_OPERATION); + } + } + + /** + * 自动转换为VO并绑定关联关系 + * + * @param entityList + * @param voClass + * @param + * @return + */ + protected List convertToVoAndBindRelations(List entityList, Class voClass) { + // 转换为VO + List voList = RelationsBinder.convertAndBind(entityList, voClass); + return voList; + } + + //============= 供子类继承重写的方法 ================= + + /*** + * 创建前的相关处理 + * @param entity + * @return + */ + protected void beforeCreate(BaseEntity entity, ModelMap modelMap) throws Exception { + } + + //============= 供子类继承重写的方法 ================= + + /*** + * 创建成功后的相关处理 + * @param entity + * @return + */ + protected String afterCreated(BaseEntity entity, ModelMap modelMap) throws Exception { + return null; + } + + //============= 供子类继承重写的方法 ================= + + /*** + * 更新前的相关处理 + * @param entity + * @return + */ + protected void beforeUpdate(BaseEntity entity, ModelMap modelMap) throws Exception { + } + + //============= 供子类继承重写的方法 ================= + + /*** + * 更新成功后的相关处理 + * @param entity + * @return + */ + protected String afterUpdated(BaseEntity entity, ModelMap modelMap) throws Exception { + return null; + } + + //============= 供子类继承重写的方法 ================= + + /*** + * 是否有删除权限,如不可删除返回错误提示信息,如 Status.FAIL_NO_PERMISSION.label() + * @param entity + * @return + */ + protected String beforeDelete(BaseEntity entity) { + return null; + } } \ No newline at end of file diff --git a/diboot-example/src/main/java/com/diboot/example/config/SpringMvcConfig.java b/diboot-example/src/main/java/com/diboot/example/config/SpringMvcConfig.java index 961f84e..7f3297a 100644 --- a/diboot-example/src/main/java/com/diboot/example/config/SpringMvcConfig.java +++ b/diboot-example/src/main/java/com/diboot/example/config/SpringMvcConfig.java @@ -36,7 +36,6 @@ import java.util.List; @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class}) @EnableTransactionManagement(proxyTargetClass=true) @ComponentScan(basePackages={"com.diboot"}) -@MapperScan({"com.diboot.**.mapper"}) public class SpringMvcConfig implements WebMvcConfigurer{ private static final Logger log = LoggerFactory.getLogger(SpringMvcConfig.class); diff --git a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index d396518..0b6a1ac 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diboot.core.binding.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; +import com.diboot.core.entity.BaseEntity; import com.diboot.core.service.BaseService; import com.diboot.core.service.DictionaryService; import com.diboot.core.util.V; @@ -20,6 +21,7 @@ import com.diboot.example.vo.SysUserListVO; import com.diboot.example.vo.SysUserVO; import com.diboot.shiro.entity.Role; import com.diboot.shiro.service.RoleService; +import com.diboot.shiro.util.AuthHelper; import com.diboot.shiro.util.JwtHelper; import com.diboot.shiro.vo.RoleVO; import org.slf4j.Logger; @@ -71,7 +73,6 @@ public class SysUserController extends BaseCrudRestController { @PostMapping("/") public JsonResult createEntity(@RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ - //TODO: 执行注册流程 return super.createEntity(entity, result, modelMap); } @@ -168,7 +169,33 @@ public class SysUserController extends BaseCrudRestController { return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); } + @Override + protected void beforeCreate(BaseEntity entity, ModelMap modelMap) throws Exception { + SysUser sysUser = (SysUser) entity; + if (V.notEmpty(sysUser.getPassword())){ + this.encryptPassword(sysUser); + } + } + @Override + protected void beforeUpdate(BaseEntity entity, ModelMap modelMap) throws Exception { + SysUser sysUser = (SysUser) entity; + if (V.notEmpty(sysUser.getPassword())){ + this.encryptPassword(sysUser); + } + } + + /*** + * 设置加密密码相关的数据 + * @param sysUser + */ + private void encryptPassword(SysUser sysUser) { + String salt = AuthHelper.createSalt(); + String password = AuthHelper.encryptMD5(sysUser.getPassword(), salt, true); + sysUser.setSalt(salt); + sysUser.setDepartmentId(0L); + sysUser.setPassword(password); + } /*** * 获取登录用户信息 diff --git a/diboot-example/src/main/java/com/diboot/example/entity/SysUser.java b/diboot-example/src/main/java/com/diboot/example/entity/SysUser.java index a6a48a1..d9d4a27 100644 --- a/diboot-example/src/main/java/com/diboot/example/entity/SysUser.java +++ b/diboot-example/src/main/java/com/diboot/example/entity/SysUser.java @@ -1,10 +1,6 @@ package com.diboot.example.entity; import com.baomidou.mybatisplus.annotation.TableField; -import com.diboot.core.entity.BaseEntity; -import com.diboot.shiro.entity.Permission; -import com.diboot.shiro.entity.Role; -import com.diboot.shiro.vo.RoleVO; import lombok.Data; import java.util.List; @@ -15,7 +11,7 @@ import java.util.List; * @date 2019/6/6 */ @Data -public class SysUser extends BaseEntity { +public class SysUser extends com.diboot.shiro.entity.SysUser { private static final long serialVersionUID = 466801280426981780L; @@ -24,18 +20,6 @@ public class SysUser extends BaseEntity { // gender字段的关联元数据 public static final String GENDER = "GENDER"; - @TableField - private Long departmentId; - - @TableField - private String username; - - @TableField - private String password; - - @TableField - private String gender; - @TableField private String phone; @@ -47,13 +31,4 @@ public class SysUser extends BaseEntity { @TableField private String comment; - - @TableField(exist = false) - private List roleList; - - @TableField(exist = false) - private List roleVOList; - - @TableField(exist = false) - private List permissionList; } diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/entity/SysUser.java b/diboot-shiro/src/main/java/com/diboot/shiro/entity/SysUser.java index e2053ae..1f2889a 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/entity/SysUser.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/entity/SysUser.java @@ -41,12 +41,4 @@ public class SysUser extends BaseEntity { @TableField(exist = false) private List permissionList; - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } } From a8168c57ea03077d80339a8def0491c4f138ef8b Mon Sep 17 00:00:00 2001 From: godchao Date: Fri, 19 Jul 2019 19:01:29 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E4=B8=8E=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/controller/SysUserController.java | 56 +++++++++---------- .../service/impl/SysUserServiceImpl.java | 21 +++++++ 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index 0b6a1ac..ea57b7a 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java @@ -73,7 +73,12 @@ public class SysUserController extends BaseCrudRestController { @PostMapping("/") public JsonResult createEntity(@RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ - return super.createEntity(entity, result, modelMap); + boolean success = sysUserService.createSysUser(entity); + if(success){ + return new JsonResult(Status.OK); + }else{ + return new JsonResult(Status.FAIL_OPERATION); + } } @@ -86,8 +91,21 @@ public class SysUserController extends BaseCrudRestController { @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ + // Model属性值验证结果 + if(result.hasErrors()) { + return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); + } + if(modelMap.get(ERROR) != null){ + return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + } + entity.setId(id); - return super.updateEntity(entity, result, modelMap); + boolean success = sysUserService.updateSysUser(entity); + if(success){ + return new JsonResult(Status.OK, "更新成功"); + }else{ + return new JsonResult(Status.FAIL_OPERATION, "更新失败"); + } } /*** @@ -111,7 +129,12 @@ public class SysUserController extends BaseCrudRestController { */ @DeleteMapping("/{id}") public JsonResult deleteModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ - return super.deleteEntity(id); + boolean success = sysUserService.deleteSysUser(id); + if(success){ + return new JsonResult(Status.OK); + }else{ + return new JsonResult(Status.FAIL_OPERATION ); + } } /*** @@ -169,33 +192,6 @@ public class SysUserController extends BaseCrudRestController { return new JsonResult(Status.FAIL_OPERATION, "用户名已存在"); } - @Override - protected void beforeCreate(BaseEntity entity, ModelMap modelMap) throws Exception { - SysUser sysUser = (SysUser) entity; - if (V.notEmpty(sysUser.getPassword())){ - this.encryptPassword(sysUser); - } - } - - @Override - protected void beforeUpdate(BaseEntity entity, ModelMap modelMap) throws Exception { - SysUser sysUser = (SysUser) entity; - if (V.notEmpty(sysUser.getPassword())){ - this.encryptPassword(sysUser); - } - } - - /*** - * 设置加密密码相关的数据 - * @param sysUser - */ - private void encryptPassword(SysUser sysUser) { - String salt = AuthHelper.createSalt(); - String password = AuthHelper.encryptMD5(sysUser.getPassword(), salt, true); - sysUser.setSalt(salt); - sysUser.setDepartmentId(0L); - sysUser.setPassword(password); - } /*** * 获取登录用户信息 diff --git a/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java b/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java index 2ddf13e..4de2bda 100644 --- a/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java +++ b/diboot-example/src/main/java/com/diboot/example/service/impl/SysUserServiceImpl.java @@ -13,6 +13,7 @@ import com.diboot.example.vo.SysUserVO; import com.diboot.shiro.entity.Role; import com.diboot.shiro.entity.UserRole; import com.diboot.shiro.service.UserRoleService; +import com.diboot.shiro.util.AuthHelper; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,6 +76,11 @@ public class SysUserServiceImpl extends BaseServiceImpl @Override @Transactional public boolean createSysUser(SysUser user) { + // 对密码进行处理 + if (V.notEmpty(user.getPassword())){ + this.encryptPassword(user); + } + //新建用户信息 boolean success = super.createEntity(user); if(!success){ @@ -102,6 +108,10 @@ public class SysUserServiceImpl extends BaseServiceImpl @Override @Transactional public boolean updateSysUser(SysUser user) { + // 对密码进行处理 + if (V.notEmpty(user.getPassword())){ + this.encryptPassword(user); + } //更新用户信息 boolean success = super.updateEntity(user); if(!success){ @@ -182,4 +192,15 @@ public class SysUserServiceImpl extends BaseServiceImpl return true; } + + /*** + * 设置加密密码相关的数据 + * @param sysUser + */ + private void encryptPassword(SysUser sysUser) { + String salt = AuthHelper.createSalt(); + String password = AuthHelper.encryptMD5(sysUser.getPassword(), salt, true); + sysUser.setSalt(salt); + sysUser.setPassword(password); + } } From 061298f334faafe1c59c4cf9434685b6e65b4a1c Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sat, 20 Jul 2019 13:00:09 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BC=98=E5=8C=96BaseCrudRestController?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4ModelMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msg/controller/MessageController.java | 8 +-- .../controller/MessageTemplateController.java | 6 +- .../controller/BaseCrudRestController.java | 68 ++++++------------- .../core/handle/DefaultExceptionHandler.java | 3 - .../controller/DepartmentController.java | 8 +-- .../controller/PermissionController.java | 8 +-- 6 files changed, 35 insertions(+), 66 deletions(-) diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java index e11e8ab..832ecbe 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java @@ -63,9 +63,9 @@ public class MessageController extends BaseCrudRestController { * @throws Exception */ @PostMapping("/") - public JsonResult createEntity(@ModelAttribute Message entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@ModelAttribute Message entity, BindingResult result, HttpServletRequest request) throws Exception{ - return super.createEntity(entity, result, modelMap); + return super.createEntity(entity, result); } /*** @@ -76,8 +76,8 @@ public class MessageController extends BaseCrudRestController { */ @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute Message entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ - return super.updateEntity(entity, result, modelMap); + HttpServletRequest request) throws Exception{ + return super.updateEntity(entity, result); } /*** diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java index 4ca05fb..520de74 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java @@ -59,9 +59,9 @@ public class MessageTemplateController extends BaseCrudRestController { * @throws Exception */ @PostMapping("/") - public JsonResult createEntity(@ModelAttribute MessageTemplate entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@ModelAttribute MessageTemplate entity, BindingResult result, HttpServletRequest request) throws Exception{ - return super.createEntity(entity, result, modelMap); + return super.createEntity(entity, result); } /*** @@ -73,7 +73,7 @@ public class MessageTemplateController extends BaseCrudRestController { @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute MessageTemplate entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) throws Exception{ - return super.updateEntity(entity, result, modelMap); + return super.updateEntity(entity, result); } /*** diff --git a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java index 8b86e7e..e16083a 100644 --- a/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java +++ b/diboot-core/src/main/java/com/diboot/core/controller/BaseCrudRestController.java @@ -45,7 +45,7 @@ public abstract class BaseCrudRestController extends BaseController { * @return JsonResult * @throws Exception */ - protected JsonResult getEntityList(HttpServletRequest request, Wrapper queryWrapper) throws Exception { + protected JsonResult getEntityList(HttpServletRequest request, Wrapper queryWrapper) throws Exception { // 查询当前页的数据 List entityList = getService().getEntityList(queryWrapper); // 返回结果 @@ -88,17 +88,6 @@ public abstract class BaseCrudRestController extends BaseController { return new JsonResult(Status.OK, voList).bindPagination(pagination); } - /*** - * 根据id获取某资源对象 - * @param id - * @return JsonResult - * @throws Exception - */ - protected JsonResult getEntity(Long id) throws Exception { - Object entity = getService().getEntity(id); - return new JsonResult(Status.OK, entity); - } - /*** * 创建资源对象 * @param entity @@ -106,26 +95,21 @@ public abstract class BaseCrudRestController extends BaseController { * @return JsonResult * @throws Exception */ - protected JsonResult createEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception { + protected JsonResult createEntity(BaseEntity entity, BindingResult result) throws Exception { // Model属性值验证结果 if (result != null && result.hasErrors()) { return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - if (modelMap.get(ERROR) != null) { - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } - // 执行创建资源前的操作 - this.beforeCreate(entity, modelMap); - if (modelMap.get(ERROR) != null) { - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + String validateResult = this.beforeCreate(entity); + if (validateResult != null) { + return new JsonResult(Status.FAIL_VALIDATION, validateResult); } - // 执行保存操作 boolean success = getService().createEntity(entity); if (success) { // 执行创建成功后的操作 - this.afterCreated(entity, modelMap); + this.afterCreated(entity); // 组装返回结果 Map data = new HashMap<>(2); data.put(PARAM_ID, entity.getId()); @@ -144,26 +128,21 @@ public abstract class BaseCrudRestController extends BaseController { * @return JsonResult * @throws Exception */ - protected JsonResult updateEntity(BaseEntity entity, BindingResult result, ModelMap modelMap) throws Exception { + protected JsonResult updateEntity(BaseEntity entity, BindingResult result) throws Exception { // Model属性值验证结果 if (result.hasErrors()) { return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - if (modelMap.get(ERROR) != null) { - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } - // 执行更新资源前的操作 - this.beforeUpdate(entity, modelMap); - if (modelMap.get(ERROR) != null) { - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); + String validateResult = this.beforeUpdate(entity); + if (validateResult != null) { + return new JsonResult(Status.FAIL_VALIDATION, validateResult); } - // 执行保存操作 boolean success = getService().updateEntity(entity); if (success) { // 执行更新成功后的操作 - this.afterUpdated(entity, modelMap); + this.afterUpdated(entity); // 组装返回结果 Map data = new HashMap<>(2); data.put(PARAM_ID, entity.getId()); @@ -188,10 +167,10 @@ public abstract class BaseCrudRestController extends BaseController { // 是否有权限删除 BaseEntity model = (BaseEntity) getService().getEntity(id); // 执行删除操作 - String error = beforeDelete(model); - if (error != null) { + String validateResult = beforeDelete(model); + if (validateResult != null) { // 返回json - return new JsonResult(Status.FAIL_OPERATION, error); + return new JsonResult(Status.FAIL_OPERATION, validateResult); } // 执行删除操作 boolean success = getService().deleteEntity(id); @@ -222,49 +201,42 @@ public abstract class BaseCrudRestController extends BaseController { } //============= 供子类继承重写的方法 ================= - /*** * 创建前的相关处理 * @param entity * @return */ - protected void beforeCreate(BaseEntity entity, ModelMap modelMap) throws Exception { + protected String beforeCreate(BaseEntity entity) throws Exception { + return null; } - //============= 供子类继承重写的方法 ================= - /*** * 创建成功后的相关处理 * @param entity * @return */ - protected String afterCreated(BaseEntity entity, ModelMap modelMap) throws Exception { + protected String afterCreated(BaseEntity entity) throws Exception { return null; } - //============= 供子类继承重写的方法 ================= - /*** * 更新前的相关处理 * @param entity * @return */ - protected void beforeUpdate(BaseEntity entity, ModelMap modelMap) throws Exception { + protected String beforeUpdate(BaseEntity entity) throws Exception { + return null; } - //============= 供子类继承重写的方法 ================= - /*** * 更新成功后的相关处理 * @param entity * @return */ - protected String afterUpdated(BaseEntity entity, ModelMap modelMap) throws Exception { + protected String afterUpdated(BaseEntity entity) throws Exception { return null; } - //============= 供子类继承重写的方法 ================= - /*** * 是否有删除权限,如不可删除返回错误提示信息,如 Status.FAIL_NO_PERMISSION.label() * @param entity diff --git a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionHandler.java b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionHandler.java index 5e1adf9..188ffa3 100644 --- a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionHandler.java +++ b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionHandler.java @@ -26,9 +26,6 @@ import java.util.Map; public class DefaultExceptionHandler { private final static Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class); - @Autowired - private ServerProperties serverProperties; - /** * 统一异常处理类 * @param request diff --git a/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java b/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java index 9153c90..bbc4ca9 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java @@ -95,12 +95,12 @@ public class DepartmentController extends BaseCrudRestController { * @throws Exception */ @PostMapping("/") - public JsonResult createEntity(@ModelAttribute DepartmentVO viewObject, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@ModelAttribute DepartmentVO viewObject, BindingResult result, HttpServletRequest request) throws Exception{ // 转换 Department entity = BeanUtils.convert(viewObject, Department.class); // 创建 - return super.createEntity(entity, result, modelMap); + return super.createEntity(entity, result); } /*** @@ -124,8 +124,8 @@ public class DepartmentController extends BaseCrudRestController { */ @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute Organization entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ - return super.updateEntity(entity, result, modelMap); + HttpServletRequest request) throws Exception{ + return super.updateEntity(entity, result); } /*** diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java b/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java index 182c692..9bef114 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java @@ -82,12 +82,12 @@ public class PermissionController extends BaseCrudRestController { */ @RequiresPermissions("permission:add") @PostMapping("/") - public JsonResult createEntity(@ModelAttribute PermissionVO viewObject, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@ModelAttribute PermissionVO viewObject, BindingResult result, HttpServletRequest request) throws Exception{ // 转换 Permission entity = BeanUtils.convert(viewObject, Permission.class); // 创建 - return super.createEntity(entity, result, modelMap); + return super.createEntity(entity, result); } /*** @@ -99,8 +99,8 @@ public class PermissionController extends BaseCrudRestController { @RequiresPermissions("permission:update") @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute Permission entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ - return super.updateEntity(entity, result, modelMap); + HttpServletRequest request) throws Exception{ + return super.updateEntity(entity, result); } /*** From 8d76fe8963ea375d3d0b6903a90e2526430dd0fd Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sat, 20 Jul 2019 13:42:29 +0800 Subject: [PATCH 5/8] =?UTF-8?q?ModelMap=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msg/controller/MessageController.java | 2 +- .../msg/controller/MessageTemplateController.java | 4 ++-- .../example/controller/DepartmentController.java | 2 +- .../example/controller/SysUserController.java | 10 +++------- .../shiro/controller/PermissionController.java | 2 +- .../com/diboot/shiro/controller/RoleController.java | 13 +++++-------- 6 files changed, 13 insertions(+), 20 deletions(-) diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java index 832ecbe..84da038 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageController.java @@ -51,7 +51,7 @@ public class MessageController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/{id}") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ MessageVO entityVO = messageService.getViewObject(id, MessageVO.class); return new JsonResult(entityVO); diff --git a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java index 520de74..248721b 100644 --- a/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java +++ b/diboot-components-msg/src/main/java/com/diboot/components/msg/controller/MessageTemplateController.java @@ -47,7 +47,7 @@ public class MessageTemplateController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/{id}") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ MessageTemplate entity = messageTemplateService.getEntity(id); return new JsonResult(entity); @@ -72,7 +72,7 @@ public class MessageTemplateController extends BaseCrudRestController { */ @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute MessageTemplate entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ + HttpServletRequest request) throws Exception{ return super.updateEntity(entity, result); } diff --git a/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java b/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java index bbc4ca9..db54c3e 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java @@ -110,7 +110,7 @@ public class DepartmentController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/{id}") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ DepartmentVO vo = departmentService.getViewObject(id, DepartmentVO.class); return new JsonResult(vo); diff --git a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index ea57b7a..d2d4e8a 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java @@ -71,7 +71,7 @@ public class SysUserController extends BaseCrudRestController { * @throws Exception */ @PostMapping("/") - public JsonResult createEntity(@RequestBody SysUser entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@RequestBody SysUser entity, BindingResult result, HttpServletRequest request) throws Exception{ boolean success = sysUserService.createSysUser(entity); if(success){ @@ -90,15 +90,11 @@ public class SysUserController extends BaseCrudRestController { */ @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody SysUser entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ + HttpServletRequest request) throws Exception{ // Model属性值验证结果 if(result.hasErrors()) { return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - if(modelMap.get(ERROR) != null){ - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } - entity.setId(id); boolean success = sysUserService.updateSysUser(entity); if(success){ @@ -115,7 +111,7 @@ public class SysUserController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/{id}") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ SysUserVO sysUserVO = sysUserService.getSysUser(id); return new JsonResult(sysUserVO); diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java b/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java index 9bef114..0a76f2d 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/controller/PermissionController.java @@ -47,7 +47,7 @@ public class PermissionController extends BaseCrudRestController { */ @GetMapping("/{id}") @AuthorizationWrapper(value = @RequiresPermissions("get"), name = "查看") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ PermissionVO vo = permissionService.getViewObject(id, PermissionVO.class); return new JsonResult(vo); diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java b/diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java index 1a694b8..9c334ec 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java @@ -71,7 +71,7 @@ public class RoleController extends BaseCrudRestController { * @throws Exception */ @PostMapping("/") - public JsonResult createEntity(@RequestBody Role entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) + public JsonResult createEntity(@RequestBody Role entity, BindingResult result, HttpServletRequest request) throws Exception{ // 创建 boolean success = roleService.createRole(entity); @@ -88,7 +88,7 @@ public class RoleController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/toUpdatePage/{id}") - public JsonResult toUpdatePage(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult toUpdatePage(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ RoleVO roleVO = roleService.toUpdatePage(id); return new JsonResult(roleVO); @@ -103,14 +103,11 @@ public class RoleController extends BaseCrudRestController { */ @PutMapping("/{id}") public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody Role entity, BindingResult result, - HttpServletRequest request, ModelMap modelMap) throws Exception{ + HttpServletRequest request) throws Exception{ // Model属性值验证结果 if(result.hasErrors()) { return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result)); } - if(modelMap.get(ERROR) != null){ - return new JsonResult(Status.FAIL_VALIDATION, (String) modelMap.get(ERROR)); - } entity.setId(id); boolean success = roleService.updateRole(entity); @@ -129,7 +126,7 @@ public class RoleController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/{id}") - public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ RoleVO roleVO = roleService.getRole(id); return new JsonResult(roleVO); @@ -157,7 +154,7 @@ public class RoleController extends BaseCrudRestController { * @throws Exception */ @GetMapping("/getAllMenu") - public JsonResult getAllMenu(HttpServletRequest request, ModelMap modelMap) + public JsonResult getAllMenu(HttpServletRequest request) throws Exception{ List list = roleService.getAllMenu(); return new JsonResult(list); From bd97e5da44dcdc0a3844f0d22dd6b66b402260ee Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sat, 20 Jul 2019 14:47:11 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/diboot/core/test/util/PropertiesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java b/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java index 6b3dcf6..fd0d785 100644 --- a/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java +++ b/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java @@ -15,7 +15,7 @@ public class PropertiesTest { @Test public void testGetString(){ String str1 = PropertiesUtils.get("spring.datasource.url"); - String str2 = PropertiesUtils.get("spring.datasource.url", "application.properties.bak"); + String str2 = PropertiesUtils.get("spring.datasource.url", "application.properties"); Assert.assertNotNull(str1); Assert.assertNotNull(str2); } From 77d880104f163620a9c9f415345e219534dc026a Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sat, 20 Jul 2019 15:39:07 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diboot-core/README.md | 47 +++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/diboot-core/README.md b/diboot-core/README.md index d860c39..2cee07e 100644 --- a/diboot-core/README.md +++ b/diboot-core/README.md @@ -1,18 +1,18 @@ -## diboot-core: 全新优化内核 +# diboot-core: 全新优化内核 主要实现: 1. 多表关联的自动绑定, 实现单表CRUD和多表关联的无SQL化 2. 提供其他常用开发场景的最佳实践封装。 -### ** 单表CRUD无SQL +## ** 一. 单表CRUD无SQL > 依赖Mybatis-Plus实现(Mybatis-Plus具备通用Mapper方案和灵活的查询构造器) -### ** 多表关联查询无SQL(适用于大多数场景,拆分成单表查询自动实现结果绑定) +## ** 二. 多表关联查询无SQL(适用于大多数场景,拆分成单表查询自动实现结果绑定) > 通过注解实现多数场景下的关联查询无SQL -#### 2.1. 注解自动绑定数据字典(枚举值)的显示值Label +### 1. 注解自动绑定数据字典(枚举值)的显示值Label ~~~java @BindDict(type="GENDER", field = "gender") private String genderLabel; ~~~ -#### 2. 注解自动绑定其他表的字段 +### 2. 注解自动绑定其他表的字段 ~~~java // 支持关联条件+附加条件绑定字段 @BindField(entity=Department.class, field="name", condition="department_id=id AND parent_id>=0") @@ -22,7 +22,7 @@ private String deptName; @BindField(entity = Organization.class, field="name", condition="this.department_id=department.id AND department.org_id=id") private String orgName; ~~~ -#### 3. 注解自动绑定其他表实体Entity +### 3. 注解自动绑定其他表实体Entity ~~~java // 支持关联条件+附加条件绑定Entity @BindEntity(entity = Department.class, condition="department_id=id") @@ -32,7 +32,7 @@ private Department department; @BindEntity(entity = Organization.class, condition = "this.department_id=department.id AND department.org_id=id AND department.deleted=0") private Organization organization; ~~~ -#### 4. 注解自动绑定其他表实体集合List +### 4. 注解自动绑定其他表实体集合List ~~~java // 支持关联条件+附加条件绑定多个Entity @BindEntityList(entity = Department.class, condition = "id=parent_id") @@ -43,21 +43,34 @@ private List children; private List roleList; ~~~ -### ** 调用方式 -#### 1. 自动绑定关联(不需要转型) +## ** 三. 使用方式 +### 引入依赖 +Gradle: +~~~gradle +compile("com.diboot:diboot-core:2.0.1") +~~~ +或Maven +~~~xml + + com.diboot + diboot-core + 2.0.1 + +~~~ +### 定义你的Service(继承diboot的BaseService或Mybatis-plus的ISerivice)及Mapper + +### 使用注解绑定: +调用RelationsBinder自动绑定注解相关关联: +#### 方式1. 自动绑定关联(不需要转型) ~~~java -// 调用AnnotationBindingManager自动绑定注解相关关联 //List voList = ...; RelationsBinder.bind(voList); ~~~ -#### 2. 自动转型并绑定关联(需要转型) +#### 方式2. 自动转型并绑定关联(需要转型) ~~~java -// 获取Entity列表 -List entityList = userService.getEntityList(queryWrapper); -// 调用AnnotationBindingManager自动绑定注解相关关联 +// 查询单表获取Entity集合 +// List entityList = userService.list(queryWrapper); List voList = RelationsBinder.convertAndBind(userList, MyUserVO.class); ~~~ - -##### 使用样例请参考 - [diboot-core-example](https://github.com/dibo-software/diboot-v2-example/tree/master/diboot-core-example) - +## 使用样例请参考 - [diboot-core-example](https://github.com/dibo-software/diboot-v2-example/tree/master/diboot-core-example) From f74e06bd5b3f05bc21cee6a967de994a73f8e794 Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Sat, 20 Jul 2019 15:48:41 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diboot-core/README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/diboot-core/README.md b/diboot-core/README.md index 2cee07e..9f67c85 100644 --- a/diboot-core/README.md +++ b/diboot-core/README.md @@ -7,10 +7,10 @@ > 依赖Mybatis-Plus实现(Mybatis-Plus具备通用Mapper方案和灵活的查询构造器) ## ** 二. 多表关联查询无SQL(适用于大多数场景,拆分成单表查询自动实现结果绑定) > 通过注解实现多数场景下的关联查询无SQL -### 1. 注解自动绑定数据字典(枚举值)的显示值Label +### 1. 注解自动绑定数据字典(自定义枚举)的显示值Label ~~~java -@BindDict(type="GENDER", field = "gender") -private String genderLabel; +@BindDict(type="USER_STATUS", field = "status") +private String statusLabel; ~~~ ### 2. 注解自动绑定其他表的字段 ~~~java @@ -44,7 +44,7 @@ private List roleList; ~~~ ## ** 三. 使用方式 -### 引入依赖 +### 1. 引入依赖 Gradle: ~~~gradle compile("com.diboot:diboot-core:2.0.1") @@ -57,9 +57,9 @@ compile("com.diboot:diboot-core:2.0.1") 2.0.1 ~~~ -### 定义你的Service(继承diboot的BaseService或Mybatis-plus的ISerivice)及Mapper +### 2. 定义你的Service(继承diboot的BaseService或Mybatis-plus的ISerivice)及Mapper -### 使用注解绑定: +### 3. 使用注解绑定: 调用RelationsBinder自动绑定注解相关关联: #### 方式1. 自动绑定关联(不需要转型) ~~~java @@ -73,4 +73,4 @@ RelationsBinder.bind(voList); List voList = RelationsBinder.convertAndBind(userList, MyUserVO.class); ~~~ -## 使用样例请参考 - [diboot-core-example](https://github.com/dibo-software/diboot-v2-example/tree/master/diboot-core-example) +## 四. 样例参考 - [diboot-core-example](https://github.com/dibo-software/diboot-v2-example/tree/master/diboot-core-example)