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..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); @@ -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..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); @@ -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); } /*** @@ -72,8 +72,8 @@ 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); + HttpServletRequest request) throws Exception{ + return super.updateEntity(entity, result); } /*** diff --git a/diboot-core/README.md b/diboot-core/README.md index d860c39..9f67c85 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; +@BindDict(type="USER_STATUS", field = "status") +private String statusLabel; ~~~ -#### 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. 自动绑定关联(不需要转型) +## ** 三. 使用方式 +### 1. 引入依赖 +Gradle: +~~~gradle +compile("com.diboot:diboot-core:2.0.1") +~~~ +或Maven +~~~xml + + com.diboot + diboot-core + 2.0.1 + +~~~ +### 2. 定义你的Service(继承diboot的BaseService或Mybatis-plus的ISerivice)及Mapper + +### 3. 使用注解绑定: +调用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) 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..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 @@ -27,191 +27,223 @@ 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); + } + + /*** + * 创建资源对象 + * @param entity + * @param result + * @return JsonResult + * @throws Exception + */ + protected JsonResult createEntity(BaseEntity entity, BindingResult result) 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)); - } + // 执行创建资源前的操作 + String validateResult = this.beforeCreate(entity); + if (validateResult != null) { + return new JsonResult(Status.FAIL_VALIDATION, validateResult); + } // 执行保存操作 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); + // 组装返回结果 + 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) 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); + // 执行更新资源前的操作 + String validateResult = this.beforeUpdate(entity); + if (validateResult != null) { + return new JsonResult(Status.FAIL_VALIDATION, validateResult); } - } - - /*** - * 根据id删除资源对象 - * @param id - * @return - * @throws Exception - */ - protected JsonResult deleteEntity(Serializable id) throws Exception{ - if(id == null) { - return new JsonResult(Status.FAIL_INVALID_PARAM, "请选择需要删除的条目!"); + // 执行保存操作 + boolean success = getService().updateEntity(entity); + if (success) { + // 执行更新成功后的操作 + this.afterUpdated(entity); + // 组装返回结果 + 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); } - // 是否有权限删除 - 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; - } + /*** + * 根据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 validateResult = beforeDelete(model); + if (validateResult != null) { + // 返回json + return new JsonResult(Status.FAIL_OPERATION, validateResult); + } + // 执行删除操作 + 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); + } + } - //============= 供子类继承重写的方法 ================= - /*** - * 是否有删除权限,如不可删除返回错误提示信息,如 Status.FAIL_NO_PERMISSION.label() - * @param entity - * @return - */ - protected String beforeDelete(BaseEntity entity){ - return null; - } + /** + * 自动转换为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 String beforeCreate(BaseEntity entity) throws Exception { + return null; + } + + /*** + * 创建成功后的相关处理 + * @param entity + * @return + */ + protected String afterCreated(BaseEntity entity) throws Exception { + return null; + } + + /*** + * 更新前的相关处理 + * @param entity + * @return + */ + protected String beforeUpdate(BaseEntity entity) throws Exception { + return null; + } + + /*** + * 更新成功后的相关处理 + * @param entity + * @return + */ + protected String afterUpdated(BaseEntity entity) 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-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-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); } 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/DepartmentController.java b/diboot-example/src/main/java/com/diboot/example/controller/DepartmentController.java index 9153c90..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 @@ -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); } /*** @@ -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); @@ -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-example/src/main/java/com/diboot/example/controller/SysUserController.java b/diboot-example/src/main/java/com/diboot/example/controller/SysUserController.java index d4410bb..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 @@ -1,9 +1,11 @@ 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; +import com.diboot.core.entity.BaseEntity; import com.diboot.core.service.BaseService; import com.diboot.core.service.DictionaryService; import com.diboot.core.util.V; @@ -19,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; @@ -62,14 +65,13 @@ public class SysUserController extends BaseCrudRestController { return new JsonResult(Status.OK, userVoList).bindPagination(pagination); } - /*** * 创建Entity * @return * @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){ @@ -88,21 +90,17 @@ 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){ - return new JsonResult(Status.OK); + return new JsonResult(Status.OK, "更新成功"); }else{ - return new JsonResult(Status.FAIL_OPERATION); + return new JsonResult(Status.FAIL_OPERATION, "更新失败"); } } @@ -113,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); @@ -173,39 +171,24 @@ 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, "用户名已存在"); } - /*** * 获取登录用户信息 * @param request 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-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); + } } 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..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); @@ -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); } /*** 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); 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; - } }