Idea搭建mybatis-generator
阿新 • • 發佈:2018-12-30
1、springboot 2.0 + mybatis+web + mysql +jdbc 搭建專案
2、pom.xml中引入mybatis-generator-maven-plugin,只需配置第二個plugin。之後會在Maven Projects的Plugins下自動生成一個mybatis-generator外掛,配置generatorConfig.xml後雙擊該外掛即可自動生成相關程式碼。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
3、在resources資料夾下建立“generatorConfig.xml”
注意:
(1)提前建好contoller、model、dao、service以及resources的子目錄mapper;
(2)classPathEntry配置的是本地mysql驅動包路徑;
(3)mapper的xml對映檔案自動生成時我移到了resources/mapper目錄下;
(4) model、dao和mapper程式碼的生成路徑別配錯了;
(5)table可以配置一或多個,主要是配置tablename和domainObjectName;
<?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> <classPathEntry location="D:/Maven/mysql-connector-java-5.1.46.jar" /> <context id="test" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 這個元素用來去除指定生成的註釋中是否包含生成的日期 false:表示保護 --> <!-- 如果生成日期,會造成即使修改一個欄位,整個實體類所有屬性都會發生變化,不利於版本控制,所以設定為true --> <property name="suppressDate" value="true" /> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--資料庫連結URL,使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 資料夾自己定義--> <javaModelGenerator targetPackage="com.mm.spboot.model" 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.mm.spboot.dao" implementationPackage="com.mm.spboot.dao.impl" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="t_user" domainObjectName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
4、雙擊mabitis-generator外掛,程式碼會生成到相應的位置。