一、创建项目
首先得用idea创建一个maven项目。
二、修改pom文件
在 pom 文件中添加依赖:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
在 pom 文件中添加 mybatis-generator-maven-plugin 插件:
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
三、添加配置文件
在 src/main/resources 目录下,添加文件 mybatis-generator.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>
<!-- 填写mysql-connector-java的jar包所在位置-->
<classPathEntry location="D:\develop\apache-maven-3.6.2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
<context id="mysql" targetRuntime="MyBatis3">
<!--<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>-->
<!-- 配置在逆向生成时不需要有他们默认的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/trademarket?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"
userId="root"
password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!-- 指定javabean生成的位置
targetPackage:在哪个包下生成
targetProject:在哪个工程下
-->
<javaModelGenerator targetPackage="com.qq.model" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="false" />
</javaModelGenerator>
<!-- 指定sql映射文件生成的位置 -->
<sqlMapGenerator targetPackage="resources.mapper"
targetProject="src/main">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定到接口生成的位置 ,mapper接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.qq.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定每个表的生成策略 -->
<table tableName="tbl_user" domainObjectName="User"/>
</context>
</generatorConfiguration>
四、运行
点击idea右边的 Maven ,然后找到我用红色框框起来的部分,双击运行。
五、结果
当你看到控制台输出 BUILD SUCCESS ,则表明已经自动生成成功了。
F:\qq\java\jdk1.8\bin\java.exe -Dmaven.multiModuleProjectDirectory=F:\qq\trademarket "-Dmaven.home=F:\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=F:\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:F:\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=52561:F:\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "F:\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building trademarket-core 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ trademarket-core ---
[INFO] Connecting to the Database
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
[INFO] Introspecting table tbl_bill
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Introspecting table tbl_user
[INFO] Generating Example class for table tbl_user
[INFO] Generating Record class for table tbl_user
[INFO] Generating Mapper Interface for table tbl_user
[INFO] Generating SQL Map for table tbl_user
[INFO] Saving file UserMapper.xml
[INFO] Saving file UserExample.java
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.948 s
[INFO] Finished at: 2019-12-27T14:30:05+08:00
[INFO] Final Memory: 15M/203M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
如遇到什么问题,可以在下方留言区进行讨论。如果觉得本文写的还可以,不妨点个赞 收藏以下吧~