MyBatis代碼自動生成器Mybatis-Generator的使用詳解
MyBatis代碼生成器Mybatis-Generator的配置和使用
- 注:項目介紹
- 編譯器:Intellij IDEA
- 項 目:SpringBoot項目
講道理現(xiàn)在MyBatis-plus出來了。省去了再寫許多繁瑣的xml文件。也大大簡化了開發(fā)壓力。類似于SpringData-JPA(也挺好用的)。MyBatis-plus也有相關(guān)的代碼生成器。后面有時間博主再去踩一下回來再整理。
首先我們有一個建好的現(xiàn)成項目
可以看到什么都還沒有加進去,那我們就從連接數(shù)據(jù)庫到代碼自動生成演示一下。
使用Mybatis-Generator
1、首先我們要添加一個配置文件,這個也是最關(guān)鍵的文件。
配置文件代碼:mybatis-generator-cfg.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.yml"></properties> <classPathEntry location="F:\apache-maven-3.5.4\LocalHouse\mysql\mysql-connector-java\5.1.46\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:表示保護 --> <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發(fā)生變化,不利于版本控制,所以設(shè)置為true --> <property name="suppressDate" value="true" /> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--數(shù)據(jù)庫鏈接URL,用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/demo" userId="root" password="123456"> </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> <!-- targetpakage是即將生成的目錄,targetProject是對應(yīng)的前綴目錄??筛鶕?jù)自己需求生到對應(yīng)目錄。下次運行會直接默認覆蓋原來位置的文件 --> <!-- 生成模型的包名和位置 映射實體類的位置 --> <javaModelGenerator targetPackage="com.sbproject.sb.common.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 mapper.xml --> <sqlMapGenerator targetPackage="com.sbproject.sb.common.dao.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 mapper接口--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sbproject.sb.common.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 orders是我的表名,Orders是生成的類名,比如我的映射類為Order,映射接口OrderMapper, 映射文件為OrderMapper.xml,可以添加多個表,里面的幾個配置大概意思就是是否允許生成example文件和支持selectByExample。用過Mybatis的應(yīng)該知道selectByExample,對于一些簡單查詢用這個還是比較方便的。哈哈、話有點多,記得刪除 --> <table tableName="orders" domainObjectName="Orders" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table> <!-- 要生成哪些表 --> <table tableName="products" domainObjectName="Products" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table> </context> </generatorConfiguration>
2、其次就是pom里面添加配置,加載plugins里面!
注:紅圈的路徑對應(yīng)的就是剛才添加的配置文件。
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <!-- <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> --> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>
3、添加運行配置、運行文件(對了記得吧application.properties后綴改為yml。
不然會找不到y(tǒng)ml?;蛘咴谥暗哪莻€配置文件把yml改為properties,都可以。)
懶人專用:請copy
mybatis-generator:generate -e
4、選中剛才配置的直接運行就ok了。是不是很簡單。
由于Mybatis要寫很多很多的xml文件、和Sql語句。這個代碼生成器會直接生成諸多常用的增刪查改。
看到以下提示、說明你很幸運,直接成功了。你可真是太優(yōu)秀了!博主開始用的時候可是遇到了太多的坑了。
我們再到剛才配置生成的路徑下看看文件。已經(jīng)幫我們直接生成了我們想要的基礎(chǔ)文件。
隨便例舉一個Mapper接口吧, 為了讓大家更直觀的看到、我把后面自動生成的注釋刪了。
注釋的作用主要用于表格變動之類需要再次生成時識別是否為自動生成的代碼(比如第一個countByExample)。會默認覆蓋自動生成的代碼。
沒有注釋的會保留下來。:
public interface OrdersMapper { /** * This method was generated by MyBatis Generator. * This method corresponds to the database table orders * * @mbg.generated */ long countByExample(OrdersExample example); int deleteByExample(OrdersExample example); int deleteByPrimaryKey(String id); int insert(Orders record); int insertSelective(Orders record); List<Orders> selectByExample(OrdersExample example); Orders selectByPrimaryKey(String id); int updateByExampleSelective(@Param("record") Orders record, @Param("example") OrdersExample example); int updateByExample(@Param("record") Orders record, @Param("example") OrdersExample example); int updateByPrimaryKeySelective(Orders record); int updateByPrimaryKey(Orders record); }
重點
以下提幾點需要注意的問題。
1、注意mysql的版本問題,不能超過5 。
博主遇到過問題超過五就報錯。太久了忘了記錄了。跟我一樣選一樣默認依賴。
新建SpringBoot項目后MySql-Connector默認是8.幾。
所以加一個版本號就行。
2、配置文件里面的連接依賴和項目配置的依賴路徑一致。
不然也會報錯。
可直接右鍵jar包->find in path -> copy路徑到右邊配置文件對應(yīng)位置即可。
3、還是開始提的pom里面的generator.xml路徑一定要配對。
最后就附上我的pom.xml文件吧
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springbootMybatis</groupId> <artifactId>sbm</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sbm</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>2.1.5.RELEASE</version> </dependency> </dependencies> <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.5</version> <configuration> <configurationFile>src/main/resources/mybatis-generator/mybatis-generator-cfg.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
注:此博客基礎(chǔ)博客。博主比較菜。常用的是SSM,這個是博主搭建SpringBoot測試項目的時候記錄的??赡懿皇呛芤?guī)范,望見諒。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09JavaWeb?Servlet實現(xiàn)文件上傳與下載功能實例
因自己負責的項目中需要實現(xiàn)文件上傳,所以下面下面這篇文章主要給大家介紹了關(guān)于JavaWeb?Servlet實現(xiàn)文件上傳與下載功能的相關(guān)資料,需要的朋友可以參考下2022-04-04Java調(diào)用第三方http接口的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java調(diào)用第三方http接口的四種方式,在實際開發(fā)中我們經(jīng)常會與第三方公司進行合作,接入第三方接口,文中給出了詳細的代碼實例,需要的朋友可以參考下2023-08-08