- MyBatis代碼生成器Mybatis-Generator的配置和使用。
- 注:項目介紹
- 編譯器:Intellij IDEA
- 項 目:SpringBoot項目
- 講道理現在MyBatis-plus出來了。省去了再寫許多繁瑣的xml文件。也大大簡化了開發壓力。類似於SpringData-JPA(也挺好用的)。MyBatis-plus也有相關的代碼生成器。後面有時間博主再去踩一下回來再整理。
1、首先我們有一個建好的現成項目。
-
可以看到什麼都還沒有加進去,那我們就從連接數據庫到代碼自動生成演示一下。使用Mybatis-Generator
-
1、首先我們要添加一個配置文件,這個也是最關鍵的文件。
-
配置文件代碼: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:表示保護 -->
<!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利於版本控制,所以設置爲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/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是對應的前綴目錄。可根據自己需求生到對應目錄。下次運行會直接默認覆蓋原來位置的文件 -->
<!-- 生成模型的包名和位置 映射實體類的位置 -->
<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的應該知道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裏面!注:紅圈的路徑對應的就是剛纔添加的配置文件。
<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。不然會找不到yml。或者在之前的那個配置文件把yml改爲properties,都可以。)
-
懶人專用:請copy
mybatis-generator:generate -e
-
4、選中剛纔配置的直接運行就ok了。是不是很簡單。由於Mybatis要寫很多很多的xml文件、和Sql語句。這個代碼生成器會直接生成諸多常用的增刪查改。
-
看到以下提示、說明你很幸運,直接成功了。你可真是太優秀了!博主開始用的時候可是遇到了太多的坑了。
-
我們再到剛纔配置生成的路徑下看看文件。已經幫我們直接生成了我們想要的基礎文件。
-
隨便例舉一個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路徑到右邊配置文件對應位置即可。
-
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>
- 注:此博客基礎博客。博主比較菜。常用的是SSM,這個是博主搭建SpringBoot測試項目的時候記錄的。可能不是很規範,望見諒。如有錯漏還望指正。謝謝謝哈!