1、创建数据库和数据表(以Mysql数据库为例) 设计一个权限管理系统。 表设计如下:用户表、角色表、菜单表 使用Mybaits构建用户、角色、菜单的映射关系 1、用户表(UserInfo):Id、UserName、UserPwd,userSex,userSchool,userAddress 2、角色表(RoleInfo):Id、RoleName 3、菜单表(MenuInfo):Id、MenuName 4、用户角色表(UserRole):Id、UserId、RoleId; 5、角色菜单表(RoleMenu):Id、RoleId、MenuId 2、1.用户和角色之间的关系 2.角色和菜单之间的关系查询编写单元测试用例。 3.使用动态sql拼接的方式实现在一个接口内进行用户名、用户地址、用户性别的筛选。
时间: 2023-06-11 14:08:07 浏览: 279
1. 创建数据库和数据表(以Mysql数据库为例)
创建数据库:
```
CREATE DATABASE IF NOT EXISTS `permission_management` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
```
创建用户表:
```
CREATE TABLE IF NOT EXISTS `user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`user_name` varchar(255) NOT NULL COMMENT '用户名称',
`user_pwd` varchar(255) NOT NULL COMMENT '用户密码',
`user_sex` varchar(255) NOT NULL COMMENT '用户性别',
`user_school` varchar(255) NOT NULL COMMENT '用户学校',
`user_address` varchar(255) NOT NULL COMMENT '用户地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表';
```
创建角色表:
```
CREATE TABLE IF NOT EXISTS `role_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`role_name` varchar(255) NOT NULL COMMENT '角色名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='角色表';
```
创建菜单表:
```
CREATE TABLE IF NOT EXISTS `menu_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`menu_name` varchar(255) NOT NULL COMMENT '菜单名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='菜单表';
```
创建用户角色表:
```
CREATE TABLE IF NOT EXISTS `user_role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户角色ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`role_id` int(11) NOT NULL COMMENT '角色ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户角色表';
```
创建角色菜单表:
```
CREATE TABLE IF NOT EXISTS `role_menu` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色菜单ID',
`role_id` int(11) NOT NULL COMMENT '角色ID',
`menu_id` int(11) NOT NULL COMMENT '菜单ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='角色菜单表';
```
2. 使用Mybaits构建用户、角色、菜单的映射关系
创建用户表对应的实体类:
```
public class UserInfo {
private Integer id;
private String userName;
private String userPwd;
private String userSex;
private String userSchool;
private String userAddress;
// getter and setter
}
```
创建角色表对应的实体类:
```
public class RoleInfo {
private Integer id;
private String roleName;
// getter and setter
}
```
创建菜单表对应的实体类:
```
public class MenuInfo {
private Integer id;
private String menuName;
// getter and setter
}
```
创建用户角色表对应的实体类:
```
public class UserRole {
private Integer id;
private Integer userId;
private Integer roleId;
// getter and setter
}
```
创建角色菜单表对应的实体类:
```
public class RoleMenu {
private Integer id;
private Integer roleId;
private Integer menuId;
// getter and setter
}
```
创建用户、角色、菜单的Mapper类:
```
public interface UserInfoMapper {
UserInfo getUserInfoById(Integer id);
List<UserInfo> getUserInfoListByRoleId(Integer roleId);
List<UserInfo> getUserInfoListByUserNameAndUserAddressAndUserSex(@Param("userName") String userName, @Param("userAddress") String userAddress, @Param("userSex") String userSex);
}
public interface RoleInfoMapper {
RoleInfo getRoleInfoById(Integer id);
List<RoleInfo> getRoleInfoListByUserId(Integer userId);
}
public interface MenuInfoMapper {
MenuInfo getMenuInfoById(Integer id);
List<MenuInfo> getMenuInfoListByRoleId(Integer roleId);
}
public interface UserRoleMapper {
UserRole getUserRoleById(Integer id);
List<UserRole> getUserRoleListByUserId(Integer userId);
List<UserRole> getUserRoleListByRoleId(Integer roleId);
}
public interface RoleMenuMapper {
RoleMenu getRoleMenuById(Integer id);
List<RoleMenu> getRoleMenuListByRoleId(Integer roleId);
List<RoleMenu> getRoleMenuListByMenuId(Integer menuId);
}
```
在Mybatis的配置文件中,分别加入上述Mapper类的配置。
3. 用户和角色之间的关系查询
查询某个用户所具有的角色:
```
SELECT r.* FROM role_info r LEFT JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}
```
查询某个角色所对应的用户列表:
```
SELECT u.* FROM user_info u LEFT JOIN user_role ur ON u.id = ur.user_id WHERE ur.role_id = #{roleId}
```
4. 角色和菜单之间的关系查询
查询某个角色所具有的菜单:
```
SELECT m.* FROM menu_info m LEFT JOIN role_menu rm ON m.id = rm.menu_id WHERE rm.role_id = #{roleId}
```
查询某个菜单所对应的角色列表:
```
SELECT r.* FROM role_info r LEFT JOIN role_menu rm ON r.id = rm.role_id WHERE rm.menu_id = #{menuId}
```
5. 编写单元测试用例
创建测试类:
```
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class PermissionManagementTest {
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private RoleInfoMapper roleInfoMapper;
@Autowired
private MenuInfoMapper menuInfoMapper;
@Autowired
private UserRoleMapper userRoleMapper;
@Autowired
private RoleMenuMapper roleMenuMapper;
@Test
public void testGetUserInfoById() {
UserInfo userInfo = userInfoMapper.getUserInfoById(1);
System.out.println(userInfo.getUserName());
}
@Test
public void testGetRoleInfoById() {
RoleInfo roleInfo = roleInfoMapper.getRoleInfoById(1);
System.out.println(roleInfo.getRoleName());
}
@Test
public void testGetMenuInfoById() {
MenuInfo menuInfo = menuInfoMapper.getMenuInfoById(1);
System.out.println(menuInfo.getMenuName());
}
@Test
public void testGetUserInfoListByRoleId() {
List<UserInfo> userInfoList = userInfoMapper.getUserInfoListByRoleId(1);
for (UserInfo userInfo : userInfoList) {
System.out.println(userInfo.getUserName());
}
}
@Test
public void testGetRoleInfoListByUserId() {
List<RoleInfo> roleInfoList = roleInfoMapper.getRoleInfoListByUserId(1);
for (RoleInfo roleInfo : roleInfoList) {
System.out.println(roleInfo.getRoleName());
}
}
@Test
public void testGetMenuInfoListByRoleId() {
List<MenuInfo> menuInfoList = menuInfoMapper.getMenuInfoListByRoleId(1);
for (MenuInfo menuInfo : menuInfoList) {
System.out.println(menuInfo.getMenuName());
}
}
@Test
public void testGetUserRoleListByUserId() {
List<UserRole> userRoleList = userRoleMapper.getUserRoleListByUserId(1);
for (UserRole userRole : userRoleList) {
System.out.println(userRole.getRoleId());
}
}
@Test
public void testGetUserRoleListByRoleId() {
List<UserRole> userRoleList = userRoleMapper.getUserRoleListByRoleId(1);
for (UserRole userRole : userRoleList) {
System.out.println(userRole.getUserId());
}
}
@Test
public void testGetRoleMenuListByRoleId() {
List<RoleMenu> roleMenuList = roleMenuMapper.getRoleMenuListByRoleId(1);
for (RoleMenu roleMenu : roleMenuList) {
System.out.println(roleMenu.getMenuId());
}
}
@Test
public void testGetRoleMenuListByMenuId() {
List<RoleMenu> roleMenuList = roleMenuMapper.getRoleMenuListByMenuId(1);
for (RoleMenu roleMenu : roleMenuList) {
System.out.println(roleMenu.getRoleId());
}
}
}
```
6. 使用动态sql拼接的方式实现在一个接口内进行用户名、用户地址、用户性别的筛选
在UserInfoMapper接口中添加如下方法:
```
List<UserInfo> getUserInfoListByCondition(@Param("userName") String userName, @Param("userAddress") String userAddress, @Param("userSex") String userSex);
```
在UserInfoMapper.xml中添加如下SQL:
```
<select id="getUserInfoListByCondition" parameterType="java.util.Map" resultType="com.example.demo.model.UserInfo">
SELECT *
FROM user_info
WHERE 1=1
<if test="userName != null and userName != ''">
AND user_name = #{userName}
</if>
<if test="userAddress != null and userAddress != ''">
AND user_address = #{userAddress}
</if>
<if test="userSex != null and userSex != ''">
AND user_sex = #{userSex}
</if>
</select>
```
在UserService中调用该方法:
```
public List<UserInfo> getUserInfoListByCondition(String userName, String userAddress, String userSex) {
return userInfoMapper.getUserInfoListByCondition(userName, userAddress, userSex);
}
```
阅读全文