mybatis-generator自動生成器外掛使用詳解
阿新 • • 發佈:2019-01-03
1、構建maven專案,我的專案是在idea上面建立。
2、資料庫表提供:
CREATE TABLE `users` (
CREATE TABLE `emp` (
`id` INT(10) NOT NULL,
`name` VARCHAR(20) DEFAULT NULL,
`age` INT(3) DEFAULT NULL,
`depNo` INT(10) DEFAULT NULL,
`salary` DOUBLE DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC
3、generator.properties
jdbc.driverLocation=F:\\JavaNote.ZCH\\tools.java\\mysql-connector-java-5.1.8-bin.jar
# 資料庫連線引數
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password =root
# 包路徑配置
model.package=com.xsx.test.model
dao.package=com.xsx.test.dao
xml.mapper.package=com.xsx.test.dao
target.project=src/main/java
4、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="generator.properties"></properties>
<!--資料庫驅動包路徑 -->
<classPathEntry location="${jdbc.driverLocation}"/>
<context id="MySQLTables" targetRuntime="MyBatis3">
<!--關閉註釋 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--資料庫連線資訊 -->
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!--生成的model 包路徑 -->
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成xml mapper檔案 路徑 -->
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
<!-- 生成的Dao介面 的包路徑 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
<!--對應資料庫表名 -->
<table tableName="emp" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
<!--<commentGenerator>-->
<!--<property name="suppressDate" value="false"/>-->
<!--</commentGenerator>-->
</generatorConfiguration>
5、mybatis-generator:generate -e
6、結果