欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis?Generator快速生成實體類和映射文件的方法

 更新時間:2023年10月23日 17:25:49   作者:陳亦康  
這篇文章主要介紹了MyBatis?Generator快速生成實體類和映射文件的方法,通過示例代碼介紹了MyBatis?Generator?的使用,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下

一、MyBatis Generator 的使用

1.1、生成類和映射文件

1.1.1、在 pom.xml 中引入依賴

在 properties 標簽中加入版本號.

<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>

在 build => plugins 標簽中加入如下配置

            <!-- mybatis ?成器插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis-generator-plugin-version}</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- 相關配置 -->
                <configuration>
                    <!-- 打開?志 -->
                    <verbose>true</verbose>
                    <!-- 允許覆蓋 -->
                    <overwrite>true</overwrite>
                    <!-- 配置?件路徑 -->
                    <configurationFile>
                        src/main/resources/mybatis/generatorConfig.xml
                    </configurationFile>
                </configuration>
            </plugin>

上述配置中需要注意的是 “配置文件路徑”,這個路徑就是用來生成 實體類和映射文件 配置規(guī)則的位置.

1.1.2、根據(jù) configurationFile 標簽中配置的路徑 創(chuàng)建 generatorConfig.xml 文件

這個文件就是用來描述生成規(guī)則的.

根據(jù)路徑(src/main/resources/mybatis),在 mybatis 目錄下創(chuàng)建 generatorConfig.xml 文件.

Ps:下述配置文件中需要修改的有 數(shù)據(jù)庫連接、實體類和映射文件的路徑、數(shù)據(jù)庫表名

<?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>
    <!-- 驅動包路徑,location中路徑替換成??本地路徑 -->
    <classPathEntry location="D:\class\source\mysql-connector-java-5.1.49.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 禁??動?成的注釋 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 連接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/javanav_db?
characterEncoding=utf8&amp;useSSL=false"
                        userId="root"
                        password="1111">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- ?數(shù)統(tǒng)?轉為BigDecimal -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 實體類?成位置 -->
        <javaModelGenerator targetPackage="com.example.cyk.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- mapper.xml?成位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- mapper 接口?成位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.cyk.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 配置?成表與實例, 只需要修改表名tableName, 與對應類名domainObjectName 即
       可-->
        <table tableName="j_article" domainObjectName="Article"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <!-- 類的屬性?數(shù)據(jù)庫中的真實字段名做為屬性名, 不指定這個屬性會?動轉換 _ 為
           駝峰命名規(guī)則-->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_article_reply" domainObjectName="ArticleReply"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_board" domainObjectName="Board"
               enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_message" domainObjectName="Message"
               enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
               enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="j_user" domainObjectName="User"
               enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

注意:

驅動包路徑是自己本地倉庫的路徑

但一定注意??! 需要在非中文的目錄下,因此你可以把這個驅動包拷貝出來,放到一個非中文的目錄中即可.

1.1.3、自動生成類 和 映射文件

重新加載Maven項?,在Plugins節(jié)點下出現(xiàn)mybatis-generator,雙擊運?,在對應的目錄下?成相應的類與映射?件:

接著你就可以看到對應的生成了

1.1.4、在 Insert 標簽中添加獲取主鍵值的選項

在生成的 xml 文件中,給每一個 insert 標簽都添加以下屬性:useGeneratedKeys="true" keyProperty="id"

<!-- useGeneratedKeys = true -->
<!-- keyProperty = 主鍵字段--> 
 
<!-- 當插??條數(shù)據(jù)后,可以通過user.getId()獲取到?動?成的Id值,如果?法中需要?即獲取Id值,加?這個配置 --> 
<insert id="insert" parameterType="com.example.cyk.model.User" 
useGeneratedKeys="true" keyProperty="id" >

Ps:這個選項也可以自動生成,但是不理想(有些問題)

1.1.5、掃描配置:添加 @Mapper 注解 / 添加掃描注解

有兩種方式配置掃描 Mapper 接口.

1)給每個 mapper 包下的 mapper 接口都添加 @Mapper 注解.

2)給啟動類上 或者 新建一個配置類(有 @Configuration 注解)加上 @MapperScan("com.example.cyk.mapper") 注解.

1.1.6、配置 mybatis

在 yml 文件中配置

mybatis:
  mapper-locations: classpath:mapper/**/*Mapper.xml

1.1.7、測試

@SpringBootTest
public class TestMapper {
 
    @Autowired
    private UserMapper userMapper;
 
    @Test
    public void select() {
        User user = userMapper.selectByPrimaryKey(1L);
        System.out.println(user);
    }
 
}

到此這篇關于MyBatis Generator如何快速生成實體類和映射文件的文章就介紹到這了,更多相關MyBatis Generator生成 實體類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中絕對值函數(shù)的介紹與其妙用

    Java中絕對值函數(shù)的介紹與其妙用

    這篇文章主要給大家介紹了Java中絕對值函數(shù)的介紹與其妙用,其中包括絕對值函數(shù)用來獲取表達式的絕對值和絕對值函數(shù)實現(xiàn)降序+升序輸出。文章末尾給出了實例介紹,有需要的朋友們可以參考學習,下面來一起看看吧。
    2017-01-01
  • mybatis打印的sql日志不寫入到log文件的問題及解決

    mybatis打印的sql日志不寫入到log文件的問題及解決

    這篇文章主要介紹了mybatis打印的sql日志不寫入到log文件的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 在Spring Boot框架中使用AOP的正確姿勢

    在Spring Boot框架中使用AOP的正確姿勢

    aop是spring的兩大功能模塊之一,功能非常強大,為解耦提供了非常優(yōu)秀的解決方案。下面這篇文章主要給大家介紹了如何在Spring Boot框架中使用AOP的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-08-08
  • Java中RocketMQ的流量削峰詳解

    Java中RocketMQ的流量削峰詳解

    這篇文章主要介紹了Java中RocketMQ的流量削峰詳解,MQ的主要特點為解耦、異步、削峰,該文章主要記錄與分享個人在實際項目中的RocketMQ削峰用法,用于減少數(shù)據(jù)庫壓力的業(yè)務場景,需要的朋友可以參考下
    2023-09-09
  • 解決idea check out 切換分支時找不到需要的分支問題

    解決idea check out 切換分支時找不到需要的分支問題

    這篇文章主要介紹了解決idea check out 切換分支時找不到需要的分支問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 項目總結之HttpURLConnection的disconnect的問題

    項目總結之HttpURLConnection的disconnect的問題

    這篇文章主要介紹了項目總結之HttpURLConnection的disconnect的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Springboot處理跨域的實現(xiàn)方式(附Demo)

    Springboot處理跨域的實現(xiàn)方式(附Demo)

    這篇文章主要介紹了Springboot處理跨域的實現(xiàn)方式(附Demo),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Spring?注入集合實現(xiàn)過程示例詳解

    Spring?注入集合實現(xiàn)過程示例詳解

    這篇文章主要為大家介紹了Spring?注入集合實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Springboot 2使用外部Tomcat源碼分析

    Springboot 2使用外部Tomcat源碼分析

    這篇文章主要介紹了Springboot 2使用外部Tomcat源碼分析,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • SpringBoot限制接口訪問頻率避坑

    SpringBoot限制接口訪問頻率避坑

    這篇文章主要為大家介紹了SpringBoot限制接口訪問頻率避坑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05

最新評論