最近刚刚学习了mybatis,也使用了mybatis进行RBAC(Role-Based Access Control:基于角色的访问控制)的mapper实现。
首先是我的数据库(很简陋):
user:用户
role:角色
menu:角色的访问菜单权限(这里可能不太对)
其他两张表为关系表
sql语句如下:
drop database if exists ssmtest;
create database ssmtest;
use ssmtest;
create table t_user(
id int(11) not null auto_increment,
username varchar(15) not null,
password varchar(15) not null,
primary key(id)
)engine=InnoDB default charset=utf8 AUTO_INCREMENT=1;
create table t_role(
id int(11) not null auto_increment,
rolename varchar(15) not null,
primary key(id)
)engine=InnoDB default charset=utf8 AUTO_INCREMENT=1;
create table t_menu(
id int(11) not null auto_increment,
urlname varchar(25),
menuname varchar(25) not null,
primary key(id)
)engine=InnoDB default charset=utf8 AUTO_INCREMENT=1;
create table t_user_role(
id int(11) not null auto_increment,
user_id int not null,
role_id int not null,
primary key(id)
)engine=InnoDB default charset=utf8 AUTO_INCREMENT=1;
create table t_role_menu(
id int(11) not null auto_increment,
role_id int not null,
menu_id int not null,
primary key(id)
)engine=InnoDB default charset=utf8 AUTO_INCREMENT=1;
insert into t_role(rolename) values('普通用户');
insert into t_role(rolename) values('普通管理员');
insert into t_role(rolename) values('系统管理员');
insert into t_role(rolename) values('VIP用户');
insert into t_user(username,password) value('张三','123456');
insert into t_user(username,password) value('李四','123456');
insert into t_user(username,password) value('王五','123456');
insert into t_user(username,password) value('张二','123456');
insert into t_user(username,password) value('赵毅','123456');
insert into t_menu(urlname,menuname) values('/user/index.jsp','用户主界面');
insert into t_menu(urlname,menuname) values('/sys/index.jsp','管理员主界面');
insert into t_menu(urlname,menuname) values('/sys/sys_index.jsp','超级管理员主界面');
insert into t_menu(urlname,menuname) values('/user/vip_index.jsp','VIP用户主界面');
insert into t_user_role(user_id,role_id) values(1,1);
insert into t_user_role(user_id,role_id) values(2,4);
insert into t_user_role(user_id,role_id) values(3,1);
insert into t_user_role(user_id,role_id) values(4,3);
insert into t_user_role(user_id,role_id) values(5,2);
insert into t_role_menu(role_id,menu_id) values(1,1);
insert into t_role_menu(role_id,menu_id) values(2,2);
insert into t_role_menu(role_id,menu_id) values(3,3);
insert into t_role_menu(role_id,menu_id) values(4,4);
由于刚开始学习mybatis,开始的时候我是使用直接生成器(可以查看我上一篇文章)进行数据生成,这样我的关系的映射也直接变成了一个实体类。后来在这里卡住了好久。
后来,把role当成一个属性加入到user实体类中:
被menu作为属性加入到role实体类中:
在usermapper.xml中的查询语句如下:
<resultMap id="user" type="entity.User">
<result property="id" column="id"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<collection property="role" column="id" javaType="entity.Role" select="selectAllRoles"></collection>
//这里使用collection好像有点不对,好像应该使用association,请大神指导
</resultMap>
<resultMap type="entity.Role" id="RoleMap">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="rolename" property="rolename" jdbcType="VARCHAR" />
<collection property="menus" column="id" javaType="java.util.List" select="selectAllMenus"> </collection>
</resultMap>
<select id="selectAllMenus" resultType="entity.Menu" parameterType="java.lang.Integer">
select id, urlname,menuname from t_menu where id in (select t_role_menu.menu_id from t_role_menu where t_role_menu.role_id=#{id})
</select>
<select id="selectAllRoles" resultMap="RoleMap" parameterType="java.lang.Integer">
select id, rolename from t_role where id in (select t_user_role.role_id from t_user_role where t_user_role.user_id=#{id})
</select>
<select id="getByUserId" resultMap="user" parameterType="java.lang.Integer">
select * from t_user where id=#{id}
</select>
这里的大概思路是先设置两个resultMap,一个对应user类,一个对应role类,user类的resultMap中要使用collection或者association把user类中的role属性包起来 ,加上select=“selectAllRoles”,同理在role类中也一样。
select="selectAllRoles"是根据数据库中关系表的关系查询。
显示简单结果如下: