导读
技术
- SpringBoot
- Shiro
- Mybatis
- Generator
- MySQL
1、创建表
创建用户表,角色表,权限表,用户角色表,角色权限表五张表 ```sql drop table if exists permission;
drop table if exists role;
drop table if exists role_to_permission;
drop table if exists user_to_role;
/==============================================================/ / Table: permission / /==============================================================/ create table permission ( id int not null auto_increment, name varchar(50) comment ‘权限名称’, url varchar(100) not null comment ‘请求路径’, permission varchar(150) not null comment ‘角色编码’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int default 0, remark varchar(50) default ‘’, primary key (id) );
alter table permission comment ‘权限表’;
/==============================================================/ / Table: role / /==============================================================/ create table role ( id int not null auto_increment, role_name varchar(100) not null comment ‘角色名称’, role_code varchar(150) not null comment ‘角色编码’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, remark varchar(50) default ‘’, primary key (id) );
alter table role comment ‘角色表’;
/==============================================================/ / Table: role_to_permission / /==============================================================/ create table role_to_permission ( id int not null auto_increment, permission_id varchar(100) not null comment ‘权限id’, role_id varchar(150) not null comment ‘角色id’, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );
alter table role_to_permission comment ‘角色与权限表’;
/==============================================================/ / Table: user_to_role / /==============================================================/ create table user_to_role ( id int not null auto_increment, user_id varchar(100) not null, role_id varchar(150) not null, status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );
alter table user_to_role comment ‘用户角色表’;
create table user ( id int not null auto_increment, user_id varchar(100) not null, username varchar(150) not null, password varchar(150) not null, real_name varchar(150), address varchar(150), status tinyint(1) default 1, created datetime default CURRENT_TIMESTAMP, createdby int, updated datetime default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, updatedby int, primary key (id) );
alter table user comment ‘用户表’;
SQL数据:
```sql
DELIMITER ;
delete from sys_user;
delete from sys_role;
delete from sys_resource;
delete from sys_organization;
insert into sys_user values(1,1,'admin','d3c59d25033dbf980d29554025c23a75','8d78869f470951332959580424d4bf4f', '1', false);
insert into sys_organization values(1, '总公司', 0, '0/', true);
insert into sys_organization values(2, '分公司1', 1, '0/1/', true);
insert into sys_organization values(3, '分公司2', 1, '0/1/', true);
insert into sys_organization values(4, '分公司11', 2, '0/1/2/', true);
insert into sys_resource values(1, '资源', 'menu', '', 0, '0/', '', true);
insert into sys_resource values(11, '组织机构管理', 'menu', '/organization', 1, '0/1/', 'organization:*', true);
insert into sys_resource values(12, '组织机构新增', 'button', '', 11, '0/1/11/', 'organization:create', true);
insert into sys_resource values(13, '组织机构修改', 'button', '', 11, '0/1/11/', 'organization:update', true);
insert into sys_resource values(14, '组织机构删除', 'button', '', 11, '0/1/11/', 'organization:delete', true);
insert into sys_resource values(15, '组织机构查看', 'button', '', 11, '0/1/11/', 'organization:view', true);
insert into sys_resource values(21, '用户管理', 'menu', '/user', 1, '0/1/', 'user:*', true);
insert into sys_resource values(22, '用户新增', 'button', '', 21, '0/1/21/', 'user:create', true);
insert into sys_resource values(23, '用户修改', 'button', '', 21, '0/1/21/', 'user:update', true);
insert into sys_resource values(24, '用户删除', 'button', '', 21, '0/1/21/', 'user:delete', true);
insert into sys_resource values(25, '用户查看', 'button', '', 21, '0/1/21/', 'user:view', true);
insert into sys_resource values(31, '资源管理', 'menu', '/resource', 1, '0/1/', 'resource:*', true);
insert into sys_resource values(32, '资源新增', 'button', '', 31, '0/1/31/', 'resource:create', true);
insert into sys_resource values(33, '资源修改', 'button', '', 31, '0/1/31/', 'resource:update', true);
insert into sys_resource values(34, '资源删除', 'button', '', 31, '0/1/31/', 'resource:delete', true);
insert into sys_resource values(35, '资源查看', 'button', '', 31, '0/1/31/', 'resource:view', true);
insert into sys_resource values(41, '角色管理', 'menu', '/role', 1, '0/1/', 'role:*', true);
insert into sys_resource values(42, '角色新增', 'button', '', 41, '0/1/41/', 'role:create', true);
insert into sys_resource values(43, '角色修改', 'button', '', 41, '0/1/41/', 'role:update', true);
insert into sys_resource values(44, '角色删除', 'button', '', 41, '0/1/41/', 'role:delete', true);
insert into sys_resource values(45, '角色查看', 'button', '', 41, '0/1/41/', 'role:view', true);
insert into sys_resource values(51, '会话管理', 'menu', '/sessions', 1, '0/1/', 'session:*', true);
insert into sys_role values(1, 'admin', '超级管理员', '11,21,31,41,51', true);
2、使用mybatis的逆向生成文件
1、引入依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<plugins>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 配置文件路径 -->
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
2、generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry
location="G:\apache-maven-3.6.0\package\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/junk-classes?serverTimezone=UTC"
userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类的包名和位置 ,targetPackage指的是包名,targetProject值得是路径位置-->
<javaModelGenerator targetPackage="com.ck.junk.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ck.junk.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<!--</table>-->
<!--<table tableName="junk_infos" domainObjectName="JunkInfosModel" enableCountByExample="false"-->
<!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--selectByExampleQueryId="false">-->
<!--<property name="my.isgen.usekeys" value="true"/>-->
<!--<property name="useActualColumnNames" value="false"/>-->
<!--<generatedKey column="id" sqlStatement="JDBC"/>-->
<!--</table>-->
<!--<table tableName="junk_oper_log" domainObjectName="JunkOperLogModel" enableCountByExample="false"-->
<!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--selectByExampleQueryId="false">-->
<!--<property name="my.isgen.usekeys" value="true"/>-->
<!--<property name="useActualColumnNames" value="false"/>-->
<!--<generatedKey column="id" sqlStatement="JDBC"/>-->
<!--</table>-->
<!--<table tableName="junk_types" domainObjectName="JunkTypesModel" enableCountByExample="false"-->
<!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--selectByExampleQueryId="false">-->
<!--<property name="my.isgen.usekeys" value="true"/>-->
<!--<property name="useActualColumnNames" value="false"/>-->
<!--<generatedKey column="id" sqlStatement="JDBC"/>-->
<!--</table>-->
<table tableName="junk_leave_meg" domainObjectName="JunkLeaveMegModel" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="my.isgen.usekeys" value="true"/>
<property name="useActualColumnNames" value="false"/>
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
3、执行 mybatis-generator:generate
命令
4、运行结果
虽然出现几个问题,but最后成功了,以上是生成结果。
3、问题详解
问题描述1:
出现http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd
报红
原因:缺少相关依赖或者配置
解决办法:
1、引入 mybatis-generator-core
依赖:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2、在pom.xml文件添加:
<plugins>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 配置文件路径 -->
<configurationFile>${basedir}/src/main/resources/${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
或者在setting—>Schemas and DTDs中 添加相应的URL
问题得以解决。
问题描述2:
原因:xml文件内容重复
如下所示,生成的xml文件,内容重复。
解决办法:删除就可以了
或者将其属性设置为false
<commentGenerator>
<!--<!– 是否去除自动生成的注释 true:是 : false:否 –>-->
<property name="suppressAllComments" value="false"/>
<!--<!– 是否去除所有自动生成的文件的时间戳,默认为false –>-->
<property name="suppressDate" value="true"/>
</commentGenerator>
4、Shiro权限验证
问题描述2:
执行 mybatis-generator:generate
命令出现以下错误:
原因:MySQL包放错位置
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ shiro-demo ---
[INFO] Connecting to the Database
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.094 s
[INFO] Finished at: 2019-05-21T13:04:22+08:00
[INFO] Final Memory: 13M/232M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project shiro-demo: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
解决方案:
将MySQL的依赖放到这个下面即可,或者mysql的版本不对,请自行查看。
<!--mybatis.generator逆向生成Sql依赖-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
</dependencies>
</plugin>