本例子用RBAC(Role-Based Access Control 基于角色的权限访问控制)系统作为例子来介绍MyBatis的XML基本用法
1、先增加几个数据库表
DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘用户ID‘, `user_name` varchar(50) DEFAULT NULL COMMENT ‘用户名‘, `user_password` varchar(50) DEFAULT NULL COMMENT ‘密码‘, `user_email` varchar(50) DEFAULT ‘[email protected]‘ COMMENT ‘邮箱‘, `user_info` text COMMENT ‘简介‘, `head_img` blob COMMENT ‘头像‘, `create_time` datetime DEFAULT NULL COMMENT ‘创建时间‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1035 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘; DROP TABLE IF EXISTS `sys_role`; CREATE TABLE `sys_role` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘角色ID‘, `role_name` varchar(50) DEFAULT NULL COMMENT ‘角色名‘, `enabled` int(11) DEFAULT NULL COMMENT ‘有效标志‘, `create_by` bigint(20) DEFAULT NULL COMMENT ‘创建人‘, `create_time` datetime DEFAULT NULL COMMENT ‘创建时间‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT=‘角色表‘; DROP TABLE IF EXISTS `sys_privilege`; CREATE TABLE `sys_privilege` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘权限ID‘, `privilege_name` varchar(50) DEFAULT NULL COMMENT ‘权限名称‘, `privilege_url` varchar(200) DEFAULT NULL COMMENT ‘权限URL‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT=‘权限表‘; DROP TABLE IF EXISTS `sys_user_role`; CREATE TABLE `sys_user_role` ( `user_id` bigint(20) DEFAULT NULL COMMENT ‘用户ID‘, `role_id` bigint(20) DEFAULT NULL COMMENT ‘角色ID‘ ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘用户角色关联表‘; DROP TABLE IF EXISTS `sys_role_privilege`; CREATE TABLE `sys_role_privilege` ( `role_id` bigint(20) DEFAULT NULL COMMENT ‘角色ID‘, `privilege_id` bigint(20) DEFAULT NULL COMMENT ‘权限ID‘ ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘角色权限关联表‘;
2、插入测试数据
INSERT INTO `sys_privilege` VALUES (‘1‘, ‘用户管理‘, ‘/users‘); INSERT INTO `sys_privilege` VALUES (‘2‘, ‘角色管理‘, ‘/roles‘); INSERT INTO `sys_privilege` VALUES (‘3‘, ‘系统日志‘, ‘/logs‘); INSERT INTO `sys_privilege` VALUES (‘4‘, ‘人员维护‘, ‘/persons‘); INSERT INTO `sys_privilege` VALUES (‘5‘, ‘单位维护‘, ‘/companies‘); INSERT INTO `sys_role` VALUES (‘1‘, ‘管理员‘, ‘1‘, ‘1‘, ‘2016-04-01 17:02:14‘); INSERT INTO `sys_role` VALUES (‘2‘, ‘普通用户‘, ‘1‘, ‘1‘, ‘2016-04-01 17:02:34‘); INSERT INTO `sys_role_privilege` VALUES (‘1‘, ‘1‘); INSERT INTO `sys_role_privilege` VALUES (‘1‘, ‘3‘); INSERT INTO `sys_role_privilege` VALUES (‘1‘, ‘2‘); INSERT INTO `sys_role_privilege` VALUES (‘2‘, ‘4‘); INSERT INTO `sys_role_privilege` VALUES (‘2‘, ‘5‘); INSERT INTO `sys_user` VALUES (‘1‘, ‘admin‘, ‘123456‘, ‘[email protected]‘, ‘管理员用户‘, 0x1231231230, ‘2016-06-07 01:11:12‘); INSERT INTO `sys_user` VALUES (‘1001‘, ‘test‘, ‘123456‘, ‘[email protected]‘, ‘测试用户‘, 0x1231231230, ‘2016-06-07 00:00:00‘); INSERT INTO `sys_user_role` VALUES (‘1‘, ‘1‘); INSERT INTO `sys_user_role` VALUES (‘1‘, ‘2‘); INSERT INTO `sys_user_role` VALUES (‘1001‘, ‘2‘);
3、创建实体类
开发中创建实体类可以使用MyBatis Generator工具来根据数据库表来生成实体类
package tk.mybatis.simple.model; import java.io.Serializable; /** * 权限表 */ public class SysPrivilege implements Serializable { private static final long serialVersionUID = 6315662516417216377L; /** * 权限ID */ private Long id; /** * 权限名称 */ private String privilegeName; /** * 权限URL */ private String privilegeUrl; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPrivilegeName() { return privilegeName; } public void setPrivilegeName(String privilegeName) { this.privilegeName = privilegeName; } public String getPrivilegeUrl() { return privilegeUrl; } public void setPrivilegeUrl(String privilegeUrl) { this.privilegeUrl = privilegeUrl; } }
package tk.mybatis.simple.model; import java.io.Serializable; import java.util.Date; import java.util.List; import tk.mybatis.simple.type.Enabled; /** * 角色表 */ public class SysRole implements Serializable { private static final long serialVersionUID = 6320941908222932112L; /** * 角色ID */ private Long id; /** * 角色名 */ private String roleName; /** * 有效标志 */ private Enabled enabled; /** * 创建人 */ private String createBy; /** * 创建时间 */ private Date createTime; /** * 用户信息 */ private SysUser user; /** * 创建信息 */ private CreateInfo createInfo; public CreateInfo getCreateInfo() { return createInfo; } public void setCreateInfo(CreateInfo createInfo) { this.createInfo = createInfo; } /** * 角色包含的权限列表 */ List<SysPrivilege> privilegeList; public List<SysPrivilege> getPrivilegeList() { return privilegeList; } public void setPrivilegeList(List<SysPrivilege> privilegeList) { this.privilegeList = privilegeList; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public Enabled getEnabled() { return enabled; } public void setEnabled(Enabled enabled) { this.enabled = enabled; } public String getCreateBy() { return createBy; } public void setCreateBy(String createBy) { this.createBy = createBy; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public SysUser getUser() { return user; } public void setUser(SysUser user) { this.user = user; } }
package tk.mybatis.simple.model; /** * 角色权限关联表 */ public class SysRolePrivilege { /** * 角色ID */ private Long roleId; /** * 权限ID */ private Long privilegeId; public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { this.roleId = roleId; } public Long getPrivilegeId() { return privilegeId; } public void setPrivilegeId(Long privilegeId) { this.privilegeId = privilegeId; } }
package tk.mybatis.simple.model; import java.io.Serializable; import java.util.Date; import java.util.List; /** * 用户表 */ public class SysUser implements Serializable { private static final long serialVersionUID = -328602757171077630L; /** * 用户ID */ private Long id; /** * 用户名 */ private String userName; /** * 密码 */ private String userPassword; /** * 邮箱 */ private String userEmail; /** * 简介 */ private String userInfo; /** * 头像 */ private byte[] headImg; /** * 创建时间 */ private Date createTime; /** * 用户角色 */ private SysRole role; /** * 用户的角色集合 */ private List<SysRole> roleList; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserEmail() { return userEmail; } public void setUserEmail(String userEmail) { this.userEmail = userEmail; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo; } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public SysRole getRole() { return role; } public void setRole(SysRole role) { this.role = role; } public List<SysRole> getRoleList() { return roleList; } public void setRoleList(List<SysRole> roleList) { this.roleList = roleList; } }
package tk.mybatis.simple.model; /** * 用户角色关联表 */ public class SysUserRole { /** * 用户ID */ private Long userId; /** * 角色ID */ private Long roleId; public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { this.roleId = roleId; } }
4、创建Mapper.xml和对应接口类
在 src/main/resources 的 tk.mybatis.simple.mapper 目录下创建 5 个表各自对应的 XML 文件,分别为 UserMapper.xml 、 RoleMapper.xml 、 PrivilegeMapper.xml 、 UserRoleMapper.xml 和
RolePrivilegeMapper.xml 。然后,在 src/main/java 下面创建包 tk.mybatis.simple.mapper 。接着,在该包下创建 XML 文件对应的接口类,分别为 UserMapper.java 、 RoleMapper.java 、 PrivilegeMapper.java 、
UserRoleMapper.java 和 RolePrivilegeMapper.java
下面以User表对应的XML文件和接口类设计为例介绍如何实现
UserMapper.java
package tk.mybatis.simple.mapper; public interface UserMapper { }
UserMapper.xml
此文件用全限定的类名,把接口和XML文件关联起来
<?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="tk.mybatis.simple.mapper.UserMapper"> </mapper>
mybatis-config.xml
把UserMapper配置到mybatis里面去,有2种方法:
1、通过接口配置
<mappers> <package name="tk.mybatis.simple.mapper" /> </mappers>
2、通过xml文件配置
<mappers> <mapper resource="tk/mybatis/simple/mapper/UserMapper.xml" /> </mappers>
建议通过接口配置,因为只需要指定接口所在的包,MyBatis会扫描所有接口对应的Mapper
这种方式的运行流程如下:
1、判断接口对应的命名空间是否已经存在,如果存在就抛出异常,不存在就继续进行接下来的操作
2、加载接口对应的XML 映射文件,将接口全限定名转换为路径,例如,将接口tk.mybatis.simple.mapper.UserMapper转换为tk/mybatis/simple/mapper/UserMapper.xml,以.xml为后缀搜索XML资源,如果找到就解析XML
3、处理接口中的注解方法
这样就完成了XML的配置