motorSystem.iml
@@ -26,7 +26,6 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" exported="" name="lib" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.12.RELEASE" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.12.RELEASE" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.12.RELEASE" level="project" /> src/main/java/com/whyc/config/MybatisPlusConfig.java
@@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.yckj.mapper") @MapperScan("com.whyc.mapper") public class MybatisPlusConfig { @Bean src/main/java/com/whyc/config/WebConfig.java
File was deleted src/main/java/com/whyc/controller/PrivilegeController.java
New file @@ -0,0 +1,65 @@ package com.whyc.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.whyc.pojo.Privilege; import com.whyc.pojo.User; import com.whyc.service.PrivilegeService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.List; @RestController @RequestMapping("privilege") @Api(tags = "权限") @Slf4j public class PrivilegeController { @Resource private PrivilegeService privilegeService; @PostMapping @ApiOperation(value = "添加") public boolean add(@RequestBody Privilege privilege){ return privilegeService.add(privilege); } @PostMapping("/batch") @ApiOperation(value = "添加批量") public boolean addBatch(@RequestBody List<Privilege> privileges){ return privilegeService.addBatch(privileges); } @GetMapping("/all") @ApiOperation(value = "查询所有") public List<Privilege> getAll(){ return privilegeService.getAll(); } @GetMapping("/page") @ApiOperation(value = "查询分页") public IPage<Privilege> getPage(@RequestParam int pageNum,int pageSize){ Page<Privilege> page = new Page<>(pageNum, pageSize); return privilegeService.getAllWithPage(page); } @PutMapping @ApiOperation(value = "编辑") public boolean update(@RequestBody Privilege privilege){ return privilegeService.update(privilege); } @DeleteMapping @ApiOperation(value = "删除") public boolean delete(@RequestParam int id){ return privilegeService.delete(id); } } src/main/java/com/whyc/controller/RoleController.java
@@ -1,21 +1,100 @@ package com.whyc.controller; import com.whyc.pojo.*; import com.whyc.service.RolePrivilegeService; import com.whyc.service.RoleService; import com.whyc.service.UserRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; @RequestMapping("role") @RestController @Slf4j @Api(value ="role value",tags = "角色") @Api(value ="role value",tags = "角色相关-用户,权限,菜单") public class RoleController { @GetMapping @ApiOperation(value = "查询") public void get(){ @Autowired private RoleService roleService; @Autowired private UserRoleService userRoleService; @Autowired private RolePrivilegeService rolePrivilegeService; @GetMapping("userWithNoRole") @ApiOperation(value = "查询未分配角色的用户") public List<User> getUserWithNoRole(){ return userRoleService.getUserWithNoRole(); } @GetMapping("userWithRole") @ApiOperation(value = "查询分配角色的用户") public List<UserRole> getUserWithRole(){ return userRoleService.getUserWithRole(); } @GetMapping("userWithRoleMap") @ApiOperation(value = "查询分配角色的用户Map") public Map<String,List<UserRole>> getUserWithRoleMap(){ return userRoleService.getUserWithRoleMap(); } @GetMapping("roleAll") @ApiOperation(value = "查询所有角色") public List<Role> getRoleAll(){ return roleService.getAll(); } @PostMapping @ApiOperation(value = "新增角色") public boolean add(@RequestBody Role role){ return roleService.add(role); } @PostMapping("batch") @ApiOperation(value = "批量新增角色") public boolean addBatch(@RequestBody List<Role> roles){ return roleService.addBatch(roles); } @PostMapping("bindingUserWithRole") @ApiOperation(value = "绑定用户和角色") public boolean bindingUserWithRole(@RequestParam int userId,int roleId){ return userRoleService.bindingUserWithRole(userId,roleId); } @PostMapping("bindingUserWithRoleBatch") @ApiOperation(value = "批量绑定用户和角色",notes = "传入userId和roleId的数组") public boolean bindingUserWithRoleBatch(@RequestBody List<UserRole> userRoles){ return userRoleService.bindingUserWithRoleBatch(userRoles); } @PostMapping("bindingRoleWithPrivilege") @ApiOperation(value = "绑定角色-权限") public boolean bindingRoleWithPrivilege(@RequestParam int roleId,int privilegeId){ return rolePrivilegeService.bindingUserWithRole(roleId,privilegeId); } @PostMapping("bindingRoleWithPrivilegeBatch") @ApiOperation(value = "批量绑定角色-权限",notes = "传入roleId和privilegeId的数组") public boolean bindingRoleWithPrivilegeBatch(@RequestBody List<RolePrivilege> rolePrivileges){ return rolePrivilegeService.bindingUserWithRoleBatch(rolePrivileges); } @GetMapping("privilege") @ApiOperation(value = "获取当前用户的权限") public List<Privilege> getPrivileges(HttpServletRequest request){ User user = (User) request.getSession().getAttribute("user"); return rolePrivilegeService.getPrivileges(user.getId()); } } src/main/java/com/whyc/controller/UserController.java
@@ -70,7 +70,7 @@ } @PostMapping("login") @ApiOperation(value = "登录") @ApiOperation(value = "登录",position = 1) public boolean login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){ return userService.login(username,password,request); } src/main/java/com/whyc/filter/LoginFilter.java
@@ -11,9 +11,9 @@ import java.io.PrintWriter; @Slf4j @WebFilter(filterName = "loginFilter",urlPatterns = "/*",initParams = { /*@WebFilter(filterName = "loginFilter",urlPatterns = "/*",initParams = { @WebInitParam(name = "exclusions",value = "*.css,*.js") }) })*/ //@WebInitParam(name = "exclusions",value = "*.css,*.js") public class LoginFilter implements Filter { src/main/java/com/whyc/mapper/PrivilegeMapper.java
New file @@ -0,0 +1,11 @@ package com.whyc.mapper; import com.whyc.pojo.Privilege; /** * 权限 */ public interface PrivilegeMapper extends CustomMapper<Privilege> { } src/main/java/com/whyc/mapper/RoleMapper.java
New file @@ -0,0 +1,11 @@ package com.whyc.mapper; import com.whyc.pojo.Role; /** * 角色 */ public interface RoleMapper extends CustomMapper<Role>{ } src/main/java/com/whyc/mapper/RolePrivilegeMapper.java
New file @@ -0,0 +1,16 @@ package com.whyc.mapper; import com.whyc.pojo.Privilege; import com.whyc.pojo.RolePrivilege; import org.apache.ibatis.annotations.Select; import java.util.List; /** * 角色-权限 */ public interface RolePrivilegeMapper extends CustomMapper<RolePrivilege> { @Select("") List<Privilege> getPrivileges(Integer userId); } src/main/java/com/whyc/mapper/UserRoleMapper.java
New file @@ -0,0 +1,16 @@ package com.whyc.mapper; import com.whyc.pojo.User; import com.whyc.pojo.UserRole; import java.util.List; import java.util.Map; public interface UserRoleMapper extends CustomMapper<UserRole>{ List<User> getUserWithNoRole(); List<UserRole> getUserWithRole(); List<UserRole> getUserWithRoleMap(); } src/main/java/com/whyc/pojo/Menu.java
@@ -1,8 +1,11 @@ package com.whyc.pojo; import org.apache.ibatis.type.Alias; /** * 用户菜单 */ @Alias("Menu") public class Menu { private Integer id; private String name; src/main/java/com/whyc/pojo/Privilege.java
@@ -1,12 +1,15 @@ package com.whyc.pojo; import org.apache.ibatis.type.Alias; /** * 权限 */ @Alias("Privilege") public class Privilege { private Integer id; private String name; private String privilege; public Integer getId() { return id; @@ -16,11 +19,11 @@ this.id = id; } public String getName() { return name; public String getPrivilege() { return privilege; } public void setName(String name) { this.name = name; public void setPrivilege(String privilege) { this.privilege = privilege; } } src/main/java/com/whyc/pojo/Role.java
@@ -1,12 +1,26 @@ package com.whyc.pojo; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import org.apache.ibatis.type.Alias; /** * 角色 */ @Alias("Role") public class Role { private Integer id; @TableField("name") private String name; public Role() { } public Role(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } src/main/java/com/whyc/pojo/RoleMenu.java
@@ -1,8 +1,11 @@ package com.whyc.pojo; import org.apache.ibatis.type.Alias; /** * 角色菜单 */ @Alias("RoleMenu") public class RoleMenu { private Integer id; private Integer roleId; src/main/java/com/whyc/pojo/RolePrivilege.java
@@ -1,14 +1,22 @@ package com.whyc.pojo; import org.apache.ibatis.type.Alias; /** * 角色对应权限 */ @Alias("RolePrivilege") public class RolePrivilege { private Integer id; private Integer roleId; private Integer privilegeId; public RolePrivilege(int roleId, int privilegeId) { this.roleId = roleId; this.privilegeId = privilegeId; } public Integer getId() { return id; } src/main/java/com/whyc/pojo/User.java
@@ -1,15 +1,31 @@ package com.whyc.pojo; import com.baomidou.mybatisplus.annotation.TableField; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.*; import org.apache.ibatis.type.Alias; import java.util.Date; @Alias("User") @Data @Builder @ToString @AllArgsConstructor public class User { private Integer id; private String username; private String name; private String password; @JsonFormat(locale = "zh",timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; public User() { } public User(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; @@ -19,12 +35,12 @@ this.id = id; } public String getUsername() { return username; public String getName() { return name; } public void setUsername(String username) { this.username = username; public void setName(String name) { this.name = name; } public String getPassword() { src/main/java/com/whyc/pojo/UserRole.java
@@ -1,11 +1,64 @@ package com.whyc.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import org.apache.ibatis.type.Alias; import java.beans.Transient; /** * 用户角色表 */ @Alias("UserRole") public class UserRole { private Integer id; private Integer userId; private Integer roleId; @TableField(exist = false) private User user; @TableField(exist = false) private Role role; @TableField(exist = false) private String userIds; @TableField(exist = false) private String userNames; @TableField(exist = false) private String roleName; public UserRole() { } public UserRole(Integer userId, Integer roleId) { this.userId = userId; this.roleId = roleId; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Integer getRoleId() { return roleId; } public void setRoleId(Integer roleId) { this.roleId = roleId; } public User getUser() { return user; @@ -22,4 +75,28 @@ public void setRole(Role role) { this.role = role; } public String getUserIds() { return userIds; } public void setUserIds(String userIds) { this.userIds = userIds; } public String getUserNames() { return userNames; } public void setUserNames(String userNames) { this.userNames = userNames; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } } src/main/java/com/whyc/service/PrivilegeService.java
New file @@ -0,0 +1,49 @@ package com.whyc.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.whyc.mapper.PrivilegeMapper; import com.whyc.pojo.Privilege; import com.whyc.pojo.User; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * 权限 */ @Service public class PrivilegeService { @Resource private PrivilegeMapper mapper; public boolean add(Privilege privilege) { return mapper.insert(privilege)>0; } @Transactional public boolean addBatch(List<Privilege> privileges) { return mapper.insertBatchSomeColumn(privileges)==privileges.size(); } public List<Privilege> getAll() { return mapper.selectList(null); } public IPage<Privilege> getAllWithPage(Page<Privilege> page) { return mapper.selectPage(page,null); } public boolean update(Privilege privilege) { return mapper.updateById(privilege)>0; } public boolean delete(int id) { return mapper.deleteById(id)>0; } } src/main/java/com/whyc/service/RolePrivilegeService.java
New file @@ -0,0 +1,35 @@ package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.whyc.mapper.RolePrivilegeMapper; import com.whyc.pojo.Privilege; import com.whyc.pojo.RolePrivilege; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * 角色-权限 */ @Service public class RolePrivilegeService { @Resource private RolePrivilegeMapper mapper; public boolean bindingUserWithRole(int roleId, int privilegeId) { return mapper.insert(new RolePrivilege(roleId,privilegeId))>0; } @Transactional public boolean bindingUserWithRoleBatch(List<RolePrivilege> rolePrivileges) { return mapper.insertBatchSomeColumn(rolePrivileges)==rolePrivileges.size(); } public List<Privilege> getPrivileges(Integer userId) { return mapper.getPrivileges(userId); } } src/main/java/com/whyc/service/RoleService.java
New file @@ -0,0 +1,29 @@ package com.whyc.service; import com.whyc.mapper.RoleMapper; import com.whyc.pojo.Role; import com.whyc.pojo.UserRole; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class RoleService { @Resource private RoleMapper mapper; public List<Role> getAll() { return mapper.selectList(null); } public boolean add(Role role) { return mapper.insert(role)>0; } public boolean addBatch(List<Role> roles) { return mapper.insertBatchSomeColumn(roles)==roles.size(); } } src/main/java/com/whyc/service/UserRoleService.java
New file @@ -0,0 +1,62 @@ package com.whyc.service; import com.whyc.mapper.UserRoleMapper; import com.whyc.pojo.Role; import com.whyc.pojo.User; import com.whyc.pojo.UserRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * 用户对应角色 */ @Service public class UserRoleService { @Resource private UserRoleMapper mapper; public List<User> getUserWithNoRole() { return mapper.getUserWithNoRole(); } public List<UserRole> getUserWithRole() { List<UserRole> userRoles = mapper.getUserWithRole(); return userRoles; } public Map<String,List<UserRole>> getUserWithRoleMap() { HashMap<String, List<UserRole>> map = new HashMap<>(); List<UserRole> userRoleList = new LinkedList<>(); List<UserRole> userRoles= mapper.getUserWithRoleMap(); for (UserRole temp:userRoles){ String[] userIds = temp.getUserIds().split(","); String[] userNames = temp.getUserNames().split(","); for (int i = 0; i < userIds.length; i++) { UserRole userRole = new UserRole(); userRole.setUser(new User(Integer.parseInt(userIds[i]),userNames[i])); userRole.setRoleId(temp.getRoleId()); userRole.setRoleName(temp.getRoleName()); userRoleList.add(userRole); } map.put(temp.getRoleName(),userRoleList); } return map; } public boolean bindingUserWithRole(int userId,int roleId) { return mapper.insert(new UserRole(userId,roleId))>0; } public boolean bindingUserWithRoleBatch(List<UserRole> userRoles) { return mapper.insertBatchSomeColumn(userRoles)==userRoles.size(); } } src/main/java/com/whyc/service/UserService.java
@@ -108,8 +108,8 @@ } Map<String,UserLoginInfo> userMap = (Map<String, UserLoginInfo>) servletContext.getAttribute("users"); UserLoginInfo userLoginInfo = userMap.get(user.getUsername()); if(userLoginInfo.getUsername().equals(user.getUsername()) && userLoginInfo.getTimestamp().compareTo(timestamp)==0){ UserLoginInfo userLoginInfo = userMap.get(user.getName()); if(userLoginInfo.getUsername().equals(user.getName()) && userLoginInfo.getTimestamp().compareTo(timestamp)==0){ //说明没有从其他地方登录 }else{ //说明从其他地方登录了,当前用户的session清除 src/main/resources/config/application.yml
@@ -18,7 +18,7 @@ maxPoolSize: 500 mybatis-plus: typeAliasesPackage: com.fgkj.pojo typeAliasesPackage: com.whyc.pojo mapper-locations: classpath:mapper/**/*Mapper.xml global-config: db-config: @@ -31,7 +31,7 @@ #mp2.3+ 全局表前缀 tb_ table-prefix: tb_ #刷新mapper 调试神器 refresh-mapper: true # refresh-mapper: true configuration: #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId) map-underscore-to-camel-case: true src/main/resources/mapper/UserRoleMapper.xml
New file @@ -0,0 +1,34 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.whyc.mapper.UserRoleMapper" > <resultMap id="Map_UserRole" type="UserRole"> <association property="user" javaType="User"> <id property="id" column="userId" /> <result property="name" column="username" /> </association> <association property="role" javaType="Role"> <id property="id" column="roleId"/> <result property="name" column="name"/> </association> </resultMap> <resultMap id="Map_UserRoles" type="UserRole"> <result column="userIds" property="userIds" /> <result column="userNames" property="userNames" /> <result property="roleId" column="roleId"/> <result property="roleName" column="name"/> </resultMap> <select id="getUserWithNoRole" resultType="User"> select user.id,user.name from tb_user user left join tb_user_role userRole on user.id =userRole.user_id where userRole.role_id is null </select> <select id="getUserWithRole" resultMap="Map_UserRole"> select user.id as userId,user.name as username,role.id as roleId,role.name from tb_user_role userRole,tb_user user,tb_role role where user.id = userRole.user_id and userRole.role_id=role.id </select> <select id="getUserWithRoleMap" resultMap="Map_UserRoles"> select GROUP_CONCAT(user.id ) as userIds,GROUP_CONCAT(user.name) as userNames,role.id as roleId,role.name FROM tb_user_role userRole,tb_user user,tb_role role WHERE user.id = userRole.user_id and userRole.role_id=role.id group by roleId </select> </mapper>