Mybatis Dao实例

tech2024-08-04  56

IRoleDao

public interface IRoleDao { @Select("select * from role") List<Role> findAll() throws Exception; //根据用户id查询出所有对应的角色 @Select(("select * from role where id in (select roleId from users_role where userId=#{userId})")) @Results({ @Result(id = true, property = "id", column = "id"), @Result(property = "roleName", column = "roleName"), @Result(property = "roleDesc", column = "roleDesc"), @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.itheima.ssm.dao.IPermissionDao.findPermissionByRoleId")), }) List<Role> findRoleByUserId(String userId) throws Exception; @Insert("insert into role (roleName, roleDesc) values(#{roleName}, #{roleDesc})") void save(Role role) throws Exception; @Select("select * from role where id = #{roleId}") @Results({ @Result(id = true, property = "id", column = "id"), @Result(property = "roleName", column = "roleName"), @Result(property = "roleDesc", column = "roleDesc"), @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.itheima.ssm.dao.IPermissionDao.findPermissionByRoleId")) }) Role findById(int roleId); @Delete(("delete from users_role where roleId = #{roleId}")) void deleteFromUser_RoleByRoleId(int roleId); @Delete(("delete from role_permission where roleId = #{roleId}")) void deleteFromRole_PermissionByRoleId(int roleId); @Delete(("delete from role where id = #{roleId}")) void deleteRoleById(int roleId); @Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{roleId})") List<Permission> findOtherPermissions(int roleId) throws Exception; @Insert("insert into role_permission (roleId, permissionId) values (#{roleId}, #{permissionId})") void addPermissionToRole(@Param("roleId") int roleId, @Param("permissionId") int permissionId); }

IPermissionDao

public interface IPermissionDao { @Select("select * from permission where id in (select permissionId from role_permission where roleId = #{id})") public List<Permission> findPermissionByRoleId(int id) throws Exception; @Select("select * from permission") List<Permission> findAll() throws Exception; @Insert("insert into permission(permissionName, url) values(#{permissionName}, #{url})") void save(Permission permission); }

Role

public class Role { private String id; private String roleName; private String roleDesc; private List<Permission> permissions; private List<UserInfo> users; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } public List<Permission> getPermissions() { return permissions; } public void setPermissions(List<Permission> permissions) { this.permissions = permissions; } public List<UserInfo> getUsers() { return users; } public void setUsers(List<UserInfo> users) { this.users = users; } }

Permission

public class Permission { private String id; private String permissionName; private String url; private List<Role> roles; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPermissionName() { return permissionName; } public void setPermissionName(String permissionName) { this.permissionName = permissionName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } }
最新回复(0)