spring boot整合mybatis+mysql自動生成實體類和sql語句

首先,在pom.xml中配置generator 插件所對應的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xmljava

spring boot配置generator

接下來是所須要的generatorConfig.xml,項目中位置以下:mysql

generatorConfig.xml文件內容以下:spring

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
 3 
 4 <!-- 根據數據庫表生成實體類及mapper -->
 5 <generatorConfiguration>
 6     <!-- <properties resource="db.properties" /> -->
 7     <classPathEntry
 8             location="C:\maven\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" />
 9     <context id="mysql2Beans" targetRuntime="MyBatis3">
10         <commentGenerator>
11             <!-- 刪除代碼中帶有 代碼生成器的註釋信息 -->
12             <property name="suppressAllComments" value="true" />
13             <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
14             <property name="suppressDate" value="true" />
15         </commentGenerator>
16 
17         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
18                         connectionURL="jdbc:mysql://localhost:3306/數據庫名?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=TRUE"
19                         userId="root" password="admin" />
20 
21         <javaTypeResolver>
22             <property name="forceBigDecimals" value="false" />
23         </javaTypeResolver>
24 
25         <!-- 生成模型的包名和位置 -->
26         <javaModelGenerator targetPackage="com.mycenter.model"
27                             targetProject="src\main\java">
28             <property name="enableSubPackages" value="true" />
29             <property name="trimStrings" value="false" />
30             <!-- <property name="trimStrings" value="true" /> -->
31         </javaModelGenerator>
32 
33         <sqlMapGenerator targetPackage="mapper"
34                          targetProject="src\main\resources">
35             <property name="enableSubPackages" value="true" />
36         </sqlMapGenerator>
37 
38         <javaClientGenerator targetPackage="com.mycenter.mapper"
39                              targetProject="src\main\java" type="XMLMAPPER">
40             <property name="enableSubPackages" value="true" />
41         </javaClientGenerator>
42 
43 
44         <table schema="test" tableName="表名"
45                domainObjectName="實體類名" enableCountByExample="false"
46                enableUpdateByExample="false" enableDeleteByExample="false"
47                enableSelectByExample="false" selectByExampleQueryId="false">
48         </table>
49     </context>
50 </generatorConfiguration>

點擊IDEA上的run ==》Edit Configurations:sql

而後配置generator:數據庫

而後運行此插件:mybatis

 

注意同一張表必定不要運行屢次,由於mapper的映射文件中會生成屢次的代碼,會致使致使報錯,運行以後項目目錄結構爲:app

至此 實體類,實體類接口,sql文件都已生成。dom

相關文章
相關標籤/搜索