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

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

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

開始第一個(gè)例子: Hello World

 新建Java工程,設(shè)置maven依賴

新建maven工程,設(shè)置項(xiàng)目編譯級(jí)別為Java8及以上,引入fluent mybatis依賴包。

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

新建演示用的數(shù)據(jù)庫結(jié)構(gòu)

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)建時(shí)間',
    gmt_modified datetime   DEFAULT NULL COMMENT '更新時(shí)間',
    is_deleted   tinyint(2) DEFAULT 0 COMMENT '是否邏輯刪除'
) ENGINE = InnoDB
  CHARACTER SET = utf8 comment '簡(jiǎn)單演示表';

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

創(chuàng)建數(shù)據(jù)庫表對(duì)應(yīng)的Entity類: HelloWorldEntity, 你只需要簡(jiǎn)單的做3個(gè)動(dòng)作:

  • 根據(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 方法
}

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

運(yùn)行測(cè)試來見證Fluent Mybatis的神奇

為了運(yùn)行測(cè)試, 我們還需要進(jìn)行JUnit和Spring Test相關(guān)配置。

配置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 {
    /**
     * 設(shè)置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í)行測(cè)試

  • 使用spring-test初始化spring容器
  • 注入HelloWorldEntity對(duì)應(yīng)的Mapper類: HelloWorldMapper, 這個(gè)類是fluent mybatis編譯時(shí)生成的。
  • 使用HelloWorldMapper進(jìn)行刪除、插入、查詢、修改操作。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfig.class)
public class HelloWorldTest {
    /**
     * fluent mybatis編譯時(shí)生成的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());
        /**
         * 控制臺(tái)直接打印出查詢結(jié)果
         */
        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)
        );
        /**
         * 控制臺(tái)直接打印出查詢結(jié)果
         */
        System.out.println("2. HelloWorldEntity:" + result2.toString());
    }
}

執(zhí)行Junit4測(cè)試方法,控制臺(tái)輸出

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}

神奇吧! 我們?cè)俚綌?shù)據(jù)庫中查看一下結(jié)果

在這里插入圖片描述

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

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

在這里插入圖片描述

核心接口類, 使用時(shí)需要了解

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

Fluent Mybatis文檔&示例

Fluent Mybatis源碼, github

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

相關(guān)文章

最新評(píng)論

遂川县| 黔江区| 盐城市| 若尔盖县| 饶河县| 中卫市| 三都| 阿勒泰市| 长沙县| 扶沟县| 宣恩县| 徐闻县| 岳阳县| 临清市| 湘潭县| 虎林市| 平罗县| 义乌市| 集贤县| 皋兰县| 大冶市| 普洱| 个旧市| 象山县| 安岳县| 诏安县| 淮南市| 裕民县| 鄂州市| 五家渠市| 韶关市| 昌平区| 措美县| 揭西县| 哈尔滨市| 景泰县| 田阳县| 抚顺市| 焉耆| 嘉峪关市| 枣阳市|