Mybatis代码生成器
导入依赖
通过编码方式去运行插件先需要引入mybatis-generator-core
依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis.generator.core</artifactId>
<version>1.3.2</version>
</dependency>
如果使用Maven
插件,那么不需要引入mybatis-generator-core
依赖,只需要引入一个Maven
的插件mybatis-generator-maven-plugin
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- 输出详细信息 -->
<verbose>true</verbose>
<!-- 覆盖生成文件 -->
<overwrite>true</overwrite>
<!-- 定义配置文件 -->
<configurationFile>
src/main/resources/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
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="db.properties"/>
<!-- 数据库驱动,解析数据库表来生成实体类 -->
<classPathEntry location="D:/mysql-connector-java-5.1.5.jar" />
<!-- 一个数据库一个context -->
<context id="MyTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- suppressDate:**阻止**生成的注释包含时间戳 -->
<property name="suppressDate" value="true"/>
<!-- suppressAllComments:true **阻止**生成注释 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接地址账号密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/health"
userId="root"
password="">
</jdbcConnection>
<javaTypeResolver>
<!-- 控制是否强制DECIMAL和NUMERIC类型的字段转为Java类型的java.math.BigDecimal -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成Model类存放位置 -->
<javaModelGenerator targetPackage="com.itheima.pojo" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Dao类存放位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itheima.dao" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 生成对应表及类名 -->
<table schema="health" tableName="t_checkitem" domainObjectName="CheckItem"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"
/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
</context>
</generatorConfiguration>
执行插件