亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot整合mybatis-generator插件流程詳細講解

 更新時間:2023年02月03日 10:19:38   作者:TryMyBestTo  
這篇文章主要介紹了SpringBoot整合mybatis-generator插件流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

mybatis-generator 插件

mybatis 相關依賴

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>
<!-- 數據庫連接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.15</version>
</dependency>

mybatis-generator 插件,自動生成mybatis所需要的 dao、bean、mapper.xml文件。

<build>
    <plugins>
        <!-- mybatis 插件 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
            </configuration>
        </plugin>
    </plugins>
</build>

創(chuàng)建generatorConfig.xml 文件,是這個插件的配置文件

需要第八行<classPathEntry location=“項目mysql-connection-java-版本.jar 包的絕對路徑”

<?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"/>
  <!-- mysql驅動的位置 這個是MySQL連接的jar包,你需要指定你自己計算機上的jar包的位置-->
  <classPathEntry location="E:\Java\IDEA\code\mall\src\main\resources\lib\mysql-connector-java-8.0.25.jar" />
  <context id="MysqlTables" targetRuntime="MyBatis3">
    <property name="autoDelimitKeywords" value="true"/>
    <!--可以使用``包括字段名,避免字段名與sql保留字沖突報錯-->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!-- 是否生成注釋 -->
    <commentGenerator>
      <!-- 是否生成注釋代時間戳 -->
      <property name="suppressDate" value="true"/>
      <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--數據庫鏈接地址賬號密碼-->
    <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
      connectionURL="${spring.datasource.url}"
      userId="${spring.datasource.username}"
      password="${spring.datasource.password}">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制-->
    <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL 和
         NUMERIC 類型解析為java.math.BigDecimal -->
    <javaTypeResolver>
      <!-- 是否使用bigDecimal, false可自動轉化以下類型(Long, Integer, Short, etc.) -->
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成實體類地址 這里需要你改動,其中 targetPackage 需要根據你自己創(chuàng)建的目錄進行改動 -->
    <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}"
      targetProject="src/main/java">
      <!-- 是否允許子包,即targetPackage.schemaName.tableName -->
      <property name="enableSubPackages" value="true"/>
      <!-- 是否對類CHAR類型的列的數據進行trim操作 -->
      <property name="trimStrings" value="true"/>
      <!-- 建立的Model對象是否 不可改變  即生成的Model對象不會有 setter方法,只有構造方法 -->
      <property name="immutable" value="true"/>
    </javaModelGenerator>
    <!--生成mapper映射文件存放位置-->
    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!--生成Dao類存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="${generator.mappers}"
      targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--生成對應表及類名-->
    <!-- schema即為數據庫名 tableName為對應的數據庫表 domainObjectName是要生成的實體類 enable*ByExample 是否生成 example類 -->
    <table schema="root" tableName="imooc_mall_cart" domainObjectName="Cart"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_category" domainObjectName="Category" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order" domainObjectName="Order" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order_item" domainObjectName="OrderItem"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_product" domainObjectName="Product" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_user" domainObjectName="User" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
  </context>
</generatorConfiguration>

然后修改 application.properties 文件,增加一點配置項。

當然,也可以選擇直接在 generatorConfig.xml 文件中書寫,但想要運行整個項目,也是必須配置mysql的。

#mysql配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
#mybatis
#指定mapper.xml文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#設置pojo類型別名
mybatis.type-aliases-package=com.daxiong.mall.pojo
#Generator配置
#生成pojo位置
generator.javaModel-targetPackage=com.daxiong.mall.pojo
#生成接口位置
generator.mappers=com.daxiong.mall.mapper

啟動插件生成文件:打開maven工具欄-》選擇當前項目名-》Plugins-》mybatis-generator -》雙擊運行 mybatis-generator:generate 。

此時,便會**自動生成**mybatis所需要的 dao、bean、mapper.xml文件

自動生成的mapper接口示例:

// 根據 主索引字段 刪除
int deleteByPrimaryKey(Integer id);
// 插入
int insert(Order record);
// 可選擇性插入(值為null的字段不插入,使用默認值)
int insertSelective(Order record);
// 根據 主索引字段 查詢
Order selectByPrimaryKey(Integer id);
// 可選擇性更新(值為null的字段不插入,使用默認值)
int updateByPrimaryKeySelective(Order record);
// 更新
int updateByPrimaryKey(Order record);

通知MyBatis,mapper接口存放的位置:

方法一:為 主啟動類添加注解:

@MapperScan(basePackages = “com.daxiong.mall.mapper”):告訴 ,mapper 接口放在了哪里。

方法二:為為每個 mapper 接口添加注解:

@Mapper:讓此接口在Spring Ioc 初始化時生成 bean,以便其他類注入使用。

到此這篇關于SpringBoot整合mybatis-generator插件流程詳細講解的文章就介紹到這了,更多相關SpringBoot整合mybatis-generator內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Mybatis-Plus實現用戶ID自增出現的問題解決

    Mybatis-Plus實現用戶ID自增出現的問題解決

    項目基于 SpringBoot + MybatisPlus 3.5.2 使用數據庫自增ID時, 出現重復鍵的問題,本文就來介紹一下解決方法,感興趣的可以了解一下
    2023-09-09
  • hibernate多表操作實例代碼

    hibernate多表操作實例代碼

    這篇文章主要介紹了hibernate多表操作實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 使用IDEA配置Maven搭建開發(fā)框架ssm教程

    使用IDEA配置Maven搭建開發(fā)框架ssm教程

    這篇文章主要為大家詳細介紹了使用IDEA配置Maven搭建開發(fā)框架ssm教程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java中正則表達式split()特殊符號使用詳解

    Java中正則表達式split()特殊符號使用詳解

    這篇文章主要介紹了Java中正則表達式split()特殊符號使用詳解, 文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • java抓取網頁或文件中的郵箱號碼

    java抓取網頁或文件中的郵箱號碼

    這篇文章主要為大家詳細介紹了java如何抓取網頁或文件中的郵箱號碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Java結構型設計模式之享元模式示例詳解

    Java結構型設計模式之享元模式示例詳解

    享元模式(FlyWeight?Pattern),也叫蠅量模式,運用共享技術,有效的支持大量細粒度的對象,享元模式就是池技術的重要實現方式。本文將通過示例詳細講解享元模式,感興趣的可以了解一下
    2022-09-09
  • Java如何優(yōu)雅關閉異步中的ExecutorService

    Java如何優(yōu)雅關閉異步中的ExecutorService

    在并發(fā)編程領域,Java的ExecutorService是線程池管理的關鍵接口,這篇文章主要為大家介紹了如何優(yōu)雅關閉異步中的ExecutorService,需要的可以了解下
    2025-02-02
  • Spring外部化配置的幾種技巧分享

    Spring外部化配置的幾種技巧分享

    在油管上看了龍之春的一個Spring tips 視頻,講述Spring外部化配置的幾種技巧,收獲頗多,想拿出來給大家分享下。對spring感興趣的朋友可以了解下本文
    2021-06-06
  • springMVC4之強大類型轉換器實例解析

    springMVC4之強大類型轉換器實例解析

    本篇文章主要介紹了springMVC4之強大類型轉換器實例解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Spring和SpringBoot有哪些區(qū)別

    Spring和SpringBoot有哪些區(qū)別

    相信對于用了 SpringBoot很久的同學來說,還不是很理解 SpringBoot到底和 Spring有什么區(qū)別,看完文章中的比較,或許你有了不同的答案和看法。
    2020-10-10

最新評論