一、简介
mybatis逆向工程是常用的生成代码工具,在jdk8之后提供了一套新的API,可以将数据库date类型的生成LocalDate的java类型,将数据库datetime类型的生成LocalDateTime类型, 在xml中生成timeStamp类型。
该工程我放到了github上,链接为:https://github.com/1956025812/mybatis-generator.git
二、搭建步骤
2.1 项目结构图
2.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis</groupId>
<artifactId>generator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 使用jdk1.8进行编译 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<!--
用maven mybatis插件如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
在plugin外部得jar包,他不会去找到并执行,所以要把plugin运行依赖得jar配置都放在里面
-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.3 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>
<context id="testTables" targetRuntime="MyBatis3">
<!-- 逆向生成的实体默认实现序列化接口和添加serialVersionUID字段 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址(使用jdk8的时间API必须serverTimezone=UTC)、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://IP:3306/privilege_write?serverTimezone=UTC"
userId="用户名"
password="密码">
<!-- 生成的包含ByPrimaryKey的方法 如果没有改配置 不会生成 -->
<property name="useInformationSchema" value="true"/>
<property name="remarks" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
<!-- 此属性用于指定MyBatis生成器是否应强制对日期、时间和和时间戳字段,生成JDK8的时间类型,而不是使用java.util.date -->
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.mybatis.generator.entity" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射XML文件生成的位置 -->
<sqlMapGenerator targetPackage="com.mybatis.generator.mapper" targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.generator.mapper"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 注意:使用完之后置空-->
<table tableName="sys_role" domainObjectName="SysRole"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" enableDeleteByPrimaryKey="false"
>
</table>
</context>
</generatorConfiguration>
2.4 配置mvn命令
三、生成DEMO
3.0 表结构
CREATE TABLE `sys_role` (
`rid` varchar(32) NOT NULL COMMENT '主键',
`role_name` varchar(128) DEFAULT NULL COMMENT '角色名称',
`description` varchar(256) DEFAULT NULL COMMENT '角色描述',
`sid` varchar(32) DEFAULT NULL COMMENT '系统ID',
`system_key` varchar(128) DEFAULT NULL COMMENT '系统标识',
`parent_rid` varchar(32) DEFAULT NULL COMMENT '父角色ID',
`state` int(11) DEFAULT NULL COMMENT '状态: 0:删除, 1:启用, 2:禁用',
`create_uid` varchar(32) DEFAULT NULL COMMENT '创建人ID',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_uid` varchar(32) DEFAULT NULL COMMENT '修改人ID',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`rid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.1 POJO(时间是LocalDateTime类型)
package com.mybatis.generator.entity;
import java.io.Serializable;
import java.time.LocalDateTime;
public class SysRole implements Serializable {
private String rid;
private String roleName;
private String description;
private String sid;
private String systemKey;
private String parentRid;
private Integer state;
private String createUid;
private LocalDateTime createTime;
private String updateUid;
private LocalDateTime updateTime;
private static final long serialVersionUID = 1L;
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid == null ? null : rid.trim();
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName == null ? null : roleName.trim();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid == null ? null : sid.trim();
}
public String getSystemKey() {
return systemKey;
}
public void setSystemKey(String systemKey) {
this.systemKey = systemKey == null ? null : systemKey.trim();
}
public String getParentRid() {
return parentRid;
}
public void setParentRid(String parentRid) {
this.parentRid = parentRid == null ? null : parentRid.trim();
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCreateUid() {
return createUid;
}
public void setCreateUid(String createUid) {
this.createUid = createUid == null ? null : createUid.trim();
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public String getUpdateUid() {
return updateUid;
}
public void setUpdateUid(String updateUid) {
this.updateUid = updateUid == null ? null : updateUid.trim();
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
}
3.2 DAO接口
package com.mybatis.generator.mapper;
import com.mybatis.generator.entity.SysRole;
public interface SysRoleMapper {
int insert(SysRole record);
int insertSelective(SysRole record);
SysRole selectByPrimaryKey(String rid);
int updateByPrimaryKeySelective(SysRole record);
int updateByPrimaryKey(SysRole record);
}
3.4 xml文件(时间是TimeStamp类型)
<?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="com.mybatis.generator.mapper.SysRoleMapper">
<resultMap id="BaseResultMap" type="com.mybatis.generator.entity.SysRole">
<id column="rid" jdbcType="VARCHAR" property="rid" />
<result column="role_name" jdbcType="VARCHAR" property="roleName" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="sid" jdbcType="VARCHAR" property="sid" />
<result column="state" jdbcType="INTEGER" property="state" />
<result column="create_uid" jdbcType="VARCHAR" property="createUid" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_uid" jdbcType="VARCHAR" property="updateUid" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap>
<sql id="Base_Column_List">
rid, role_name, description, sid, state, create_uid, create_time, update_uid, update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_role
where rid = #{rid,jdbcType=VARCHAR}
</select>
<insert id="insert" parameterType="com.mybatis.generator.entity.SysRole">
insert into sys_role (rid, role_name, description,
sid, state, create_uid,
create_time, update_uid, update_time
)
values (#{rid,jdbcType=VARCHAR}, #{roleName,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR},
#{sid,jdbcType=VARCHAR}, #{state,jdbcType=INTEGER}, #{createUid,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateUid,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}
)
</insert>
<insert id="insertSelective" parameterType="com.mybatis.generator.entity.SysRole">
insert into sys_role
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="rid != null">
rid,
</if>
<if test="roleName != null">
role_name,
</if>
<if test="description != null">
description,
</if>
<if test="sid != null">
sid,
</if>
<if test="state != null">
state,
</if>
<if test="createUid != null">
create_uid,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateUid != null">
update_uid,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="rid != null">
#{rid,jdbcType=VARCHAR},
</if>
<if test="roleName != null">
#{roleName,jdbcType=VARCHAR},
</if>
<if test="description != null">
#{description,jdbcType=VARCHAR},
</if>
<if test="sid != null">
#{sid,jdbcType=VARCHAR},
</if>
<if test="state != null">
#{state,jdbcType=INTEGER},
</if>
<if test="createUid != null">
#{createUid,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateUid != null">
#{updateUid,jdbcType=VARCHAR},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.mybatis.generator.entity.SysRole">
update sys_role
<set>
<if test="roleName != null">
role_name = #{roleName,jdbcType=VARCHAR},
</if>
<if test="description != null">
description = #{description,jdbcType=VARCHAR},
</if>
<if test="sid != null">
sid = #{sid,jdbcType=VARCHAR},
</if>
<if test="state != null">
state = #{state,jdbcType=INTEGER},
</if>
<if test="createUid != null">
create_uid = #{createUid,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateUid != null">
update_uid = #{updateUid,jdbcType=VARCHAR},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where rid = #{rid,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.mybatis.generator.entity.SysRole">
update sys_role
set role_name = #{roleName,jdbcType=VARCHAR},
description = #{description,jdbcType=VARCHAR},
sid = #{sid,jdbcType=VARCHAR},
state = #{state,jdbcType=INTEGER},
create_uid = #{createUid,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_uid = #{updateUid,jdbcType=VARCHAR},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where rid = #{rid,jdbcType=VARCHAR}
</update>
<resultMap id="BaseResultMap" type="com.mybatis.generator.entity.SysRole">
<id column="rid" jdbcType="VARCHAR" property="rid" />
<result column="role_name" jdbcType="VARCHAR" property="roleName" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="sid" jdbcType="VARCHAR" property="sid" />
<result column="system_key" jdbcType="VARCHAR" property="systemKey" />
<result column="parent_rid" jdbcType="VARCHAR" property="parentRid" />
<result column="state" jdbcType="INTEGER" property="state" />
<result column="create_uid" jdbcType="VARCHAR" property="createUid" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_uid" jdbcType="VARCHAR" property="updateUid" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap>
<sql id="Base_Column_List">
rid, role_name, description, sid, system_key, parent_rid, state, create_uid, create_time,
update_uid, update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_role
where rid = #{rid,jdbcType=VARCHAR}
</select>
<insert id="insert" parameterType="com.mybatis.generator.entity.SysRole">
insert into sys_role (rid, role_name, description,
sid, system_key, parent_rid,
state, create_uid, create_time,
update_uid, update_time)
values (#{rid,jdbcType=VARCHAR}, #{roleName,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR},
#{sid,jdbcType=VARCHAR}, #{systemKey,jdbcType=VARCHAR}, #{parentRid,jdbcType=VARCHAR},
#{state,jdbcType=INTEGER}, #{createUid,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
#{updateUid,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.mybatis.generator.entity.SysRole">
insert into sys_role
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="rid != null">
rid,
</if>
<if test="roleName != null">
role_name,
</if>
<if test="description != null">
description,
</if>
<if test="sid != null">
sid,
</if>
<if test="systemKey != null">
system_key,
</if>
<if test="parentRid != null">
parent_rid,
</if>
<if test="state != null">
state,
</if>
<if test="createUid != null">
create_uid,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateUid != null">
update_uid,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="rid != null">
#{rid,jdbcType=VARCHAR},
</if>
<if test="roleName != null">
#{roleName,jdbcType=VARCHAR},
</if>
<if test="description != null">
#{description,jdbcType=VARCHAR},
</if>
<if test="sid != null">
#{sid,jdbcType=VARCHAR},
</if>
<if test="systemKey != null">
#{systemKey,jdbcType=VARCHAR},
</if>
<if test="parentRid != null">
#{parentRid,jdbcType=VARCHAR},
</if>
<if test="state != null">
#{state,jdbcType=INTEGER},
</if>
<if test="createUid != null">
#{createUid,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateUid != null">
#{updateUid,jdbcType=VARCHAR},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.mybatis.generator.entity.SysRole">
update sys_role
<set>
<if test="roleName != null">
role_name = #{roleName,jdbcType=VARCHAR},
</if>
<if test="description != null">
description = #{description,jdbcType=VARCHAR},
</if>
<if test="sid != null">
sid = #{sid,jdbcType=VARCHAR},
</if>
<if test="systemKey != null">
system_key = #{systemKey,jdbcType=VARCHAR},
</if>
<if test="parentRid != null">
parent_rid = #{parentRid,jdbcType=VARCHAR},
</if>
<if test="state != null">
state = #{state,jdbcType=INTEGER},
</if>
<if test="createUid != null">
create_uid = #{createUid,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateUid != null">
update_uid = #{updateUid,jdbcType=VARCHAR},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where rid = #{rid,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.mybatis.generator.entity.SysRole">
update sys_role
set role_name = #{roleName,jdbcType=VARCHAR},
description = #{description,jdbcType=VARCHAR},
sid = #{sid,jdbcType=VARCHAR},
system_key = #{systemKey,jdbcType=VARCHAR},
parent_rid = #{parentRid,jdbcType=VARCHAR},
state = #{state,jdbcType=INTEGER},
create_uid = #{createUid,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_uid = #{updateUid,jdbcType=VARCHAR},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where rid = #{rid,jdbcType=VARCHAR}
</update>
</mapper>
四、使用
4.1 Controller
@Api(tags = {"SysRoleController"}, description = "角色Controller")
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {
@Autowired
private SysRoleWriteService sysRoleWriteService;
@ApiOperation("新增系统对象")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "body", dataType = "String", name = "loginUid", value = "登录用户ID", required = true),
@ApiImplicitParam(paramType = "body", dataType = "String", name = "roleName", value = "系统名称", required = false),
@ApiImplicitParam(paramType = "body", dataType = "String", name = "description", value = "描述", required = false),
@ApiImplicitParam(paramType = "body", dataType = "String", name = "sid", value = "系统ID", required = false),
@ApiImplicitParam(paramType = "body", dataType = "String", name = "systemKey", value = "系统标识", required = false),
@ApiImplicitParam(paramType = "body", dataType = "String", name = "parentRid", value = "父角色ID", required = false)
})
@PostMapping("/save")
public ResultVO<Void> saveSysRole(@RequestBody @ApiIgnore SysRoleVO sysRoleVO) throws Exception {
return this.sysRoleWriteService.saveSysRole(sysRoleVO);
}
}
4.2 Service
public interface SysRoleWriteService {
/**
* 新增角色
*
* @param sysRoleVO
* @return VOID
* @throws Exception
*/
ResultVO<Void> saveSysRole(SysRoleVO sysRoleVO) throws Exception;
}
4.3 ServiceImpl
@Service
public class SysRoleWriteServiceImpl implements SysRoleWriteService {
@Autowired
private SysRoleWriteMapper sysRoleWriteMapper;
@Transactional(rollbackFor = Exception.class)
@Override
public ResultVO<Void> saveSysRole(SysRoleVO sysRoleVO) throws Exception {
SysRole sysRole = new SysRole();
sysRole.setRid(CommonUtil.generatorId());
sysRole.setRoleName(sysRoleVO.getRoleName());
sysRole.setDescription(sysRoleVO.getDescription());
sysRole.setSid(sysRoleVO.getSid());
sysRole.setSystemKey(sysRoleVO.getSystemKey());
sysRole.setParentRid(sysRoleVO.getParentRid());
sysRole.setState(1);
sysRole.setCreateUid(sysRoleVO.getLoginUid());
sysRole.setCreateTime(LocalDateTime.now());
this.sysRoleWriteMapper.insertSelective(sysRole);
return ResultVO.getSuccess("新增角色成功");
}
}
新增成功~
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/17754.html