From 81fc2d46b6d1682403b04023221c7981452cc51a Mon Sep 17 00:00:00 2001 From: wyldusername <2605214840@qq.com> Date: Thu, 11 Jul 2019 18:38:29 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shiro/controller/RoleController.java | 219 ++++++++++++++ .../com/diboot/shiro/entity/Permission.java | 11 + .../java/com/diboot/shiro/entity/Role.java | 9 + .../com/diboot/shiro/service/RoleService.java | 24 ++ .../shiro/service/impl/RoleServiceImpl.java | 281 +++++++++++++++++- .../main/java/com/diboot/shiro/vo/RoleVO.java | 6 +- 6 files changed, 547 insertions(+), 3 deletions(-) create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java 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 new file mode 100644 index 0000000..1a694b8 --- /dev/null +++ b/diboot-shiro/src/main/java/com/diboot/shiro/controller/RoleController.java @@ -0,0 +1,219 @@ +package com.diboot.shiro.controller; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.diboot.core.controller.BaseCrudRestController; +import com.diboot.core.service.BaseService; +import com.diboot.core.service.DictionaryService; +import com.diboot.core.util.V; +import com.diboot.core.vo.JsonResult; +import com.diboot.core.vo.KeyValue; +import com.diboot.core.vo.Pagination; +import com.diboot.core.vo.Status; +import com.diboot.shiro.entity.Permission; +import com.diboot.shiro.entity.Role; +import com.diboot.shiro.service.RoleService; +import com.diboot.shiro.vo.RoleVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@RestController +@RequestMapping("/role") +public class RoleController extends BaseCrudRestController { + + @Autowired + private RoleService roleService; + @Autowired + private DictionaryService dictionaryService; + + @Override + protected BaseService getService() { + return roleService; + } + + /*** + * 获取Entity列表(分页) + * @param request + * @return + * @throws Exception + */ + @GetMapping("/list") + public JsonResult getVOList(HttpServletRequest request) throws Exception{ + QueryWrapper queryWrapper = buildQuery(request); + // 构建分页 + Pagination pagination = buildPagination(request); + // 获取结果 + List voList = roleService.getRoleList(queryWrapper, pagination); + // 返回结果 + return new JsonResult(Status.OK, voList).bindPagination(pagination); + } + + /*** + * 显示创建页面 + * @return + * @throws Exception + */ + @GetMapping("/toCreatePage") + public JsonResult toCreatePage(HttpServletRequest request, ModelMap modelMap) + throws Exception{ + List menuList = roleService.getAllMenu(); + modelMap.put("menuList", menuList); + return new JsonResult(modelMap); + } + + /*** + * 创建Entity + * @return + * @throws Exception + */ + @PostMapping("/") + public JsonResult createEntity(@RequestBody Role entity, BindingResult result, HttpServletRequest request, ModelMap modelMap) + throws Exception{ + // 创建 + boolean success = roleService.createRole(entity); + if(success){ + return new JsonResult(Status.OK); + }else{ + return new JsonResult(Status.FAIL_OPERATION); + } + } + + /*** + * 显示更新页面 + * @return + * @throws Exception + */ + @GetMapping("/toUpdatePage/{id}") + public JsonResult toUpdatePage(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + throws Exception{ + RoleVO roleVO = roleService.toUpdatePage(id); + return new JsonResult(roleVO); + } + + + /*** + * 更新Entity + * @param id ID + * @return + * @throws Exception + */ + @PutMapping("/{id}") + public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody Role 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 = roleService.updateRole(entity); + + if(success){ + return new JsonResult(Status.OK); + }else{ + return new JsonResult(Status.FAIL_OPERATION); + } + } + + /*** + * 查询Entity + * @param id ID + * @return + * @throws Exception + */ + @GetMapping("/{id}") + public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap) + throws Exception{ + RoleVO roleVO = roleService.getRole(id); + return new JsonResult(roleVO); + } + + /*** + * 删除 + * @param id + * @return + * @throws Exception + */ + @DeleteMapping("/{id}") + public JsonResult deleteModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{ + boolean success = roleService.deleteRole(id); + if(success){ + return new JsonResult(Status.OK); + }else{ + return new JsonResult(Status.FAIL_OPERATION ); + } + } + + /*** + * 获取所有菜单,以及每个菜单下的所有权限 + * @return + * @throws Exception + */ + @GetMapping("/getAllMenu") + public JsonResult getAllMenu(HttpServletRequest request, ModelMap modelMap) + throws Exception{ + List list = roleService.getAllMenu(); + return new JsonResult(list); + } + + /*** + * 加载更多数据 + * @return + * @throws Exception + */ + @GetMapping("/attachMore") + public JsonResult attachMore(HttpServletRequest request, ModelMap modelMap) + throws Exception{ + + //获取角色状态KV + List roleStatusKvList = dictionaryService.getKeyValueList(Role.METATYPE_STATUS); + modelMap.put("roleStatusKvList", roleStatusKvList); + + return new JsonResult(modelMap); + } + + + /* + * 校验角色code是否重复 + * */ + @GetMapping("/checkCodeRepeat") + public JsonResult checkCodeRepeat(Long id, String code, HttpServletRequest request){ + if(V.notEmpty(code)){ + QueryWrapper wrapper = new QueryWrapper(); + wrapper.lambda().eq(Role::getCode, code); + List roleList = roleService.getEntityList(wrapper); + if(V.isEmpty(id)){//新建时 + if(V.notEmpty(roleList)){ + return new JsonResult(Status.FAIL_OPERATION, "code已存在"); + } + }else{//更新时 + Role role = roleService.getEntity(id); + if(V.notEmpty(role)){ + if(V.notEmpty(roleList)){ + if(roleList.size() >= 2){ + return new JsonResult(Status.FAIL_OPERATION, "code已存在"); + }else if(!(role.getId().equals(roleList.get(0).getId()))){ + return new JsonResult(Status.FAIL_OPERATION, "code已存在"); + } + } + }else{ + if(V.notEmpty(roleList)){ + return new JsonResult(Status.FAIL_OPERATION, "code已存在"); + } + } + } + + } + + return new JsonResult(Status.OK); + } + + +} diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/entity/Permission.java b/diboot-shiro/src/main/java/com/diboot/shiro/entity/Permission.java index f652a7a..4ea94b9 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/entity/Permission.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/entity/Permission.java @@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.diboot.core.entity.BaseEntity; import lombok.Data; +import java.util.ArrayList; +import java.util.List; + /** * @author Yangzhao * @version v2.0 @@ -26,4 +29,12 @@ public class Permission extends BaseEntity { @TableField private String permissionName; + //某角色是否拥有该权限 + @TableField(exist = false) + private boolean own = false; + + //菜单下的各种权限资源 + @TableField(exist = false) + private List permissionList; + } diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/entity/Role.java b/diboot-shiro/src/main/java/com/diboot/shiro/entity/Role.java index 6574e37..5b0db06 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/entity/Role.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/entity/Role.java @@ -16,12 +16,21 @@ public class Role extends BaseEntity { private static final long serialVersionUID = 5433209472424293571L; + // status字段的关联元数据 + public static final String METATYPE_STATUS = "ROLE_STATUS"; + @TableField private String name; @TableField private String code; + @TableField + private String status; + + @TableField + private String comment; + @TableField(exist = false) private List permissionList; diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/service/RoleService.java b/diboot-shiro/src/main/java/com/diboot/shiro/service/RoleService.java index 046c385..8933e53 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/service/RoleService.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/service/RoleService.java @@ -1,6 +1,9 @@ package com.diboot.shiro.service; +import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.diboot.core.service.BaseService; +import com.diboot.core.vo.Pagination; +import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Role; import com.diboot.shiro.vo.RoleVO; @@ -14,6 +17,27 @@ import java.util.List; */ public interface RoleService extends BaseService { + //获取角色列表 + List getRoleList(Wrapper queryWrapper, Pagination pagination); + + //获取角色信息 + RoleVO getRole(Long id); + + //新建角色信息 + boolean createRole(Role role); + + //显示更新页面 + RoleVO toUpdatePage(Long id); + + //修改角色信息 + boolean updateRole(Role role); + + //删除角色信息 + boolean deleteRole(Long id); + + //获取所有菜单 + List getAllMenu(); + /*** * 根据用户类型和用户id获取角色关联权限列表 * @param userType diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java index 8de144f..8eebded 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java @@ -1,20 +1,30 @@ package com.diboot.shiro.service.impl; +import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.util.V; +import com.diboot.core.vo.Pagination; +import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Role; +import com.diboot.shiro.entity.RolePermission; import com.diboot.shiro.entity.UserRole; import com.diboot.shiro.mapper.RoleMapper; +import com.diboot.shiro.service.PermissionService; +import com.diboot.shiro.service.RolePermissionService; import com.diboot.shiro.service.RoleService; import com.diboot.shiro.service.UserRoleService; +import com.diboot.shiro.vo.PermissionVO; import com.diboot.shiro.vo.RoleVO; import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; /** @@ -26,9 +36,276 @@ import java.util.stream.Collectors; @Service @Slf4j public class RoleServiceImpl extends BaseServiceImpl implements RoleService { + private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class); @Autowired private UserRoleService userRoleService; + @Autowired + private PermissionService permissionService; + @Autowired + private RolePermissionService rolePermissionService; + + @Override + public List getRoleList(Wrapper queryWrapper, Pagination pagination) { + List roleList = super.getEntityList(queryWrapper, pagination); + List roleVOList = AnnotationBindingManager.autoConvertAndBind(roleList, RoleVO.class); + if(V.notEmpty(roleVOList)){ + for(RoleVO roleVO : roleVOList){ + List permissionList = roleVO.getPermissionList(); + if(V.notEmpty(permissionList)){ + //获取这个角色拥有的菜单资源,并去重 + List menuList = new ArrayList();//菜单资源 + HashSet menuSet = new HashSet(); + Set idSet = new HashSet<>();//资源id set + for(Permission permission : permissionList){ + idSet.add(permission.getId()); + if(menuSet.add(permission.getMenuCode())){ + menuList.add(permission); + } + } + //获取菜单资源下的该角色已有的权限资源 + if(V.notEmpty(menuList)){ + for(Permission menu : menuList){ + QueryWrapper query = new QueryWrapper(); + query.lambda().in(Permission::getId, idSet) + .eq(Permission::getMenuCode, menu.getMenuCode()); + List menuPermissionList = permissionService.getEntityList(query); + menu.setPermissionList(menuPermissionList); + } + } + + roleVO.setMenuList(menuList); + } + } + } + + return roleVOList; + } + + + + @Override + public RoleVO getRole(Long id) { + RoleVO roleVO = super.getViewObject(id, RoleVO.class); + //角色已拥有的权限列表 + List ownPermissionList = roleVO.getPermissionList(); + //角色已拥有的菜单资源 + List ownMenuList = null; + Set idSet = new HashSet<>(); + if(V.notEmpty(ownPermissionList)){ + //获取这个角色拥有的菜单资源 + ownMenuList = new ArrayList(); + HashSet set = new HashSet(); + for(Permission permission : ownPermissionList){ + idSet.add(permission.getId()); + if(set.add(permission.getMenuCode())){ + ownMenuList.add(permission); + } + } + } + + //获取菜单资源下的该角色已有的权限资源 + if(V.notEmpty(ownMenuList)){ + for(Permission menu : ownMenuList){ + QueryWrapper query = new QueryWrapper(); + query.lambda().in(Permission::getId, idSet) + .eq(Permission::getMenuCode, menu.getMenuCode()); + List menuPermissionList = permissionService.getEntityList(query); + menu.setPermissionList(menuPermissionList); + } + } + + roleVO.setMenuList(ownMenuList); + + return roleVO; + } + + @Override + @Transactional + public boolean createRole(Role role) { + if(V.isEmpty(role)){ + return false; + } + + try{ + //新建角色信息 + boolean success = super.createEntity(role); + if(!success){ + return false; + } + //新建角色权限信息 + List permissionList = role.getPermissionList(); + if(V.notEmpty(permissionList)){ + for(Permission p : permissionList){ + RolePermission rolePermission = new RolePermission(); + rolePermission.setRoleId(role.getId()); + rolePermission.setPermissionId(p.getId()); + rolePermissionService.createEntity(rolePermission); + } + } + }catch(Exception e){ + throw new RuntimeException(); + } + + return true; + } + + @Override + public RoleVO toUpdatePage(Long id) { + RoleVO roleVO = super.getViewObject(id, RoleVO.class); + //角色已拥有的权限列表 + List ownPermissionList = roleVO.getPermissionList(); + //角色已拥有的菜单资源 + List ownMenuList = null; + if(V.notEmpty(ownPermissionList)){ + //获取这个角色拥有的菜单资源 + ownMenuList = new ArrayList(); + HashSet set = new HashSet(); + for(Permission permission : ownPermissionList){ + if(set.add(permission.getMenuCode())){ + ownMenuList.add(permission); + } + } + } + + //获取所有菜单及菜单下的权限信息 + List allMenuList = getAllMenu(); + + if(V.notEmpty(allMenuList)){ + for(Permission menu : allMenuList){ + List allPermissionList = menu.getPermissionList(); + //判断该角色是否有该菜单资源,若有设为true + if(V.notEmpty(ownMenuList)){ + for(Permission m : ownMenuList){ + if(menu.getMenuCode().equals(m.getMenuCode())){ + menu.setOwn(true); + } + } + } + //判断该角色是否有该资源权限,若有设为true + if(V.notEmpty(allPermissionList) && V.notEmpty(ownPermissionList)){ + for(Permission permission : allPermissionList){ + for(Permission p : ownPermissionList){ + if(permission.getId().equals(p.getId())){ + permission.setOwn(true); + } + } + } + } + } + } + + roleVO.setMenuList(allMenuList); + + return roleVO; + } + + @Override + @Transactional + public boolean updateRole(Role role) { + if(V.isEmpty(role)){ + return false; + } + try { + //更新角色信息 + boolean success = super.updateEntity(role); + if(!success){ + return false; + } + //获取角色原来拥有的权限信息 + QueryWrapper query = new QueryWrapper(); + query.lambda().eq(RolePermission::getRoleId, role.getId()); + List oldPermissionList = rolePermissionService.getEntityList(query); + + List newPermissionList = role.getPermissionList(); + StringBuffer oldBuffer = new StringBuffer(); + StringBuffer newBuffer = new StringBuffer(); + if(V.notEmpty(oldPermissionList)){ + for(RolePermission rp : oldPermissionList){ + oldBuffer.append(rp.getPermissionId()).append(","); + } + } + if(V.notEmpty(newPermissionList)){ + for(Permission p : newPermissionList){ + newBuffer.append(p.getId()).append(","); + } + } + + //删除页面取消选择的角色权限 + if(V.notEmpty(oldPermissionList)){ + for(RolePermission rp : oldPermissionList){ + if(!(newBuffer.toString().contains(rp.getPermissionId().toString()))){ + rolePermissionService.deleteEntity(rp.getId()); + } + } + } + + //新增页面选择的角色权限 + if(V.notEmpty(newPermissionList)){ + for(Permission p : newPermissionList){ + if(!(oldBuffer.toString().contains(p.getId().toString()))){ + RolePermission entity = new RolePermission(); + entity.setRoleId(role.getId()); + entity.setPermissionId(p.getId()); + rolePermissionService.createEntity(entity); + } + } + } + + } catch (Exception e) { + throw new RuntimeException(); + } + + return true; + } + + @Override + @Transactional + public boolean deleteRole(Long id) { + try { + boolean success = super.deleteEntity(id); + if(!success){ + return false; + } + //获取角色原来拥有的权限信息 + QueryWrapper query = new QueryWrapper(); + query.lambda().eq(RolePermission::getRoleId, id); + List rolePermissionList = rolePermissionService.getEntityList(query); + //删除角色权限 + if(V.notEmpty(rolePermissionList)){ + for(RolePermission rp : rolePermissionList){ + rolePermissionService.deleteEntity(rp.getId()); + } + } + } catch (Exception e) { + throw new RuntimeException(); + } + + return true; + } + + @Override + public List getAllMenu() { + //获取所有菜单 + Wrapper wrapper = new QueryWrapper() + .lambda() + .groupBy(Permission::getMenuCode) + .select() + ; + List menuList = permissionService.getEntityList(wrapper); + + if(V.notEmpty(menuList)){ + for(Permission menu : menuList){ + //获取一个菜单的所有权限资源 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().eq(Permission::getMenuCode, menu.getMenuCode()); + List allPermissionList = permissionService.getEntityList(queryWrapper); + menu.setPermissionList(allPermissionList); + } + } + + return menuList; + } @Override public List getRelatedRoleAndPermissionListByUser(String userType, Long userId) { diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java b/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java index ae8bec9..114b67f 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java @@ -1,5 +1,6 @@ package com.diboot.shiro.vo; +import com.baomidou.mybatisplus.annotation.TableField; import com.diboot.core.binding.annotation.BindEntityList; import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Role; @@ -18,7 +19,10 @@ public class RoleVO extends Role { private static final long serialVersionUID = 860775286174387052L; /**支持通过中间表的多-多Entity实体关联*/ - @BindEntityList(entity = Permission.class, condition="this.id=role_permission.role_id AND role_permission.permission_id=id") + @BindEntityList(entity = Permission.class, condition="this.id=role_permission.role_id AND role_permission.permission_id=id AND role_permission.deleted=0") private List permissionList; + @TableField(exist = false) + private List menuList; + } \ No newline at end of file From ff7d98d94218ef2d87cd08180e1884a9b0b22b82 Mon Sep 17 00:00:00 2001 From: wyldusername <2605214840@qq.com> Date: Wed, 17 Jul 2019 18:17:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3TASK#505=EF=BC=9A?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E7=AE=A1=E7=90=86=E5=88=97=E8=A1=A8=E9=A1=B5?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=95=B0=E6=8D=AE=E6=98=BE=E7=A4=BA=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diboot-components-msg/build.gradle | 1 + .../diboot/example/test/str/ConvertStrTest.java | 17 ----------------- diboot-shiro-wx-cp/build.gradle | 1 - diboot-shiro-wx-mp/build.gradle | 1 - .../shiro/service/impl/RoleServiceImpl.java | 9 ++++++--- .../main/java/com/diboot/shiro/vo/RoleVO.java | 4 ++++ 6 files changed, 11 insertions(+), 22 deletions(-) delete mode 100644 diboot-example/src/test/java/com/diboot/example/test/str/ConvertStrTest.java diff --git a/diboot-components-msg/build.gradle b/diboot-components-msg/build.gradle index b4c402d..c9b3a68 100644 --- a/diboot-components-msg/build.gradle +++ b/diboot-components-msg/build.gradle @@ -1,3 +1,4 @@ +apply plugin: 'org.springframework.boot' dependencies { compile project(":diboot-core") diff --git a/diboot-example/src/test/java/com/diboot/example/test/str/ConvertStrTest.java b/diboot-example/src/test/java/com/diboot/example/test/str/ConvertStrTest.java deleted file mode 100644 index 5186edd..0000000 --- a/diboot-example/src/test/java/com/diboot/example/test/str/ConvertStrTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.diboot.example.test.str; - -import com.diboot.core.service.impl.BaseServiceImpl; -import org.junit.Test; -import org.springframework.stereotype.Component; - -@Component -public class ConvertStrTest{ - - @Test - public void convert2Hump() throws Exception{ - String str = "aaa_bbb_ccc_ddd_eee_fff"; - BaseServiceImpl base = new BaseServiceImpl(); - String a = base.convert2Hump(str); - System.out.println(a); - } -} diff --git a/diboot-shiro-wx-cp/build.gradle b/diboot-shiro-wx-cp/build.gradle index 4c5bbab..1a5cc39 100644 --- a/diboot-shiro-wx-cp/build.gradle +++ b/diboot-shiro-wx-cp/build.gradle @@ -1,7 +1,6 @@ apply plugin: 'org.springframework.boot' dependencies { - compile project(":diboot-core") compile project(":diboot-shiro") // 微信开发组件 diff --git a/diboot-shiro-wx-mp/build.gradle b/diboot-shiro-wx-mp/build.gradle index d27fc78..9f85e0d 100755 --- a/diboot-shiro-wx-mp/build.gradle +++ b/diboot-shiro-wx-mp/build.gradle @@ -15,7 +15,6 @@ repositories { dependencies { - compile project(":diboot-core") compile project(":diboot-shiro") // 微信开发组件 diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java index 8eebded..c3c6200 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.service.impl.BaseServiceImpl; +import com.diboot.core.util.BeanUtils; import com.diboot.core.util.V; import com.diboot.core.vo.Pagination; import com.diboot.shiro.entity.Permission; @@ -58,9 +59,11 @@ public class RoleServiceImpl extends BaseServiceImpl implement HashSet menuSet = new HashSet(); Set idSet = new HashSet<>();//资源id set for(Permission permission : permissionList){ - idSet.add(permission.getId()); - if(menuSet.add(permission.getMenuCode())){ - menuList.add(permission); + //克隆roleVO.permissionList中的每一个permission,解决查出来的列表页数据重复的问题 + Permission temp = BeanUtils.convert(permission, Permission.class); + idSet.add(temp.getId()); + if(menuSet.add(temp.getMenuCode())){ + menuList.add(temp); } } //获取菜单资源下的该角色已有的权限资源 diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java b/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java index 114b67f..da2e052 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/vo/RoleVO.java @@ -1,6 +1,7 @@ package com.diboot.shiro.vo; import com.baomidou.mybatisplus.annotation.TableField; +import com.diboot.core.binding.annotation.BindDict; import com.diboot.core.binding.annotation.BindEntityList; import com.diboot.shiro.entity.Permission; import com.diboot.shiro.entity.Role; @@ -18,6 +19,9 @@ public class RoleVO extends Role { private static final long serialVersionUID = 860775286174387052L; + @BindDict(type="ROLE_STATUS", field="status") + private String statusLabel; + /**支持通过中间表的多-多Entity实体关联*/ @BindEntityList(entity = Permission.class, condition="this.id=role_permission.role_id AND role_permission.permission_id=id AND role_permission.deleted=0") private List permissionList; From ced3b6fc27c51e39ebcd8e796bbd680732b25d3a Mon Sep 17 00:00:00 2001 From: godchao Date: Wed, 17 Jul 2019 21:24:32 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/controller/DepartmentController.java | 1 - .../main/resources/application.properties.default | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) 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 39ba325..9153c90 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 @@ -14,7 +14,6 @@ import com.diboot.example.entity.Organization; import com.diboot.example.service.DepartmentService; import com.diboot.example.vo.DepartmentVO; import org.apache.shiro.authz.annotation.RequiresPermissions; -import org.apache.shiro.authz.annotation.RequiresRoles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; diff --git a/diboot-example/src/main/resources/application.properties.default b/diboot-example/src/main/resources/application.properties.default index 46d78b4..c9ff36f 100644 --- a/diboot-example/src/main/resources/application.properties.default +++ b/diboot-example/src/main/resources/application.properties.default @@ -11,13 +11,13 @@ spring.server.MaxFileSize=10MB spring.server.MaxRequestSize=50MB #文件本地存放路径 -files.storage.directory=C:/Users/wuweiqing/Downloads/upload/ +files.storage.directory= -#七牛 -qiniu.key.access=xJPWOwatadCt8ANWPaxrhj3Dj8DGIFoN9jD29SCP -qiniu.key.secret=IbWRTTXdOKoD8qdpug7Xm+rTXX2yhKuh8tfY6Ov4t1SDq/P1oT4cbREWnbgXofwt -qiniu.bucket.name=thirdparty -qiniu.image.domain=http://thirdparty.dibo.ltd/ +#七牛配置 +qiniu.key.access=xxx +qiniu.key.secret=xxx +qiniu.bucket.name=xxx +qiniu.image.domain=xxx # spring config spring.devtools.restart.enabled=true @@ -85,7 +85,7 @@ email.sender.host=smtp.163.com #发送端口 email.sender.sslport= #发送方称呼 -email.sender.name=wangyl +email.sender.name= #权限配置 From e7a11e67d6e4c61c75a25183a38b7edf38c94681 Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Thu, 18 Jul 2019 10:53:54 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Entity/EntityList=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E6=97=B6=E4=BD=BF=E7=94=A8=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=85=8B=E9=9A=86=E6=9B=BF=E4=BB=A3=E5=BC=95=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E7=BB=91=E5=AE=9A=E5=90=8E=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E9=9D=9E=E9=A2=84=E6=9C=9F=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diboot/core/binding/EntityBinder.java | 23 ++++++++++++++++-- .../diboot/core/binding/EntityListBinder.java | 4 ++-- .../com/diboot/core/util/SqlExecutor.java | 24 ++++++++++++------- .../core/test/binder/TestEntityBinder.java | 3 +++ .../test/binder/TestEntityListBinder.java | 9 +++++++ .../core/test/binder/TestFieldBinder.java | 3 +++ .../core/test/config/SpringMvcConfig.java | 5 ++-- .../core/test/service/BaseServiceTest.java | 3 +++ .../diboot/core/test/util/PropertiesTest.java | 2 +- .../diboot/example/test/ApplicationTest.java | 11 --------- .../example/test/sql/ExecuteSqlTest.java | 20 ---------------- 11 files changed, 60 insertions(+), 47 deletions(-) delete mode 100644 diboot-example/src/test/java/com/diboot/example/test/ApplicationTest.java delete mode 100644 diboot-example/src/test/java/com/diboot/example/test/sql/ExecuteSqlTest.java diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java index b40f1fb..e8c2b80 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java @@ -96,7 +96,7 @@ public class EntityBinder extends BaseBinder { } String key = entry.getKey(); T value = listMap.get(String.valueOf(fetchValueId)); - valueEntityMap.put(key, value); + valueEntityMap.put(key, cloneEntity(value)); } } } @@ -113,7 +113,7 @@ public class EntityBinder extends BaseBinder { String refEntityPKFieldName = S.toLowerCaseCamel(referencedEntityPrimaryKey); for(T entity : list){ String pkValue = BeanUtils.getStringProperty(entity, refEntityPKFieldName); - valueEntityMap.put(pkValue, entity); + valueEntityMap.put(pkValue, cloneEntity(entity)); } } } @@ -121,4 +121,23 @@ public class EntityBinder extends BaseBinder { BeanUtils.bindPropValueOfList(annoObjectField, annoObjectList, annoObjectForeignKey, valueEntityMap); } + /** + * 克隆对象 + * @param ent + * @param + * @return + */ + protected T cloneEntity(T ent){ + // 克隆对象 + try{ + T cloneEnt = (T)org.springframework.beans.BeanUtils.instantiateClass(ent.getClass()); + BeanUtils.copyProperties(ent ,cloneEnt); + return cloneEnt; + } + catch (Exception e){ + log.warn("Clone Object "+ent.getClass().getSimpleName()+" error", e); + return ent; + } + } + } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java index 4dcc30c..eae1109 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java @@ -69,7 +69,7 @@ public class EntityListBinder extends EntityBinder { for(Object obj : annoObjFKList){ T ent = entityMap.get(String.valueOf(obj)); if(ent != null){ - valueList.add(ent); + valueList.add(cloneEntity(ent)); } } valueEntityListMap.put(entry.getKey(), valueList); @@ -89,7 +89,7 @@ public class EntityListBinder extends EntityBinder { entityList = new ArrayList<>(); valueEntityListMap.put(keyValue, entityList); } - entityList.add(entity); + entityList.add(cloneEntity(entity)); } } } diff --git a/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java b/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java index 7abd108..9a9de18 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java +++ b/diboot-core/src/main/java/com/diboot/core/util/SqlExecutor.java @@ -33,13 +33,14 @@ public class SqlExecutor { log.warn("无法获取SqlSessionFactory实例,SQL将不被执行。"); return null; } + log.debug("==> SQL: "+sql); // 替换单个?参数为多个,用于拼接IN参数 if(V.notEmpty(params)){ + log.debug("==> Params: {}", JSON.stringify(params)); if(params.size() > 2000){ log.warn("查询参数集合数量过多, size={},请检查调用是否合理!", params.size()); } } - log.debug("==> SQL: "+sql); try(SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)){ if(V.notEmpty(params)){ for(int i=0; i 2000){ - log.warn("SQL语句参数集合数量过多,size={},请检查调用是否合理!", params.size()); + log.debug("==> SQL: "+sql); + // 替换单个?参数为多个,用于拼接IN参数 + if(V.notEmpty(params)){ + log.debug("==> Params: {}", JSON.stringify(params)); + if(params.size() > 2000){ + log.warn("更新参数集合数量过多, size={},请检查调用是否合理!", params.size()); } } - log.debug("==> SQL:" + sql); try(SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)){ if (V.notEmpty(params)){ for (int i=0; i Date: Thu, 18 Jul 2019 12:49:13 +0800 Subject: [PATCH 5/5] =?UTF-8?q?1.=20=E6=B3=A8=E8=A7=A3=E5=85=B3=E7=B3=BB?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E6=94=AF=E6=8C=81=E7=BB=A7=E6=89=BFIService?= =?UTF-8?q?=EF=BC=88=E9=9D=9EBaseService=EF=BC=89=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E7=BB=91=E5=AE=9A=E3=80=822.=20=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=85=A5=E5=8F=A3=E7=B1=BB=E6=9B=B4=E5=90=8D=E4=B8=BARelations?= =?UTF-8?q?Binder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msg/controller/MessageController.java | 4 +- diboot-core/README.md | 4 +- .../com/diboot/core/binding/BaseBinder.java | 53 +++- .../com/diboot/core/binding/EntityBinder.java | 7 +- .../diboot/core/binding/EntityListBinder.java | 7 +- .../com/diboot/core/binding/FieldBinder.java | 5 +- .../manager/AnnotationBindingManager.java | 180 +----------- .../core/binding/manager/RelationsBinder.java | 264 ++++++++++++++++++ .../controller/BaseCrudRestController.java | 6 +- .../handle/DefaultExceptionAdviceHandler.java | 2 - .../core/service/impl/BaseServiceImpl.java | 26 +- .../com/diboot/core/util/ContextHelper.java | 10 +- .../core/test/binder/TestEntityBinder.java | 7 +- .../test/binder/TestEntityListBinder.java | 9 +- .../core/test/binder/TestFieldBinder.java | 7 +- .../core/test/binder/service/RoleService.java | 3 +- .../core/test/binder/service/UserService.java | 3 +- .../binder/service/impl/RoleServiceImpl.java | 3 +- .../binder/service/impl/UserServiceImpl.java | 3 +- .../controller/DictionaryController.java | 4 +- .../example/controller/SysUserController.java | 4 +- .../service/impl/DictionaryServiceImpl.java | 2 - .../service/impl/SysUserServiceImpl.java | 4 +- .../shiro/service/impl/RoleServiceImpl.java | 4 +- 24 files changed, 391 insertions(+), 230 deletions(-) create mode 100644 diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java 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 dcb9404..471f03a 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.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.service.BaseService; import com.diboot.core.vo.JsonResult; @@ -39,7 +39,7 @@ public class MessageController extends BaseCrudRestController { // 查询当前页的Entity主表数据 List entityList = getService().getEntityList(queryWrapper, pagination); // 自动转换VO中注解绑定的关联 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, MessageVO.class); + List voList = RelationsBinder.convertAndBind(entityList, MessageVO.class); //返回结果 return new JsonResult(Status.OK, voList).bindPagination(pagination); } diff --git a/diboot-core/README.md b/diboot-core/README.md index d1bbb5b..d860c39 100644 --- a/diboot-core/README.md +++ b/diboot-core/README.md @@ -48,14 +48,14 @@ private List roleList; ~~~java // 调用AnnotationBindingManager自动绑定注解相关关联 //List voList = ...; -AnnotationBindingManager.autoBind(voList); +RelationsBinder.bind(voList); ~~~ #### 2. 自动转型并绑定关联(需要转型) ~~~java // 获取Entity列表 List entityList = userService.getEntityList(queryWrapper); // 调用AnnotationBindingManager自动绑定注解相关关联 -List voList = AnnotationBindingManager.autoConvertAndBind(userList, MyUserVO.class); +List voList = RelationsBinder.convertAndBind(userList, MyUserVO.class); ~~~ diff --git a/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java index f117190..0dd82c5 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/BaseBinder.java @@ -1,16 +1,23 @@ package com.diboot.core.binding; +import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.parser.MiddleTable; +import com.diboot.core.config.BaseConfig; import com.diboot.core.service.BaseService; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.IGetter; import com.diboot.core.util.S; +import com.diboot.core.vo.Pagination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; /** * 关系绑定Binder父类 @@ -31,7 +38,7 @@ public abstract class BaseBinder { /** * 被关联对象的Service实例 */ - protected BaseService referencedService; + protected IService referencedService; /*** * DO对象中的主键属性名 */ @@ -141,4 +148,48 @@ public abstract class BaseBinder { */ public abstract void bind(); + /** + * 获取EntityList + * @param queryWrapper + * @return + */ + protected List getEntityList(Wrapper queryWrapper) { + if(referencedService instanceof BaseService){ + return ((BaseService)referencedService).getEntityList(queryWrapper, null); + } + else{ + List list = referencedService.list(queryWrapper); + return checkedList(list); + } + } + + /** + * 获取Map结果 + * @param queryWrapper + * @return + */ + protected List> getMapList(Wrapper queryWrapper) { + if(referencedService instanceof BaseService){ + return ((BaseService)referencedService).getMapList(queryWrapper); + } + else{ + List> list = referencedService.listMaps(queryWrapper); + return checkedList(list); + } + } + + /** + * 检查list,结果过多打印warn + * @param list + * @return + */ + private List checkedList(List list){ + if(list == null){ + list = Collections.emptyList(); + } + else if(list.size() > BaseConfig.getBatchSize()){ + log.warn("单次查询记录数量过大,返回结果数={},请检查!", list.size()); + } + return list; + } } \ No newline at end of file diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java index e8c2b80..cdfce0d 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import com.diboot.core.util.*; import org.slf4j.Logger; @@ -31,7 +32,7 @@ public class EntityBinder extends BaseBinder { * @param referencedService * @param voList */ - public EntityBinder(BaseService referencedService, List voList){ + public EntityBinder(IService referencedService, List voList){ this.referencedService = referencedService; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -85,7 +86,7 @@ public class EntityBinder extends BaseBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), middleTableColumnValueList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ // 转换entity列表为Map Map listMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey)); @@ -108,7 +109,7 @@ public class EntityBinder extends BaseBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ String refEntityPKFieldName = S.toLowerCaseCamel(referencedEntityPrimaryKey); for(T entity : list){ diff --git a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java index eae1109..ed84c8f 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/EntityListBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.S; @@ -25,7 +26,7 @@ public class EntityListBinder extends EntityBinder { * @param serviceInstance * @param voList */ - public EntityListBinder(BaseService serviceInstance, List voList){ + public EntityListBinder(IService serviceInstance, List voList){ this.referencedService = serviceInstance; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -56,7 +57,7 @@ public class EntityListBinder extends EntityBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), entityIdList); // 查询entity列表: List - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); // 转换entity列表为Map Map entityMap = BeanUtils.convertToStringKeyObjectMap(list, S.toLowerCaseCamel(referencedEntityPrimaryKey)); for(Map.Entry entry : middleTableResultMap.entrySet()){ @@ -80,7 +81,7 @@ public class EntityListBinder extends EntityBinder { // 构建查询条件 queryWrapper.in(S.toSnakeCase(referencedEntityPrimaryKey), annoObjectForeignKeyList); // 查询entity列表 - List list = referencedService.getEntityList(queryWrapper); + List list = getEntityList(queryWrapper); if(V.notEmpty(list)){ for(T entity : list){ String keyValue = BeanUtils.getStringProperty(entity, S.toLowerCaseCamel(referencedEntityPrimaryKey)); diff --git a/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java index d8a73f3..55b2331 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/FieldBinder.java @@ -1,6 +1,7 @@ package com.diboot.core.binding; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.annotation.BindField; import com.diboot.core.service.BaseService; import com.diboot.core.util.*; @@ -33,7 +34,7 @@ public class FieldBinder extends BaseBinder { * @param serviceInstance * @param voList */ - public FieldBinder(BaseService serviceInstance, List voList){ + public FieldBinder(IService serviceInstance, List voList){ this.referencedService = serviceInstance; this.annoObjectList = voList; this.queryWrapper = new QueryWrapper(); @@ -113,7 +114,7 @@ public class FieldBinder extends BaseBinder { } // 获取匹配结果的mapList - List> mapList = referencedService.getMapList(queryWrapper); + List> mapList = getMapList(queryWrapper); if(V.isEmpty(mapList)){ return; } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java b/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java index 685b37d..03a79cb 100644 --- a/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java +++ b/diboot-core/src/main/java/com/diboot/core/binding/manager/AnnotationBindingManager.java @@ -1,6 +1,9 @@ package com.diboot.core.binding.manager; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.binding.BaseBinder; +import com.diboot.core.binding.EntityBinder; +import com.diboot.core.binding.EntityListBinder; import com.diboot.core.binding.FieldBinder; import com.diboot.core.binding.annotation.BindEntity; import com.diboot.core.binding.annotation.BindEntityList; @@ -25,11 +28,13 @@ import java.util.HashMap; import java.util.List; /** - * 绑定管理器 + * 绑定管理器 (已废弃,请调用RelationsBinder) * @author Mazhicheng * @version v2.0 * @date 2019/3/30 + * @see com.diboot.core.binding.manager.RelationsBinder */ +@Deprecated public class AnnotationBindingManager { private static final Logger log = LoggerFactory.getLogger(AnnotationBindingManager.class); @@ -42,11 +47,7 @@ public class AnnotationBindingManager { * @return */ public static List autoConvertAndBind(List entityList, Class voClass){ - // 转换为VO列表 - List voList = BeanUtils.convertList(entityList, voClass); - // 自动绑定关联对象 - autoBind(voList); - return voList; + return RelationsBinder.convertAndBind(entityList, voClass); } /** @@ -55,172 +56,7 @@ public class AnnotationBindingManager { * @throws Exception */ public static void autoBind(List voList){ - if(V.isEmpty(voList)){ - return; - } - // 获取VO类 - Class voClass = voList.get(0).getClass(); - BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass); - if(bindAnnotationGroup.isNotEmpty()){ - // 绑定数据字典 - List dictAnnoList = bindAnnotationGroup.getBindDictAnnotations(); - if(dictAnnoList != null){ - for(FieldAnnotation annotation : dictAnnoList){ - doBindingDict(voList, annotation); - } - } - // 绑定Field字段名 - List fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations(); - if(fieldAnnoList != null){ - doBindingField(voList, fieldAnnoList); - } - // 绑定Entity实体 - List entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations(); - if(entityAnnoList != null){ - for(FieldAnnotation anno : entityAnnoList){ - doBindingEntity(voList, anno); - } - } - // 绑定Entity实体List - List entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations(); - if(entitiesAnnoList != null){ - for(FieldAnnotation anno : entitiesAnnoList){ - doBindingEntityList(voList, anno); - } - } - } - } - - /*** - * 绑定数据字典 - * @param voList - * @param fieldAnno - * @param - */ - private static void doBindingDict(List voList, FieldAnnotation fieldAnno) { - DictionaryService dictionaryService = (DictionaryService) ContextHelper.getBean(DictionaryService.class); - if(dictionaryService != null){ - BindDict annotation = (BindDict) fieldAnno.getAnnotation(); - dictionaryService.bindItemLabel(voList, fieldAnno.getFieldName(), annotation.field(), annotation.type()); - } - } - - /*** - * 绑定字段 - * @param voList - * @param fieldAnnoList - * @param - */ - private static void doBindingField(List voList, List fieldAnnoList) { - //多个字段,合并查询,以减少SQL数 - Map> clazzToListMap = new HashMap<>(); - for(FieldAnnotation anno : fieldAnnoList){ - BindField bindField = (BindField) anno.getAnnotation(); - String key = bindField.entity().getName() + ":" + bindField.condition(); - List list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>()); - list.add(anno); - } - // 解析条件并且执行绑定 - for(Map.Entry> entry : clazzToListMap.entrySet()){ - List list = entry.getValue(); - BindField bindAnnotation = (BindField) list.get(0).getAnnotation(); - BaseService service = getService(bindAnnotation); - FieldBinder binder = service.bindingFieldTo(voList); - for(FieldAnnotation anno : list){ - BindField bindField = (BindField) anno.getAnnotation(); - binder.link(bindField.field(), anno.getFieldName()); - } - parseConditionsAndBinding(binder, bindAnnotation.condition()); - } - } - - /*** - * 绑定Entity - * @param voList - * @param fieldAnnotation - * @param - */ - private static void doBindingEntity(List voList, FieldAnnotation fieldAnnotation) { - BindEntity annotation = (BindEntity) fieldAnnotation.getAnnotation(); - // 绑定关联对象entity - BaseService service = getService(annotation); - if(service != null){ - // 字段名 - String voFieldName = fieldAnnotation.getFieldName(); - // 构建binder - BaseBinder binder = service.bindingEntityTo(voList).set(voFieldName); - // 解析条件并且执行绑定 - parseConditionsAndBinding(binder, annotation.condition()); - } - } - - /*** - * 绑定Entity - * @param voList - * @param fieldAnnotation - * @param - */ - private static void doBindingEntityList(List voList, FieldAnnotation fieldAnnotation) { - BindEntityList bindAnnotation = (BindEntityList) fieldAnnotation.getAnnotation(); - // 绑定关联对象entity - BaseService service = getService(bindAnnotation); - if(service != null){ - // 字段名 - String voFieldName = fieldAnnotation.getFieldName(); - // 构建binder - BaseBinder binder = service.bindingEntityListTo(voList).set(voFieldName); - // 解析条件并且执行绑定 - parseConditionsAndBinding(binder, bindAnnotation.condition()); - } - } - - /*** - * 解析条件并且执行绑定 - * @param condition - * @param binder - */ - private static void parseConditionsAndBinding(BaseBinder binder, String condition){ - try{ - ConditionManager.parseConditions(condition, binder); - binder.bind(); - } - catch (Exception e){ - log.error("解析注解条件与绑定执行异常", e); - } - } - - /** - * 通过Entity获取对应的Service实现类 - * @param annotation - * @return - */ - private static BaseService getService(Annotation annotation){ - Class entityClass = null; - if(annotation instanceof BindDict){ - entityClass = Dictionary.class; - } - else if(annotation instanceof BindField){ - BindField bindAnnotation = (BindField)annotation; - entityClass = bindAnnotation.entity(); - } - else if(annotation instanceof BindEntity){ - BindEntity bindAnnotation = (BindEntity)annotation; - entityClass = bindAnnotation.entity(); - } - else if(annotation instanceof BindEntityList){ - BindEntityList bindAnnotation = (BindEntityList)annotation; - entityClass = bindAnnotation.entity(); - } - else{ - log.warn("非预期的注解: "+ annotation.getClass().getSimpleName()); - return null; - } - // 根据entity获取Service - BaseService service = ContextHelper.getServiceByEntity(entityClass); - if(service == null){ - log.error("未能识别到Entity: "+entityClass.getName()+" 的Service实现!"); - } - return service; + RelationsBinder.bind(voList); } } diff --git a/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java b/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java new file mode 100644 index 0000000..50793bc --- /dev/null +++ b/diboot-core/src/main/java/com/diboot/core/binding/manager/RelationsBinder.java @@ -0,0 +1,264 @@ +package com.diboot.core.binding.manager; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.diboot.core.binding.BaseBinder; +import com.diboot.core.binding.EntityBinder; +import com.diboot.core.binding.EntityListBinder; +import com.diboot.core.binding.FieldBinder; +import com.diboot.core.binding.annotation.BindDict; +import com.diboot.core.binding.annotation.BindEntity; +import com.diboot.core.binding.annotation.BindEntityList; +import com.diboot.core.binding.annotation.BindField; +import com.diboot.core.binding.parser.BindAnnotationGroup; +import com.diboot.core.binding.parser.ConditionManager; +import com.diboot.core.binding.parser.FieldAnnotation; +import com.diboot.core.entity.Dictionary; +import com.diboot.core.service.DictionaryService; +import com.diboot.core.util.BeanUtils; +import com.diboot.core.util.ContextHelper; +import com.diboot.core.util.V; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 绑定管理器 + * @author Mazhicheng + * @version v2.0 + * @date 2019/7/18 + */ +public class RelationsBinder { + private static final Logger log = LoggerFactory.getLogger(RelationsBinder.class); + + /** + * 自动转换和绑定VO中的注解关联 + * @param entityList + * @param voClass + * @param + * @param + * @return + */ + public static List convertAndBind(List entityList, Class voClass){ + // 转换为VO列表 + List voList = BeanUtils.convertList(entityList, voClass); + // 自动绑定关联对象 + bind(voList); + return voList; + } + + /** + * 自动绑定关联对象 + * @return + * @throws Exception + */ + public static void bind(List voList){ + if(V.isEmpty(voList)){ + return; + } + // 获取VO类 + Class voClass = voList.get(0).getClass(); + BindAnnotationGroup bindAnnotationGroup = BindAnnotationCacheManager.getBindAnnotationGroup(voClass); + if(bindAnnotationGroup.isNotEmpty()){ + // 绑定数据字典 + List dictAnnoList = bindAnnotationGroup.getBindDictAnnotations(); + if(dictAnnoList != null){ + for(FieldAnnotation annotation : dictAnnoList){ + doBindingDict(voList, annotation); + } + } + // 绑定Field字段名 + List fieldAnnoList = bindAnnotationGroup.getBindFieldAnnotations(); + if(fieldAnnoList != null){ + doBindingField(voList, fieldAnnoList); + } + // 绑定Entity实体 + List entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations(); + if(entityAnnoList != null){ + for(FieldAnnotation anno : entityAnnoList){ + doBindingEntity(voList, anno); + } + } + // 绑定Entity实体List + List entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations(); + if(entitiesAnnoList != null){ + for(FieldAnnotation anno : entitiesAnnoList){ + doBindingEntityList(voList, anno); + } + } + } + } + + /*** + * 绑定数据字典 + * @param voList + * @param fieldAnno + * @param + */ + private static void doBindingDict(List voList, FieldAnnotation fieldAnno) { + DictionaryService dictionaryService = (DictionaryService) ContextHelper.getBean(DictionaryService.class); + if(dictionaryService != null){ + BindDict annotation = (BindDict) fieldAnno.getAnnotation(); + dictionaryService.bindItemLabel(voList, fieldAnno.getFieldName(), annotation.field(), annotation.type()); + } + } + + /*** + * 绑定字段 + * @param voList + * @param fieldAnnoList + * @param + */ + private static void doBindingField(List voList, List fieldAnnoList) { + //多个字段,合并查询,以减少SQL数 + Map> clazzToListMap = new HashMap<>(); + for(FieldAnnotation anno : fieldAnnoList){ + BindField bindField = (BindField) anno.getAnnotation(); + String key = bindField.entity().getName() + ":" + bindField.condition(); + List list = clazzToListMap.computeIfAbsent(key, k -> new ArrayList<>()); + list.add(anno); + } + // 解析条件并且执行绑定 + for(Map.Entry> entry : clazzToListMap.entrySet()){ + List list = entry.getValue(); + BindField bindAnnotation = (BindField) list.get(0).getAnnotation(); + FieldBinder binder = buildFieldBinder(bindAnnotation, voList); + for(FieldAnnotation anno : list){ + BindField bindField = (BindField) anno.getAnnotation(); + binder.link(bindField.field(), anno.getFieldName()); + } + parseConditionsAndBinding(binder, bindAnnotation.condition()); + } + } + + /*** + * 绑定Entity + * @param voList + * @param fieldAnnotation + * @param + */ + private static void doBindingEntity(List voList, FieldAnnotation fieldAnnotation) { + BindEntity annotation = (BindEntity) fieldAnnotation.getAnnotation(); + // 绑定关联对象entity + EntityBinder binder = buildEntityBinder(annotation, voList); + if(binder != null){ + // 构建binder + binder.set(fieldAnnotation.getFieldName()); + // 解析条件并且执行绑定 + parseConditionsAndBinding(binder, annotation.condition()); + } + } + + /*** + * 绑定Entity + * @param voList + * @param fieldAnnotation + * @param + */ + private static void doBindingEntityList(List voList, FieldAnnotation fieldAnnotation) { + BindEntityList bindAnnotation = (BindEntityList) fieldAnnotation.getAnnotation(); + // 构建binder + EntityListBinder binder = buildEntityListBinder(bindAnnotation, voList); + if(binder != null){ + binder.set(fieldAnnotation.getFieldName()); + // 解析条件并且执行绑定 + parseConditionsAndBinding(binder, bindAnnotation.condition()); + } + } + + /*** + * 解析条件并且执行绑定 + * @param condition + * @param binder + */ + private static void parseConditionsAndBinding(BaseBinder binder, String condition){ + try{ + ConditionManager.parseConditions(condition, binder); + binder.bind(); + } + catch (Exception e){ + log.error("解析注解条件与绑定执行异常", e); + } + } + + /** + * 构建FieldBinder + * @param annotation + * @param voList + * @return + */ + private static FieldBinder buildFieldBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new FieldBinder<>(service, voList); + } + return null; + } + + /** + * 构建EntityBinder + * @param annotation + * @param voList + * @return + */ + private static EntityBinder buildEntityBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new EntityBinder<>(service, voList); + } + return null; + } + + /** + * 构建EntityListBinder + * @param annotation + * @param voList + * @return + */ + private static EntityListBinder buildEntityListBinder(Annotation annotation, List voList){ + IService service = getService(annotation); + if(service != null){ + return new EntityListBinder<>(service, voList); + } + return null; + } + + /** + * 通过Entity获取对应的Service实现类 + * @param annotation + * @return + */ + private static IService getService(Annotation annotation){ + Class entityClass = null; + if(annotation instanceof BindDict){ + entityClass = Dictionary.class; + } + else if(annotation instanceof BindField){ + BindField bindAnnotation = (BindField)annotation; + entityClass = bindAnnotation.entity(); + } + else if(annotation instanceof BindEntity){ + BindEntity bindAnnotation = (BindEntity)annotation; + entityClass = bindAnnotation.entity(); + } + else if(annotation instanceof BindEntityList){ + BindEntityList bindAnnotation = (BindEntityList)annotation; + entityClass = bindAnnotation.entity(); + } + else{ + log.warn("非预期的注解: "+ annotation.getClass().getSimpleName()); + return null; + } + // 根据entity获取Service + IService service = ContextHelper.getServiceByEntity(entityClass); + if(service == null){ + log.error("未能识别到Entity: "+entityClass.getName()+" 的Service实现!"); + } + return service; + } + +} 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 2c1dd87..d2fc7f8 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 @@ -1,11 +1,9 @@ package com.diboot.core.controller; import com.baomidou.mybatisplus.core.conditions.Wrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.entity.BaseEntity; import com.diboot.core.service.BaseService; -import com.diboot.core.util.BeanUtils; -import com.diboot.core.util.V; import com.diboot.core.vo.JsonResult; import com.diboot.core.vo.Status; import com.diboot.core.vo.Pagination; @@ -202,7 +200,7 @@ public abstract class BaseCrudRestController extends BaseController { */ protected List convertToVoAndBindRelations(List entityList, Class voClass){ // 转换为VO - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass); + List voList = RelationsBinder.convertAndBind(entityList, voClass); return voList; } diff --git a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java index 914565c..d6f6279 100644 --- a/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java +++ b/diboot-core/src/main/java/com/diboot/core/handle/DefaultExceptionAdviceHandler.java @@ -32,7 +32,6 @@ import org.springframework.web.servlet.ModelAndView; */ @ControllerAdvice public class DefaultExceptionAdviceHandler { - private final static Logger log = LoggerFactory.getLogger(ExceptionHandler.class); @Autowired @@ -71,6 +70,5 @@ public class DefaultExceptionAdviceHandler { return new ModelAndView("redirect:" + errorUrl); } return new ModelAndView(); - } } diff --git a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java index 55a8c1f..4f50380 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java +++ b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java @@ -3,12 +3,13 @@ package com.diboot.core.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.binding.EntityBinder; import com.diboot.core.binding.EntityListBinder; import com.diboot.core.binding.FieldBinder; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.config.BaseConfig; import com.diboot.core.config.Cons; import com.diboot.core.mapper.BaseCrudMapper; @@ -247,7 +248,7 @@ public class BaseServiceImpl, T> extends ServiceImpl List enityList = new ArrayList<>(); enityList.add(entity); // 绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(enityList, voClass); + List voList = RelationsBinder.convertAndBind(enityList, voClass); return voList.get(0); } @@ -255,7 +256,7 @@ public class BaseServiceImpl, T> extends ServiceImpl public List getViewObjectList(Wrapper queryWrapper, Pagination pagination, Class voClass) { List entityList = getEntityList(queryWrapper, pagination); // 自动转换为VO并绑定关联对象 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, voClass); + List voList = RelationsBinder.convertAndBind(entityList, voClass); return voList; } @@ -264,17 +265,26 @@ public class BaseServiceImpl, T> extends ServiceImpl * @param pagination * @return */ - protected IPage convertToIPage(Pagination pagination){ + protected Page convertToIPage(Pagination pagination){ if(pagination == null){ return null; } - IPage page = new Page() + Page page = new Page() .setCurrent(pagination.getPageIndex()) .setSize(pagination.getPageSize()) // 如果前端传递过来了缓存的总数,则本次不再count统计 - .setTotal(pagination.getTotalCount() > 0? -1 : pagination.getTotalCount()) - .setAscs(S.toSnakeCase(pagination.getAscList())) - .setDescs(S.toSnakeCase(pagination.getDescList())); + .setTotal(pagination.getTotalCount() > 0? -1 : pagination.getTotalCount()); + // 排序 + if(V.notEmpty(pagination.getAscList())){ + pagination.getAscList().forEach(s -> { + page.addOrder(OrderItem.asc(s)); + }); + } + if(V.notEmpty(pagination.getDescList())){ + pagination.getDescList().forEach(s -> { + page.addOrder(OrderItem.desc(s)); + }); + } return page; } diff --git a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java index 89909fb..c1ce614 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java +++ b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java @@ -1,5 +1,7 @@ package com.diboot.core.util; +import com.baomidou.mybatisplus.extension.service.IService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.BaseService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,7 +37,7 @@ public class ContextHelper implements ApplicationContextAware { /** * Entity-对应的Mapper缓存 */ - private static Map entityToMapperCacheMap = new ConcurrentHashMap<>(); + private static Map entityToMapperCacheMap = new ConcurrentHashMap<>(); @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -106,11 +108,11 @@ public class ContextHelper implements ApplicationContextAware { * @param entity * @return */ - public static BaseService getServiceByEntity(Class entity){ + public static IService getServiceByEntity(Class entity){ if(entityToMapperCacheMap.isEmpty()){ - Map serviceMap = getApplicationContext().getBeansOfType(BaseService.class); + Map serviceMap = getApplicationContext().getBeansOfType(IService.class); if(V.notEmpty(serviceMap)){ - for(Map.Entry entry : serviceMap.entrySet()){ + for(Map.Entry entry : serviceMap.entrySet()){ String entityClassName = getEntityClassByServiceImpl(entry.getValue().getClass()); if(V.notEmpty(entityClassName)){ entityToMapperCacheMap.put(entityClassName, entry.getValue()); diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java index 954702d..c62d398 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityBinder.java @@ -1,14 +1,13 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; import diboot.core.test.StartupApplication; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.service.UserService; import diboot.core.test.binder.vo.EntityBinderVO; -import diboot.core.test.binder.vo.FieldBinderVO; import diboot.core.test.config.SpringMvcConfig; import org.junit.Assert; import org.junit.Test; @@ -39,9 +38,9 @@ public class TestEntityBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, EntityBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java index c9e2475..75b42e6 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestEntityListBinder.java @@ -1,8 +1,7 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; import diboot.core.test.StartupApplication; @@ -51,7 +50,7 @@ public class TestEntityListBinder { queryWrapper.eq(Department::getId, 10001L); List entityList = departmentService.getEntityList(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(entityList, EntityListSimpleBinderVO.class); + List voList = RelationsBinder.convertAndBind(entityList, EntityListSimpleBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityListSimpleBinderVO vo : voList){ @@ -76,9 +75,9 @@ public class TestEntityListBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, EntityListComplexBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, EntityListComplexBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(EntityListComplexBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java b/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java index 584617e..33f38c1 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/TestFieldBinder.java @@ -1,10 +1,9 @@ package diboot.core.test.binder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.util.JSON; import com.diboot.core.util.V; -import com.diboot.core.vo.Pagination; import diboot.core.test.StartupApplication; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.service.UserService; @@ -39,9 +38,9 @@ public class TestFieldBinder { // 加载测试数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(User::getId, 1001L, 1002L); - List userList = userService.getEntityList(queryWrapper); + List userList = userService.list(queryWrapper); // 自动绑定 - List voList = AnnotationBindingManager.autoConvertAndBind(userList, FieldBinderVO.class); + List voList = RelationsBinder.convertAndBind(userList, FieldBinderVO.class); // 验证绑定结果 if(V.notEmpty(voList)){ for(FieldBinderVO vo : voList){ diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java b/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java index 5aca866..bd471b3 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/RoleService.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import diboot.core.test.binder.entity.Role; import diboot.core.test.binder.entity.User; @@ -10,6 +11,6 @@ import diboot.core.test.binder.entity.User; * @version v2.0 * @date 2019/1/5 */ -public interface RoleService extends BaseService { +public interface RoleService extends IService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java b/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java index 8ccb7b3..48045c5 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/UserService.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service; +import com.baomidou.mybatisplus.extension.service.IService; import com.diboot.core.service.BaseService; import diboot.core.test.binder.entity.User; @@ -9,6 +10,6 @@ import diboot.core.test.binder.entity.User; * @version v2.0 * @date 2019/1/5 */ -public interface UserService extends BaseService { +public interface UserService extends IService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java index 3ec8977..573708d 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/RoleServiceImpl.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service.impl; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.impl.BaseServiceImpl; import diboot.core.test.binder.entity.Role; import diboot.core.test.binder.mapper.RoleMapper; @@ -13,6 +14,6 @@ import org.springframework.stereotype.Service; * Copyright © www.dibo.ltd */ @Service -public class RoleServiceImpl extends BaseServiceImpl implements RoleService { +public class RoleServiceImpl extends ServiceImpl implements RoleService { } diff --git a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java index 78d924e..1f31686 100644 --- a/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java +++ b/diboot-core/src/test/java/diboot/core/test/binder/service/impl/UserServiceImpl.java @@ -1,5 +1,6 @@ package diboot.core.test.binder.service.impl; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.diboot.core.service.impl.BaseServiceImpl; import diboot.core.test.binder.entity.User; import diboot.core.test.binder.mapper.UserMapper; @@ -13,6 +14,6 @@ import org.springframework.stereotype.Service; * Copyright © www.dibo.ltd */ @Service -public class UserServiceImpl extends BaseServiceImpl implements UserService { +public class UserServiceImpl extends ServiceImpl implements UserService { } diff --git a/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java b/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java index f6e9cec..d9cce1e 100644 --- a/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java +++ b/diboot-example/src/main/java/com/diboot/example/controller/DictionaryController.java @@ -1,7 +1,7 @@ package com.diboot.example.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.entity.Dictionary; import com.diboot.core.service.BaseService; @@ -47,7 +47,7 @@ public class DictionaryController extends BaseCrudRestController { //获取实体list List dictionaryList = dictionaryService.getEntityList(queryWrapper, pagination); //筛选出在列表页展示的字段 - List dicVoList = AnnotationBindingManager.autoConvertAndBind(dictionaryList, DictionaryListVO.class); + List dicVoList = RelationsBinder.convertAndBind(dictionaryList, DictionaryListVO.class); //返回结果 return new JsonResult(Status.OK, dicVoList).bindPagination(pagination); } 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 8a22f29..df934fc 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 @@ -2,7 +2,7 @@ package com.diboot.example.controller; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.controller.BaseCrudRestController; import com.diboot.core.service.BaseService; import com.diboot.core.service.DictionaryService; @@ -57,7 +57,7 @@ public class SysUserController extends BaseCrudRestController { // 查询当前页的Entity主表数据 List voList = sysUserService.getSysUserList(queryWrapper, pagination); //筛选出在列表页展示的字段 - List userVoList = AnnotationBindingManager.autoConvertAndBind(voList, SysUserListVO.class); + List userVoList = RelationsBinder.convertAndBind(voList, SysUserListVO.class); // 返回结果 return new JsonResult(Status.OK, userVoList).bindPagination(pagination); } diff --git a/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java b/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java index 511f386..572f9a0 100644 --- a/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java +++ b/diboot-example/src/main/java/com/diboot/example/service/impl/DictionaryServiceImpl.java @@ -1,7 +1,6 @@ package com.diboot.example.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; import com.diboot.core.entity.Dictionary; import com.diboot.core.mapper.DictionaryMapper; import com.diboot.core.service.impl.BaseServiceImpl; @@ -15,7 +14,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; /** 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 280cbc1..ae628c1 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 @@ -2,7 +2,7 @@ package com.diboot.example.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.util.V; import com.diboot.core.vo.Pagination; @@ -40,7 +40,7 @@ public class SysUserServiceImpl extends BaseServiceImpl @Override public List getSysUserList(Wrapper queryWrapper, Pagination pagination) { List sysUserList = super.getEntityList(queryWrapper, pagination); - List sysUserVOList = AnnotationBindingManager.autoConvertAndBind(sysUserList, SysUserVO.class); + List sysUserVOList = RelationsBinder.convertAndBind(sysUserList, SysUserVO.class); if(V.notEmpty(sysUserVOList)){ for(SysUserVO sysUserVO : sysUserVOList){ List roleList = sysUserVO.getRoleList(); diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java index c3c6200..3f64952 100644 --- a/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java +++ b/diboot-shiro/src/main/java/com/diboot/shiro/service/impl/RoleServiceImpl.java @@ -2,7 +2,7 @@ package com.diboot.shiro.service.impl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.diboot.core.binding.manager.AnnotationBindingManager; +import com.diboot.core.binding.manager.RelationsBinder; import com.diboot.core.service.impl.BaseServiceImpl; import com.diboot.core.util.BeanUtils; import com.diboot.core.util.V; @@ -49,7 +49,7 @@ public class RoleServiceImpl extends BaseServiceImpl implement @Override public List getRoleList(Wrapper queryWrapper, Pagination pagination) { List roleList = super.getEntityList(queryWrapper, pagination); - List roleVOList = AnnotationBindingManager.autoConvertAndBind(roleList, RoleVO.class); + List roleVOList = RelationsBinder.convertAndBind(roleList, RoleVO.class); if(V.notEmpty(roleVOList)){ for(RoleVO roleVO : roleVOList){ List permissionList = roleVO.getPermissionList();