MaintainUserRole, 一个功能的实现方法
在系统开发中,经常会遇到像下面这种场景的功能模块:管理用户的角色,在界面上显示所有Active的角色,如果用户有这个角色在界面上标示出来,比如CheckBox勾中,如下图。
这里会涉及三个表,User,Role,UserRoleRelation.
DECLARE @UserID INT
SELECT @UserID=1
;WITH CTE_UserRole(RoleID) AS
(
SELECT RoleID
FROM dbo.BasicUserRoleRelation WITH(NOLOCK)
WHERE UserID=@UserID
)
SELECT a.RoleID
,a.RoleName
,CAST(CASE WHEN b.RoleID IS NULL THEN 0
ELSE 1
END AS BIT) AS IsChecked
FROM dbo.[BasicRole] a WITH(NOLOCK)
LEFT OUTER JOIN CTE_UserRole b WITH(NOLOCK)
ON a.RoleID=b.RoleID
WHERE a.[Status]='A'
SELECT @UserID=1
;WITH CTE_UserRole(RoleID) AS
(
SELECT RoleID
FROM dbo.BasicUserRoleRelation WITH(NOLOCK)
WHERE UserID=@UserID
)
SELECT a.RoleID
,a.RoleName
,CAST(CASE WHEN b.RoleID IS NULL THEN 0
ELSE 1
END AS BIT) AS IsChecked
FROM dbo.[BasicRole] a WITH(NOLOCK)
LEFT OUTER JOIN CTE_UserRole b WITH(NOLOCK)
ON a.RoleID=b.RoleID
WHERE a.[Status]='A'
查询结果:
再用ORM等映射到对象列表,然后在UI端绑定即可。
——致力于微软企业解决方案、项目管理及技术培训
posted on 2011-02-16 22:47 James.H.Fu 阅读(1332) 评论(1) 编辑 收藏 举报