利用generator自动生成model(实体)、dao(接口)、mapper(映射)

时间:2023-03-09 20:59:43
利用generator自动生成model(实体)、dao(接口)、mapper(映射)

1 在MySQL数据库中创建相应的表

/*
Navicat MySQL Data Transfer Source Server : 虚拟机_zeus01
Source Server Version : 50717
Source Host : 10.24.3.108:3306
Source Database : security Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001 Date: 2017-12-10 16:44:58
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for `sys_acl`
-- ----------------------------
DROP TABLE IF EXISTS `sys_acl`;
CREATE TABLE `sys_acl` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '权限ID',
`code` varchar(20) NOT NULL DEFAULT '' COMMENT '权限码',
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '权限名称',
`acl_module_id` int(11) NOT NULL DEFAULT '0' COMMENT '权限所在的权限模块ID',
`url` varchar(100) NOT NULL DEFAULT '' COMMENT '请求的URL,可以填正则表达式',
`type` int(11) NOT NULL DEFAULT '3' COMMENT '类型,1:菜单,2:按钮,3:其他',
`status` int(11) NOT NULL DEFAULT '1' COMMENT '状态,1:正常,0:冻结',
`seq` int(11) NOT NULL DEFAULT '0' COMMENT '权限在当前模块的顺序,有效到大',
`remark` varchar(200) DEFAULT '' COMMENT '备注',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者的IP地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_acl
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_acl_module`
-- ----------------------------
DROP TABLE IF EXISTS `sys_acl_module`;
CREATE TABLE `sys_acl_module` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '权限模块ID',
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '权限模块名称',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '上级权限模块ID',
`level` varchar(200) NOT NULL DEFAULT '' COMMENT '权限模块层级',
`seq` int(11) NOT NULL DEFAULT '0' COMMENT '权限模块在当前层级的顺序,由小到大',
`status` int(11) NOT NULL DEFAULT '1' COMMENT '状态,1:正常,0:冻结',
`remark` varchar(200) DEFAULT '' COMMENT '备注',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后一次更新的操作者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次修改时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后一次更新操作者的IP地址 ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_acl_module
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_dept`
-- ----------------------------
DROP TABLE IF EXISTS `sys_dept`;
CREATE TABLE `sys_dept` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '部门ID',
`name` varchar(20) NOT NULL DEFAULT '' COMMENT '部门名称',
`parent_id` int(11) NOT NULL DEFAULT '0' COMMENT '上级部门ID',
`level` varchar(200) NOT NULL DEFAULT '' COMMENT '部门层级',
`seq` int(11) NOT NULL DEFAULT '0' COMMENT '部门在当前层级的顺序,由小到大',
`remark` varchar(200) DEFAULT '' COMMENT '备注',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后一次更新的操作者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次修改时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后一次更新操作者的IP地址 ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_dept
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_log`
-- ----------------------------
DROP TABLE IF EXISTS `sys_log`;
CREATE TABLE `sys_log` (
` id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL DEFAULT '0' COMMENT '权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系',
`target_id` int(11) NOT NULL COMMENT '基于type后制定的对象ID,比如用户、权限、角色等表的主键',
`old_value` text COMMENT '原始值',
`new_value` text COMMENT '新值',
`status` int(11) NOT NULL DEFAULT '0' COMMENT '当前是否复原过,0:没有,1:复原过',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者的IP地址',
PRIMARY KEY (` id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_log
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_role`
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`name` varchar(20) NOT NULL DEFAULT '',
`type` int(11) NOT NULL DEFAULT '1' COMMENT '角色类型,1:管理角色,2:其他',
`status` int(11) NOT NULL DEFAULT '1' COMMENT '状态,1:可用,0:冻结',
`remark` varchar(200) DEFAULT '' COMMENT '备注',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者的IP地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_role
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_role_acl`
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_acl`;
CREATE TABLE `sys_role_acl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL COMMENT '角色ID',
`acl_id` int(11) NOT NULL COMMENT '权限ID',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者的IP地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_role_acl
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_role_user`
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_user`;
CREATE TABLE `sys_role_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL COMMENT '角色ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者的IP地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_role_user
-- ---------------------------- -- ----------------------------
-- Table structure for `sys_user`
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名称',
`telephone` varchar(13) NOT NULL DEFAULT '' COMMENT '手机号',
`email` varchar(20) NOT NULL DEFAULT '' COMMENT '邮箱',
`password` varchar(40) NOT NULL DEFAULT '' COMMENT '加密后的密码',
`dept_id` int(11) NOT NULL DEFAULT '0' COMMENT '所在部门的ID',
`status` int(11) NOT NULL DEFAULT '1' COMMENT '状态, 1:正常,0:冻结,2:删除',
`remark` varchar(200) DEFAULT '' COMMENT '备注',
`operator` varchar(20) NOT NULL DEFAULT '' COMMENT '最后更新者',
`operator_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`operator_ip` varchar(20) NOT NULL DEFAULT '' COMMENT '最后一次更新者的IP',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ----------------------------
-- Records of sys_user
-- ----------------------------

create.sql

2 导入generator工具文件夹到springMVC项目根目录下

生成工具包:点击获取

3 根据自己的项目修改生成配置文件

<?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="F:\javaProgramming\springCloud\permission\generator\mysql-connector-java-5.1.34.jar" /> <!-- 1 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://10.24.3.108:3306/security?characterEncoding=utf8" userId="root" password="123456"> <!-- 2 -->
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 --> <!-- 3 -->
<javaModelGenerator targetPackage="cn.xiangxu.model" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 --> <!-- 4 -->
<sqlMapGenerator targetPackage="cn.xiangxu.mapper" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 --> <!-- 5 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.xiangxu.dao" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) --><!-- 6 -->
<table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl" domainObjectName="SysAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl_module" domainObjectName="SysAclModule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role" domainObjectName="SysRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_acl" domainObjectName="SysRoleAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_user" domainObjectName="SysRoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>

生成文件

  3.1 修改驱动包的位置

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

<classPathEntry location="F:\javaProgramming\springCloud\permission\generator\mysql-connector-java-5.1.34.jar" /> <!-- 1 -->

  3.2 修改数据库连接信息

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://IP地址:端口/数据库?characterEncoding=utf8" userId="用户名" password="密码">  <!-- 2 -->

  3.3 修改生成文件的包名和存放路径

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

<!-- 生成模型的包名和位置 --> <!-- 3 -->
<javaModelGenerator targetPackage="cn.xiangxu.model" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 --> <!-- 4 -->
<sqlMapGenerator targetPackage="cn.xiangxu.mapper" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 --> <!-- 5 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.xiangxu.dao" targetProject="F:\javaProgramming\springCloud\permission\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>

  3.4 设置数据库表名和model、dao、mapper的对应关系

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

<!-- 要生成那些表(更改tableName和domainObjectName就可以) --><!-- 6 -->
<table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl" domainObjectName="SysAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_acl_module" domainObjectName="SysAclModule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role" domainObjectName="SysRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_acl" domainObjectName="SysRoleAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_role_user" domainObjectName="SysRoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />

  3.5 执行生成语句

    java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

 

4 生成效果

  利用generator自动生成model(实体)、dao(接口)、mapper(映射)

  执行成功后控制台输出

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

  4.1 执行前的目录结构

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)

  4.2 执行后的目录结构

    利用generator自动生成model(实体)、dao(接口)、mapper(映射)