有天上飛的概念,就要有落地的實現html
概念十遍不如代碼一遍,朋友,但願你把文中全部的代碼案例都敲一遍java
先贊後看,養成習慣mysql
SpringBoot 圖文教程系列文章目錄git
在使用Mybatis進行項目開發的時候,最繁瑣的事情就是實體類,dao接口,mapper.xml文件的編寫,幾乎每一個表都須要對應寫一套,而且大部分的工做量都在最基本的增刪改查上。若是表中的字段進行了修改,那麼實體類,mapper文件甚至dao接口都要進行修改。面試
天下苦mapper文件久矣,因而Mybatis官方推薦了一個Mybatis代碼生成器(MBG)來救民於水火之中。sql
MBG 全稱 MyBatis Generator,能夠用來生成Mybatis開發相關的代碼,包括基本增刪改查的實體類,dao接口和mapper文件。而且 MBG 工具支持全部版本的Mybatis。數據庫
官方文檔地址:http://mybatis.org/generator/mybatis
本文代碼會在一個SpringBoot+Mybatis的空項目中進行,若有須要請去Git倉庫下載:https://gitee.com/bingqilinpeishenme/Java-Tutorialsapp
SpringBoot中使用MBG須要在導入MBG依賴的同時導入MBG的啓動插件。 MBG的依賴dom
<!--MybatisGenerator的依賴jar包--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
MBG的啓動插件
<!--MybatisGenerator的啓動插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <!--jar包去生成對應類 須要鏈接數據庫 數據鏈接的版本和項目中的一致--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <configuration> <!--MBG配置文件的路徑 --> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> </plugin>
注意:
- 插件中的 mysql-connector-java 版本和項目中版本號一致
- configurationFile 配置的是 MBG配置文件的地址 src/main/resources
導入依賴以後,須要在SpringBoot resources目錄下導入 MBG 的配置文件 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> <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="true" /> </commentGenerator> <!--數據庫連接URL,用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/demo" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成實體類的包名和位置 --> <javaModelGenerator targetPackage="com.lu.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成映射文件的包名和位置 com/lu/mapper--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 mybatis兩種開發模式 xml 註解式--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lu.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
重點配置 !!!
在配置文件中有一下配置須要使用者根據本身的狀況進行配置
數據庫鏈接參數 修改成本身數據庫的鏈接參數
dao接口 實體類 mapper文件生成位置 包結構的配置
注意:生成位置和包結構參數根據本身實際狀況進行修改,其餘的內容不須要修改
注意:除了以上三個配置,其餘配置均可以沒必要修改
運行插件,生成代碼以下:
實體類
dao接口
mapper文件
Tips:本文示例代碼項目地址爲:https://gitee.com/bingqilinpeishenme/Java-Tutorials
恭喜你完成了本章的學習,爲你鼓掌!若是本文對你有幫助,請幫忙點贊,評論,轉發,這對做者很重要,謝謝。
讓咱們再次回顧本文的學習目標
- 掌握SpringBoot中MBG的使用
要掌握SpringBoot更多的用法,請持續關注本系列教程。
歡迎關注本人公衆號:鹿老師的Java筆記,將在長期更新Java技術圖文教程和視頻教程,Java學習經驗,Java面試經驗以及Java實戰開發經驗。
歡迎關注本人公衆號:鹿老師的Java筆記,將在長期更新Java技術圖文教程和視頻教程,Java學習經驗,Java面試經驗以及Java實戰開發經驗。
原文出處:https://www.cnblogs.com/bingyang-py/p/12391544.html