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

FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法

 更新時間:2021年08月04日 15:29:18   作者:tryternity  
本文主要介紹了FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

開始第一個例子: Hello World

 新建Java工程,設置maven依賴

新建maven工程,設置項目編譯級別為Java8及以上,引入fluent mybatis依賴包。

<dependencies>
    <!-- 引入fluent-mybatis 運行依賴包, scope為compile -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- 引入fluent-mybatis-processor, scope設置為provider 編譯需要,運行時不需要 -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis-processor</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

新建演示用的數(shù)據(jù)庫結構

create schema fluent_mybatis_tutorial;

create table hello_world
(
    id           bigint unsigned auto_increment primary key,
    say_hello    varchar(100) null,
    your_name    varchar(100) null,
    gmt_create   datetime   DEFAULT NULL COMMENT '創(chuàng)建時間',
    gmt_modified datetime   DEFAULT NULL COMMENT '更新時間',
    is_deleted   tinyint(2) DEFAULT 0 COMMENT '是否邏輯刪除'
) ENGINE = InnoDB
  CHARACTER SET = utf8 comment '簡單演示表';

創(chuàng)建數(shù)據(jù)庫表對應的Entity類

創(chuàng)建數(shù)據(jù)庫表對應的Entity類: HelloWorldEntity, 你只需要簡單的做3個動作:

  • 根據(jù)駝峰命名規(guī)則命名Entity類和字段
  • HelloWorldEntity繼承IEntity接口類
  • 在HelloWorldEntity類上加注解 @FluentMybatis
@FluentMybatis
public class HelloWorldEntity implements IEntity {
    private Long id;

    private String sayHello;

    private String yourName;

    private Date gmtCreate;

    private Date gmtModified;

    private Boolean isDeleted;
    
    // get, set, toString 方法
}

很簡單吧,在這里,你即不需要配置任何mybatis xml文件, 也不需要寫任何Mapper接口, 但你已經(jīng)擁有了強大的增刪改查的功能,并且是Fluent API,讓我們寫一個測試來見證一下Fluent Mybatis的魔法力量!

運行測試來見證Fluent Mybatis的神奇

為了運行測試, 我們還需要進行JUnit和Spring Test相關配置。

配置spring bean定義

 數(shù)據(jù)源DataSource配置
mybatis的mapper掃描路徑
mybatis的SqlSessionFactoryBean

@ComponentScan(basePackages = "cn.org.atool.fluent.mybatis.demo1")
@MapperScan("cn.org.atool.fluent.mybatis.demo1.entity.mapper")
@Configuration
public class HelloWorldConfig {
    /**
     * 設置dataSource屬性
     *
     * @return
     */
    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fluent_mybatis_tutorial?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    /**
     * 定義mybatis的SqlSessionFactoryBean
     *
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }
}

使用Junit4和Spring-test來執(zhí)行測試

  • 使用spring-test初始化spring容器
  • 注入HelloWorldEntity對應的Mapper類: HelloWorldMapper, 這個類是fluent mybatis編譯時生成的。
  • 使用HelloWorldMapper進行刪除、插入、查詢、修改操作。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfig.class)
public class HelloWorldTest {
    /**
     * fluent mybatis編譯時生成的Mapper類
     */
    @Autowired
    HelloWorldMapper mapper;

    @Test
    public void testHelloWorld() {
        /**
         * 為了演示方便,先刪除數(shù)據(jù)
         */
        mapper.delete(mapper.query()
            .where.id().eq(1L).end());
        /**
         * 插入數(shù)據(jù)
         */
        HelloWorldEntity entity = new HelloWorldEntity();
        entity.setId(1L);
        entity.setSayHello("hello world");
        entity.setYourName("fluent mybatis");
        entity.setIsDeleted(false);
        mapper.insert(entity);
        /**
         * 查詢 id = 1 的數(shù)據(jù)
         */
        HelloWorldEntity result1 = mapper.findOne(mapper.query()
            .where.id().eq(1L).end());
        /**
         * 控制臺直接打印出查詢結果
         */
        System.out.println("1. HelloWorldEntity:" + result1.toString());
        /**
         * 更新id = 1的記錄
         */
        mapper.updateBy(mapper.updater()
            .update.sayHello().is("say hello, say hello!")
            .set.yourName().is("fluent mybatis is powerful!").end()
            .where.id().eq(1L).end()
        );
        /**
         * 查詢 id = 1 的數(shù)據(jù)
         */
        HelloWorldEntity result2 = mapper.findOne(mapper.query()
            .where.sayHello().like("hello")
            .and.isDeleted().eq(false).end()
            .limit(1)
        );
        /**
         * 控制臺直接打印出查詢結果
         */
        System.out.println("2. HelloWorldEntity:" + result2.toString());
    }
}

執(zhí)行Junit4測試方法,控制臺輸出

1. HelloWorldEntity:HelloWorldEntity{id=1, sayHello='hello world', yourName='fluent mybatis', gmtCreate=null, gmtModified=null, isDeleted=false}
2. HelloWorldEntity:HelloWorldEntity{id=1, sayHello='say hello, say hello!', yourName='fluent mybatis is powerful!', gmtCreate=null, gmtModified=null, isDeleted=false}

