1、引入依赖
<!-- mysql自动生成实体类-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- mybatis-generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- mybatis用于生成代码的配置文件 -->
<configurationFile>src/main/resources/mybatis-generator/mybatis-generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2、编写配置文件
配置文件跟位置跟上面依赖指定的位置要一样
<?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>
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<!-- 这个注释是generator的注释。不是建表时手输的,没什么卵用-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://47.110.49.50:3306/huize?characterEncoding=utf-8"
userId="db_guest" password="w5kMpzopgYgez54u">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 设置Java类生成的位置 -->
<javaModelGenerator targetPackage="cn.trussan.ota.order.model.entity"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 ***mapper.xml-->
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 ***mapper.java-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.trussan.ota.order.model.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 所有要生成的表名 -->
<table tableName="tb_cloud"></table>
<table tableName="tb_i_yun_bao"></table>
<table tableName="tb_tree"></table>
</context>
</generatorConfiguration>
3、运行mybatis-generator插件
这时运行mybatis-generator插件就可以自动生成实体类,dao接口了
生成的实体类,dao接口如下图: