最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis代碼自動生成器Mybatis-Generator的使用詳解

 更新時間:2024年10月17日 09:01:44   作者:private_static  
本文詳細介紹如何在SpringBoot項目中使用MyBatis-Generator進行代碼生成,包括配置文件的添加、POM依賴配置、運行配置等步驟,通過自動生成代碼,可以簡化MyBatis的繁瑣配置和SQL編寫,提高開發(fā)效率,注意要考慮MySQL版本兼容性,以及確保路徑配置正確

MyBatis代碼生成器Mybatis-Generator的配置和使用

  • 注:項目介紹
  • 編譯器:Intellij IDEA
  • 項 目:SpringBoot項目

講道理現(xiàn)在MyBatis-plus出來了。省去了再寫許多繁瑣的xml文件。也大大簡化了開發(fā)壓力。類似于SpringData-JPA(也挺好用的)。MyBatis-plus也有相關的代碼生成器。后面有時間博主再去踩一下回來再整理。

首先我們有一個建好的現(xiàn)成項目

可以看到什么都還沒有加進去,那我們就從連接數(shù)據(jù)庫到代碼自動生成演示一下。

使用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:表示保護 -->
			<!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發(fā)生變化,不利于版本控制,所以設置為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是對應的前綴目錄??筛鶕?jù)自己需求生到對應目錄。下次運行會直接默認覆蓋原來位置的文件 -->
		<!-- 生成模型的包名和位置   映射實體類的位置 -->
		<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。

不然會找不到y(tǒng)ml?;蛘咴谥暗哪莻€配置文件把yml改為properties,都可以。)

懶人專用:請copy

mybatis-generator:generate -e

4、選中剛才配置的直接運行就ok了。是不是很簡單。

由于Mybatis要寫很多很多的xml文件、和Sql語句。這個代碼生成器會直接生成諸多常用的增刪查改。

看到以下提示、說明你很幸運,直接成功了。你可真是太優(yōu)秀了!博主開始用的時候可是遇到了太多的坑了。

我們再到剛才配置生成的路徑下看看文件。已經(jīng)幫我們直接生成了我們想要的基礎文件。

隨便例舉一個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測試項目的時候記錄的??赡懿皇呛芤?guī)范,望見諒。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java 二分法檢索算法代碼實現(xiàn)詳解

    Java 二分法檢索算法代碼實現(xiàn)詳解

    這篇文章主要介紹了Java 二分法檢索算法代碼實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • MyBatis-Plus 全面介紹與Spring Boot 集成最佳實踐

    MyBatis-Plus 全面介紹與Spring Boot 集成最佳實踐

    MyBatis-Plus是MyBatis的增強工具,提供了通用CRUD、分頁、條件構造器等功能,簡化了開發(fā),減少重復SQL編寫,它支持多種主鍵生成策略,內(nèi)置樂觀鎖和邏輯刪除,且兼容MyBatis原有代碼,本文介紹MyBatis-Plus全面介紹與Spring Boot 集成最佳實踐,感興趣的朋友一起看看吧
    2025-12-12
  • java按照模板導出pdf或word文件詳細代碼

    java按照模板導出pdf或word文件詳細代碼

    有時候業(yè)務中我們需要使用pdf模板生成一份pdf文件,下面這篇文章主要給大家介紹了關于java按照模板導出pdf或word文件的相關資料,文中給出了詳細的代碼示例,需要的朋友可以參考下
    2023-11-11
  • java如何實現(xiàn)數(shù)位分離

    java如何實現(xiàn)數(shù)位分離

    這篇文章主要介紹了java如何實現(xiàn)數(shù)位分離,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • springboot多開端口操作方式

    springboot多開端口操作方式

    本文介紹了在IDEA中修改端口名和啟動類路徑的方法,提供了兩種方式添加端口配置,并且建議只選擇一種方式進行修改,最后點擊Apply和OK保存更改
    2026-05-05
  • 手把手帶你理解java線程池之工作隊列workQueue

    手把手帶你理解java線程池之工作隊列workQueue

    這篇文章主要介紹了java線程池之工作隊列workQueue,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • java 通過 SmbFile 類操作共享文件夾的示例

    java 通過 SmbFile 類操作共享文件夾的示例

    這篇文章主要介紹了java 通過 SmbFile 類操作共享文件夾,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Java十大經(jīng)典排序算法圖解

    Java十大經(jīng)典排序算法圖解

    這篇文章主要介紹了Java十大經(jīng)典排序算法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • 最新評論

    禹城市| 黎平县| 大埔县| 新田县| 六枝特区| 西峡县| 孝义市| 宣汉县| 卓尼县| 黄浦区| 天气| 文化| 上犹县| 扬中市| 乐亭县| 榆林市| 乐昌市| 永泰县| 香河县| 安多县| 额敏县| 潼南县| 南康市| 台南县| 儋州市| 苍梧县| 定安县| 铁岭市| 北川| 沽源县| 汾阳市| 游戏| 故城县| 泸定县| 伊川县| 玉山县| 张家口市| 双峰县| 隆化县| 米泉市| 罗田县|