1. 引入插件
將如下依賴添加到pom.xml
文件中即可。
注意此處是以
plugin
的方式
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
2. 編寫generatorConfig.xml
將該文件放置在
resources
的根目錄下面
<?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>
<!-- 1. 數據庫驅動:選擇你的本地硬盤上面的數據庫驅動包-->
<classPathEntry
location="C:\Users\lenovo\.m2\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 2. 數據庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/datadb?characterEncoding=utf8" userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 3. 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="D:/project/demo/generator/src/">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 4. 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.example.demo.mapper" targetProject="D:/project/demo/generator/src/">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 5. 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="D:/project/demo/generator/src/">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 6. 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
<table tableName="alexa" domainObjectName="Alexa" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="develop" domainObjectName="Develop" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="ios" domainObjectName="Ios" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="num" domainObjectName="Num" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="total" domainObjectName="Total" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<table tableName="users" domainObjectName="User" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
- 注意根據自己本地環境,修改對應的配置信息
targetProject
必須真實存在- 要生成的表配置取決於自己數據庫中的表
3. 運行maven
4. 執行效果
將生成的代碼移到項目中的路徑,即可將generator
目錄刪除