標籤:str mave nts end blank ble cut csdn 自動
步驟:
1.在pom.xml中添加外掛程式配置
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId> mysql</groupId> <artifactId> mysql-connector-java</artifactId> <version> 5.1.39</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允許移動產生的檔案 --> <verbose>true</verbose> <!-- 是否覆蓋 --> <overwrite>true</overwrite> <!-- 自動產生的配置 --> <configurationFile> src/main/resources/mybatis-generator.xml</configurationFile> </configuration> </plugin>
2.在resources檔案夾中添加mybatis-generator.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> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--資料庫連結地址帳號密碼--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/longhubang" userId="root" password="hongda$123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--產生Model類存放位置--> <javaModelGenerator targetPackage="com.example.scheduleService.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--產生對應檔存放位置--> <sqlMapGenerator targetPackage="com.example.scheduleService.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--產生Dao類存放位置--> <!-- 用戶端代碼,產生便於使用的針對Model對象和XML設定檔 的代碼 type="ANNOTATEDMAPPER",產生Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",產生基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",產生SQLMap XML檔案和獨立的Mapper介面 --> <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.example.scheduleService.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--產生對應表及類名--> <!--<table tableName="stocktradeinfo" domainObjectName="StockTradeInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>--> <table tableName="theme" domainObjectName="Theme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="stockTheme" domainObjectName="StockTheme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context></generatorConfiguration>
3.根據配置建立對應的model,mapper,dao檔案夾
4.使用maven中的mybatis-generator:generate根據資料庫裡面表生產相關的類,Mapper
注意:
這裡面自動生產的Mapper
1.沒有@Mapper註解
2.在insert的時候會帶上主鍵ID
http://blog.csdn.net/yezhuanxu/article/details/53483130
SpringBoot中使用mybatis-generator自動生產