一、使用IDEA新建maven工程
二、引入mybatis以及相关的jar
<!--版本仅供参考-->
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> </dependencies>
三、建表
权限管理需求的介绍,一个用户拥有若干角色,一个角色拥有若干权限,权限就是对某个资源的增删改查,这样就构成了用户-角色-权限之间的模型,在这种关系中,用户与角色,角色与权限之间一般是多对多的关系
建表sql:
create table sys_user( id int not null auto_increment comment '用户ID', user_name varchar(32) comment '用户名', password varchar(50) comment '用户密码', usr_info varchar(50) comment '用户简介', head_img blob comment '头像', create_time datetime comment '创建时间', primary key(id) ); alter table sys_user comment '用户表'; create table sys_role( id int not null auto_increment comment '角色ID', role_name varchar(50) comment '角色名称', enabled int comment '有效标志', create_by bigint comment '创建人', create_time datetime comment '创建时间', primary key(id) ); alter table sys_role comment '角色表'; create table sys_privilege( id int not null auto_increment comment '权限ID', privilege_name varchar(50) comment '权限名称', privilege_url varchar(50) comment '权限URL', primary key(id) ); alter table sys_privilege comment '权限表'; create table sys_user_role( user_id bigint not null comment '用户ID', role_id bigint not null comment '角色ID' ); alter table sys_user comment '用户角色'; create table sys_role_privilege( role_id bigint not null comment '角色ID', privilege_id bigint not null comment '权限ID' ); alter table sys_role_privilege comment '角色权限表';
给表添加外键
-- 给用户角色表的user_id添加外键 alter table sys_user_role add constraint fk_uru foreign key(user_id) references sys_user(id); -- 给用户角色表的role_id 添加外键 alter table sys_user_role add constraint fk_sysrur foreign key(role_id) references sys_role(id); -- 给角色权限表的role_id添加外键 alter table sys_role_privilege add constraint fk_sysrpp foreign key(privilege_id) references sys_privilege(id); -- 给角权限的privilege_id 添加外键 alter table sys_role_privilege add constraint fk_sysrpr foreign key(role_id) references sys_role(role_id);
插入数据
insert into sys_user(user_name,password,user_info,head_img,create_time )values('Jordan','123','管理员',null,'2018-1-22 12:02:01'),
('Alice','456','测试人员',null,'2018-1-22 12:02:01'),
('Alex','789','开发人员',null,'2018-1-22 12:02:01'),
('James','012','运维人员',null,'2018-1-22 12:02:01')
insert into sys_user_role(role_name,enabled,create_by,create_time) values('管理员',1,'1','2018-01-02'),values('普通用户',1,'1','2018-01-02');
insert into sys_privilege(privilege_name,privilege_url) values('用户管理','/users'),('角色管理','/roles'),('系统维护','/system'),('日志管理','/logs');
insert into sys_user_role values(1,1),(2,2),(3,2),(4,2);
insert into sys_role_privilege values(1,1),(2,3),(2,4);
四、使用逆向工程生成entity以及mapper接口和mapper文件
①:创建mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <package name="com.jordan.mybatis.entity"></package> </typeAliases> <environments default="defaultEnv"> <environment > <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="username" value="root"></property> <property name="password" value="123456"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="driver" value="com.mysql.jdbc.Driver"></property> </dataSource> </environment> </environments> <mappers> <package name="com.jordan.mybatis.mapper"></package> </mappers> </configuration>
②:SysUser.java
package com.jordan.mybatis.entity; import java.util.Arrays; import java.util.Date; import java.util.List; public class SysUser { private Long id; private String userName; private String password; private Date createTime; private String userInfo; private byte[] headImg; private SysRole sysRole; private List<SysRole> sysRoleList; public List<SysRole> getSysRoleList() { return sysRoleList; } public void setSysRoleList(List<SysRole> sysRoleList) { this.sysRoleList = sysRoleList; } public SysRole getSysRole() { return sysRole; } public void setSysRole(SysRole sysRole) { this.sysRole = sysRole; } 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 == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getUserInfo() { return userInfo; } public void setUserInfo(String userInfo) { this.userInfo = userInfo == null ? null : userInfo.trim(); } public byte[] getHeadImg() { return headImg; } public void setHeadImg(byte[] headImg) { this.headImg = headImg; } @Override public String toString() { return "SysUser{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", createTime=" + createTime + ", userInfo='" + userInfo + '\'' + ", headImg=" + Arrays.toString(headImg) + ", sysRole=" + sysRole + ", sysRoleList=" + sysRoleList + '}'; } }