神奇吧! 我們再到數(shù)據(jù)庫中查看一下結果

在這里插入圖片描述

現(xiàn)在,我們已經(jīng)通過一個簡單例子演示了fluent mybatis的強大功能,
在進一步介紹fluent mybatis更強大功能前,我們揭示一下為啥我們只寫了一個數(shù)據(jù)表對應的Entity類,
卻擁有了一系列增刪改查的數(shù)據(jù)庫操作方法。

fluent mybatis根據(jù)Entity類上@FluentMybatis注解在編譯時,
會在target目錄class目錄下自動編譯生成一系列文件:

在這里插入圖片描述

核心接口類, 使用時需要了解

  • mapper/*Mapper: mybatis的Mapper定義接口, 定義了一系列通用的數(shù)據(jù)操作接口方法。
  • dao/*BaseDao: Dao實現(xiàn)基類, 所有的DaoImpl都繼承各自基類
  • 根據(jù)分層編碼的原則,我們不會在Service類中直接使用Mapper類,而是引用Dao類。我們在Dao實現(xiàn)類中根據(jù)條件實現(xiàn)具體的數(shù)據(jù)操作方法。
  • wrapper/*Query: fluent mybatis核心類, 用來進行動態(tài)sql的構造, 進行條件查詢。
  • wrapper/*Updater: fluent mybatis核心類, 用來動態(tài)構造update語句。
  • entity/*EntityHelper: Entity幫助類, 實現(xiàn)了Entity和Map的轉換方法
  • 輔助實現(xiàn)時, 實現(xiàn)fluent mybatis動態(tài)sql拼裝和fluent api時內部用到的類,使用時無需了解
  • 在使用上,我們主要會接觸到上述5個生成的java類。Fluent Mybatis為了實現(xiàn)動態(tài)拼接和Fluent API功能,還生成了一系列輔助類。
  • helper/*Mapping: 表字段和Entity屬性映射定義類
  • helper/*SqlProviderP: Mapper接口動態(tài)sql提供者
  • helper/*WrapperHelper: Query和Updater具體功能實現(xiàn), 包含幾個實現(xiàn):select, where, group by, having by, order by, limit

Fluent Mybatis文檔&示例

Fluent Mybatis源碼, github

到此這篇關于FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法的文章就介紹到這了,更多相關FluentMybatis實現(xiàn)mybatis動態(tài)sql內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • MyBatis實踐之DAO與Mapper

    MyBatis實踐之DAO與Mapper

    MyBatis前身是iBatis,是一個基于Java的數(shù)據(jù)持久層/對象關系映射(ORM)框架.通過本文給大家介紹MyBatis實踐之DAO與Mapper的相關知識,需要的朋友參考下吧
    2016-03-03
  • 使用maven的profile構建不同環(huán)境配置的方法

    使用maven的profile構建不同環(huán)境配置的方法

    這篇文章主要介紹了使用maven的profile構建不同環(huán)境配置的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • springBoot2.X配置全局捕獲異常的操作

    springBoot2.X配置全局捕獲異常的操作

    這篇文章主要介紹了springBoot2.X配置全局捕獲異常的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 關于JAVA11中圖片與BASE64相互轉換的實現(xiàn)

    關于JAVA11中圖片與BASE64相互轉換的實現(xiàn)

    這篇文章主要介紹了關于JAVA11中圖片與BASE64相互轉換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Java三種獲取redis的連接及redis_String類型演示(適合新手)

    Java三種獲取redis的連接及redis_String類型演示(適合新手)

    這篇文章主要介紹了Java三種獲取redis的連接及redis_String類型演示(適合新手),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • SpringBoot使用Shiro實現(xiàn)動態(tài)加載權限詳解流程

    SpringBoot使用Shiro實現(xiàn)動態(tài)加載權限詳解流程

    本文小編將基于?SpringBoot?集成?Shiro?實現(xiàn)動態(tài)uri權限,由前端vue在頁面配置uri,Java后端動態(tài)刷新權限,不用重啟項目,以及在頁面分配給用戶?角色?、?按鈕?、uri?權限后,后端動態(tài)分配權限,用戶無需在頁面重新登錄才能獲取最新權限,一切權限動態(tài)加載,靈活配置
    2022-07-07
  • 老生常談Scanner的基本用法

    老生常談Scanner的基本用法

    下面小編就為大家?guī)硪黄仙U凷canner的基本用法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    本文主要介紹了Springboot使用redis實現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • SpringBoot項目后端開發(fā)邏輯全面梳理

    SpringBoot項目后端開發(fā)邏輯全面梳理

    這篇文章主要介紹了SpringBoot項目后端開發(fā)邏輯全面梳理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java String index out of range:100錯誤解決方案詳解

    Java String index out of range:100錯誤解決方案詳解

    這篇文章主要介紹了Java String index out of range:100錯誤解決方案詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08

最新評論