MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器。MBG能够自动生成实体类、Mapper接口以及对应的XML文件,能够在一定程度上减轻开发人员的工作量。本文介绍了使用MBG Maven插件的使用方法。
- 在Maven项目中,新建一个子模块mybatis-generator。
- mybatis-generator子模块的pom文件配置如下:
<?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">
<parent>
<artifactId>qbasic-portal</artifactId>
<groupId>hyzx</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-generator</artifactId>
<properties>
<mbg.version>1.3.7</mbg.version>
<!-- Java接口和实体类生成路径
targetMapperPackage是生成的接口包名
targetModelPackage是生成的实体类包名
-->
<targetJavaProject>${basedir}/src/main/java</targetJavaProject>
<targetMapperPackage>com.hyzx.qbasic.domain.dao</targetMapperPackage>
<targetModelPackage>com.hyzx.qbasic.common.model.po</targetModelPackage>
<!-- XML生成路径 -->
<targetResourcesProject>${basedir}/src/main/java</targetResourcesProject>
<targetXMLPackage>mapper</targetXMLPackage>
</properties>
<dependencies>
<!-- 因为我的项目是spring boot项目,非spring boot项目改成mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- MBG插件 -->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mbg.version}</version>
<configuration>
<!-- MBG配置文件路径 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- resources目录中添加MBG配置文件。
<?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>
<!-- mysql数据源配置文件路径 -->
<properties resource="mysql.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--autoDelimitKeywords,当表名或者字段名为SQL关键字的时候,可以设置该属性为true,
MBG会自动给表名或字段名添加分隔符-->
<property name="autoDelimitKeywords" value="true"/>
<!--beginningDelimiter和endingDelimiter的默认值为双引号("),
在Mysql中不能这么写,所以还要将这两个默认值改为反单引号(`)-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<!--阻止生成的注释包含时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
</jdbcConnection>
<!--
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类生成路径 -->
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<!-- XML生成路径 -->
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<!-- 接口生成路径 -->
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}"
type="XMLMAPPER"/>
<table tableName="%">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
- resources目录中添加mysql数据源配置文件。
jdbc.driver=com.mysql.jdbc.Driver
#数据库连接字符串
db.url=jdbc:mysql://127.0.0.1:3306/qbasic-portal?useUnicode=true&characterEncoding=utf8&useSSL=false
db.username=root
db.password=root
-
在项目目录下,执行mvn mybatis-generator:generate命令,会在src目录下自动生成实体类、Mapper接口以及对应的XML文件。
IntelliJ Idea - 生成的实体类如下:
import java.util.Date;
public class SysParam {
private Integer pId;
private String pType;
private String pKey;
private String pValue;
private Integer sort;
private String remark;
private Date createTime;
private Date updateTime;
public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getpType() {
return pType;
}
public void setpType(String pType) {
this.pType = pType;
}
public String getpKey() {
return pKey;
}
public void setpKey(String pKey) {
this.pKey = pKey;
}
public String getpValue() {
return pValue;
}
public void setpValue(String pValue) {
this.pValue = pValue;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
- 生成的接口如下:
import com.hyzx.qbasic.common.model.po.SysParam;
import java.util.List;
public interface SysParamMapper {
int deleteByPrimaryKey(Integer pId);
int insert(SysParam record);
SysParam selectByPrimaryKey(Integer pId);
List<SysParam> selectAll();
int updateByPrimaryKey(SysParam record);
}
- 生成的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="com.hyzx.qbasic.domain.dao.SysParamMapper">
<resultMap id="BaseResultMap" type="com.hyzx.qbasic.common.model.po.SysParam">
<id column="pId" jdbcType="INTEGER" property="pId" />
<result column="pType" jdbcType="VARCHAR" property="pType" />
<result column="pKey" jdbcType="VARCHAR" property="pKey" />
<result column="pValue" jdbcType="VARCHAR" property="pValue" />
<result column="sort" jdbcType="INTEGER" property="sort" />
<result column="remark" jdbcType="VARCHAR" property="remark" />
<result column="createTime" jdbcType="TIMESTAMP" property="createTime" />
<result column="updateTime" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from sys_param
where pId = #{pId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.hyzx.qbasic.common.model.po.SysParam">
insert into sys_param (pId, pType, pKey,
pValue, sort, remark,
createTime, updateTime)
values (#{pId,jdbcType=INTEGER}, #{pType,jdbcType=VARCHAR}, #{pKey,jdbcType=VARCHAR},
#{pValue,jdbcType=VARCHAR}, #{sort,jdbcType=INTEGER}, #{remark,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
</insert>
<update id="updateByPrimaryKey" parameterType="com.hyzx.qbasic.common.model.po.SysParam">
update sys_param
set pType = #{pType,jdbcType=VARCHAR},
pKey = #{pKey,jdbcType=VARCHAR},
pValue = #{pValue,jdbcType=VARCHAR},
sort = #{sort,jdbcType=INTEGER},
remark = #{remark,jdbcType=VARCHAR},
createTime = #{createTime,jdbcType=TIMESTAMP},
updateTime = #{updateTime,jdbcType=TIMESTAMP}
where pId = #{pId,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select pId, pType, pKey, pValue, sort, remark, createTime, updateTime
from sys_param
where pId = #{pId,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select pId, pType, pKey, pValue, sort, remark, createTime, updateTime
from sys_param
</select>
</mapper>
最后,自动生成的类名、方法名称和SQL语句需要根据自己项目的开发规范,做适当调整。
网友评论