MyBatis Generator 是MyBatis 官方出品的一款,用來自動生成MyBatis的 mapper、dao、entity 的框架,讓咱們省去規律性最強的一部分最基礎的代碼編寫。java
MyBatis Generator的使用方式有4種:mysql
其中推薦使用Maven方式進行代碼生成,由於集成和使用比較簡單。git
<!--more-->github
MySQL:8.0.12spring
MyBatis Generator:1.3.7sql
Maven:4.0數據庫
IDEA:2018.2springboot
上面介紹了使用MyBatis Generator的幾種方式,其中最推薦使用的是Maven方式,因此下面咱們來看Maven方式的MyBatis代碼生成,分爲四步:mybatis
配置pom.xml文件,增長依賴和配置生成文件(「generatorConfig.xml」)路徑:app
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</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>generatorConfig.xml</configurationFile> </configuration> </plugin>
根據上面在pom裏的配置,咱們須要添加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="application.properties"/> <!--defaultModelType="flat" 大數據字段,不分表 --> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="autoDelimitKeywords" value="true" /> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <property name="javaFileEncoding" value="utf-8" /> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 註釋 --> <commentGenerator > <property name="suppressAllComments" value="true"/><!-- 是否取消註釋 --> <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!--數據庫連接地址帳號密碼--> <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <!-- 類型轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化如下類型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model類存放位置--> <javaModelGenerator targetPackage="com.hello.springboot.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis" > <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是接口dao --> <javaClientGenerator targetPackage="com.hello.springboot.dao" targetProject="src/main/java" type="XMLMAPPER" > <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> <table tableName="user_log" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
其中數據庫鏈接的配置,是從application.properties直接讀取的。
全局屬性文件application.properties的配置,和Spring Boot增長MyBatis的配置是同樣的,若是你的Spring Boot項目裏面已經配置了MyBatis支持,請忽略此步驟。
# MyBatis 配置 spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.hello.springboot.mapper mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
注意: MySQL 6之後JDBC的配置就不同了,參照如上MySQL 8的配置。
若是你使用的是IDEA,點擊最右側的Maven Projects => 點擊mybatis-generator => 右鍵mybatis-generator:generate => Run Maven Build,以下圖所示:
正常控制檯輸出「BUILD SUCCESS」說明生成已經成功了,若是出現錯誤,根據錯誤提示信息排除處理錯誤便可。
MyBatis Generator 示例源碼:https://github.com/vipstone/springboot-example/tree/master/springboot-mybatis-xml
若是你使用的是 IDEA,那麼強烈建議你安裝一款免費的IDEA插件「Free MyBatis plugin」,能夠實現dao到mapper xml對應方法的快速映射,點擊任意一個快速調整到相應的方法,提升工做效率,效果以下圖所示:
點擊綠色的箭頭直接跳轉到了mapper xml對應的方法了,以下圖所示:
能夠相互點擊,進行對應的跳轉。
安裝步驟
關鍵步驟的截圖以下:
使用了MyBatis Generator能夠幫咱們自動生成實體類,和5個最基礎的方法,大大的提升咱們的工做效率,用戶只須要按需寫本身獨有的一些業務便可。同時增長「Free MyBatis plugin」插件,能夠很方便的幫咱們開發和調試代碼,真是實實在在的福利。