pom.xml(需要将mysql的依赖在generator里再写一次)
添加相关依赖(在dependencies标签里 不是在plugin标签里)
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--generator配置文件所在位置-->
<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.15</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
同目录下新建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>
<properties resource="application.properties"/>
<!--<classPathEntry location="${jdbc.location}"/>-->
<context id="Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 注释 -->
<commentGenerator>
<!-- 是否生成注释代时间戳 -->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- JDBC连接 -->
<jdbcConnection
driverClass="${spring.datasource.driverClassName}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.example.demo" targetProject="src/main/java">
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaModelGenerator>
<!-- 生成mapper xml文件 -->
<!--<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">-->
<!--enableSubPackages:是否让schema作为包的后缀-->
<!--<property name="enableSubPackages" value="false"/>-->
<!--</sqlMapGenerator>-->
<!-- 生成mapper xml对应Client-->
<!--<javaClientGenerator targetPackage="com.nsw.admin.mapper" targetProject="src/main/java"-->
<!--type="XMLMAPPER">-->
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<!--<property name="enableSubPackages" value="false"/>-->
<!--</javaClientGenerator>-->
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table tableName="users"
domainObjectName="User">
<generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
</table>
<table tableName="teacher">
<!--<property name="modelOnly" value="true"></property>-->
<generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
</table>
<table tableName="class">
<generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
</table>
<!--<table schema="blog" tableName="article"-->
<!--domainObjectName="Article" enableCountByExample="false"-->
<!--enableDeleteByExample="false" enableSelectByExample="false"-->
<!--enableUpdateByExample="false">-->
<!--</table>-->
<!--<table schema="blog" tableName="document"-->
<!--domainObjectName="Document" enableCountByExample="false"-->
<!--enableDeleteByExample="false" enableSelectByExample="false"-->
<!--enableUpdateByExample="false">-->
<!--</table>-->
<!--<table schema="blog" tableName="tag"-->
<!--domainObjectName="Tag" enableCountByExample="false"-->
<!--enableDeleteByExample="false" enableSelectByExample="false"-->
<!--enableUpdateByExample="false">-->
<!--</table>-->
</context>
</generatorConfiguration>
application.properties:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis
spring.datasource.username=root
spring.datasource.password=123456
运行:
EditConfigurations:
mybatis-generator:generate -e
千万别敲错了
然后运行