一、创建一个maven工程
二、引入mybatis-generator插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
</build>
- 完整代码
<?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.yang</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 使用jdk1.8 -->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
</build>
</project>
三、创建mybatis-generator配置文件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>
<!-- location 输入MySQL驱动位置,绝对路径,如下 -->
<classPathEntry
location="D:\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar"/>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 表名前后加上``的分隔符 -->
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 配置生成pojo的序列化的插件-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 配置生成toString的序列化的插件 -->
<!-- <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>-->
<commentGenerator>
<!--去掉所有的注释-->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/xxx?serverTimezone=UTC"
userId="root" password="xxx">
<!-- mysql8.0版本以上要加上 nullCatalogMeansCurrent
否则会查询所有的数据库里面的表
-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- targetPackage:生成的持久化对象(实体)所在包
targetProject:包所在路径,可填绝对路径
-->
<javaModelGenerator targetPackage="com.yang.domain"
targetProject="D:\mybatis-generator\src\main\java">
<!-- 是否允许子包,即targetPackage.SchemaName.tableName -->
<property name="enableSubPackages" value="false"></property>
<!-- 是否对model 添加构造函数 true添加 false不添加 -->
<property name="constructorBased" value="true"></property>
<!-- 去掉字符串前后的空格 -->
<property name="trimStrings" value="true"></property>
<!-- 建议的Model对象是否不可改变
即生成的对象会不会生成 setter 方法,false表示会生成setter方法
-->
<property name="immutable" value="false"></property>
</javaModelGenerator>
<!-- %表示所有表 -->
<table tableName="%" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="true" enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
配置详细信息请前往官网:http://mybatis.org/generator/configreference/xmlconfig.html
四、执行插件
-
执行结果
-
-