在Pom文件中添加依赖
- 在build 下的plugins中添加下述代码
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
<!--配置文件-->
<configuration>
<configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
- 在resources目录下添加generator 配置文件
<?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>
<!-- 具体配置内容 -->
<!--id:自定义-->
<context id="mybatisConfig" targetRuntime="MyBatis3">
<!--禁用注释-->
<commentGenerator>
<!--是否去除自动生成的注释,true代表是-->
<property name="suppressAllComments" value="true"/>
<!-- <property name="suppressDate" value="true"/>-->
</commentGenerator>
<!--连接数据库,useSSL=false解决jdbd与mysql版本兼容问题-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useSSL=false"
userId="root" password="123456"/>
<!--默认false,把jdbc DECIMAL和NUMERIC类型解析为Integer-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--targetProject:生成po类的位置-->
<javaModelGenerator targetPackage="cn.whu.springboot_mybatis_demo.pojo" targetProject=".\src\main\java">
<!--enableSubPackages:是否让schema作为包的后缀-->
<property name="enableSubPackage" value="true"/>
<!--从数据库返回的值被清理前后的空格-->
<property name="trimString" value="true"/>
</javaModelGenerator>
<!--mapper映射文件-->
<sqlMapGenerator targetPackage="cn.whu.springboot_mybatis_demo.mapper" targetProject=".\src\main\java">
<property name="enableSubPackage" value="false" />
</sqlMapGenerator>
<!--mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.whu.springboot_mybatis_demo.mapper" targetProject=".\src\main\java">
<property name="enableSubPackage" value="false" />
</javaClientGenerator>
<!--指定数据库表-->
<table schema="test" tableName="users">
</table>
</context>
</generatorConfiguration>
-
添加完配置文件可能dtd约束那部分会变红
解决方案 左边有红色小灯泡,点击Fetch external resource即可解决! -
运行行 generator 插件生成代码
点击generator即可生成代码