1. 棄用 Mybatis,自研一款輕量級、高性能、強(qiáng)類型的 ORM 框架

        共 22136字,需瀏覽 45分鐘

         ·

        2024-06-22 14:43

        來源:juejin.cn/post/7294908459002429475

        ?? 歡迎加入小哈的星球,你將獲得: 專屬的項(xiàng)目實(shí)戰(zhàn) / 1v1 提問 / Java 學(xué)習(xí)路線 / 學(xué)習(xí)打卡 / 每月贈書 / 社群討論

        • 新項(xiàng)目:《從零手?jǐn)]:仿小紅書(微服務(wù)架構(gòu))》 正在持續(xù)爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17..., 點(diǎn)擊查看項(xiàng)目介紹;
        • 《從零手?jǐn)]:前后端分離博客項(xiàng)目(全棧開發(fā))》 2期已完結(jié),演示鏈接:http://116.62.199.48/;

        截止目前,累計(jì)輸出 47w+ 字,講解圖 2090+ 張,還在持續(xù)爆肝中.. 后續(xù)還會上新更多項(xiàng)目,目標(biāo)是將 Java 領(lǐng)域典型的項(xiàng)目都整一波,如秒殺系統(tǒng), 在線商城, IM 即時(shí)通訊,Spring Cloud Alibaba 等等,戳我加入學(xué)習(xí),已有1600+小伙伴加入(早鳥價(jià)超低)

        背景

        轉(zhuǎn)java后的幾年時(shí)間里面一直在尋找一個(gè)類似.net的orm,不需要很特別的功能,僅希望90%的場景都可以通過強(qiáng)類型語法來編寫符合直覺的sql,來操作數(shù)據(jù)庫編寫業(yè)務(wù)。

        但是一直沒有找到,Mybatis-Plus的單表讓我在最初的時(shí)間段內(nèi)看到了希望,不過隨著使用的深入越發(fā)的覺得Mybatis-Plus只是一個(gè)殘缺的orm,因?yàn)榇蟛糠謭鼍安恢С直磉_(dá)式或者強(qiáng)類型會導(dǎo)致它本身的很多特性都無法使用,比如你配置了軟刪除,那么如果你遇到了join不好意思軟刪除你需要自己處理,很多配置會隨著手寫sql的加入變的那么的不智能,甚至表現(xiàn)得和sql helper沒區(qū)別。

        別說Mybatis-Plus-Join了,這玩意更逆天,如果一個(gè)orm想寫出符合自己的sql,需要不斷地調(diào)試嘗試來“拼接”出想要的語句,那么他就稱不上一個(gè)ORM,連sql builder也算不上。Mybatis-Plus-Join就是這樣。

        所以在4-5年后我終于忍受不了了,決定自研一款orm。參考現(xiàn)有.net生態(tài)十分完整的orm代碼和幾乎完美符合擴(kuò)展性和語義性的鏈?zhǔn)奖磉_(dá)式,讓.net的orm帶到j(luò)ava中。

        查詢

        查詢第一條數(shù)據(jù)

        Topic topic = easyQuery.queryable(Topic.class)
                            .where(o -> o.eq(Topic::getId, "123"))
                            .firstOrNull();

        ==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE `id` = ? LIMIT 1
        ==> Parameters: 123(String)
        <== Time Elapsed: 2(ms)
        <== Total: 0

        查詢并斷言至多一條數(shù)據(jù)

        Topic topic = easyQuery.queryable(Topic.class)
                            .where(o -> o.eq(Topic::getId, "123"))
                            .singleOrNull();

        ==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE `id` = ?
        ==> Parameters: 123(String)
        <== Time Elapsed: 2(ms)
        <== Total: 0

        查詢多條數(shù)據(jù)

        List<Topic> topics = easyQuery.queryable(Topic.class)
                            .where(o -> o.eq(Topic::getId, "123"))
                            .toList();

        ==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE `id` = ?
        ==> Parameters: 123(String)
        <== Time Elapsed: 2(ms)
        <== Total: 0

        查詢自定義列

        Topic topic = easyQuery.queryable(Topic.class)
                            .where(o -> o.eq(Topic::getId, "1"))
                            .select(o->o.column(Topic::getId).column(Topic::getTitle))
                            .firstOrNull();

        ==> Preparing: SELECT `id`,`title` FROM `t_topic` WHERE `id` = ? LIMIT 1
        ==> Parameters: 1(String)
        <== Time Elapsed: 2(ms)
        <== Total: 1

        分頁查詢

         EasyPageResult<Topic> topicPageResult = easyQuery
                        .queryable(Topic.class)
                        .where(o -> o.isNotNull(Topic::getId))
                        .toPageResult(1, 20);

        ==> Preparing: SELECT  COUNT(1)  FROM t_topic t WHERE t.`id` IS NOT NULL
        <== Total: 1
        ==> Preparing: SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM t_topic t WHERE t.`id` IS NOT NULL LIMIT 20
        <== Total: 20

        將表達(dá)式轉(zhuǎn)成匿名表嵌套查詢

        //  SELECT `id`,`title` FROM `t_topic` WHERE `id` = ? 
        Queryable<Topic> query = easyQuery.queryable(Topic.class)
                            .where(o -> o.eq(Topic::getId, "1"))
                            .select(Topic.class, o -> o.column(Topic::getId).column(Topic::getTitle));

        List<Topic> list = query.leftJoin(Topic.class, (t, t1) -> t.eq(t1, Topic::getId, Topic::getId))
                            .where((t, t1) -> {
                                t1.eq(Topic::getId, "123");
                                t.eq(Topic::getId, "456");
                            }).toList();

        SELECT t1.`id`,t1.`title` 
        FROM (SELECT t.`id`,t.`title` FROM `t_topic` t WHERE t.`id` = ?) t1 
        LEFT JOIN `t_topic` t2 ON t1.`id` = t2.`id` WHERE t2.`id` = ? AND t1.`id` = ? 

        ==> Preparing: SELECT t1.`id`,t1.`title` FROM (SELECT t.`id`,t.`title` FROM `t_topic` t WHERE t.`id` = ?) t1 LEFT JOIN `t_topic` t2 ON t1.`id` = t2.`id` WHERE t2.`id` = ? AND t1.`id` = ?
        ==> Parameters: 1(String),123(String),456(String)
        <== Time Elapsed: 5(ms)
        <== Total: 0

        子查詢

        //SELECT * FROM `t_blog` t1 WHERE t1.`deleted` = ? AND t1.`id` = ?
         Queryable<BlogEntity> subQueryable = easyQuery.queryable(BlogEntity.class)
                        .where(o -> o.eq(BlogEntity::getId, "1"));


        List<Topic> x = easyQuery
                .queryable(Topic.class).where(o -> o.exists(subQueryable.where(q -> q.eq(o, BlogEntity::getId, Topic::getId)))).toList();


        ==> Preparing: SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM `t_topic` t WHERE EXISTS (SELECT 1 FROM `t_blog` t1 WHERE t1.`deleted` = ? AND t1.`id` = ? AND t1.`id` = t.`id`)
        ==> Parameters: false(Boolean),1(String)
        <== Time Elapsed: 3(ms)
        <== Total: 1

        多表join查詢

        Topic topic = easyQuery
                        .queryable(Topic.class)
                        .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId))
                        .where(o -> o.eq(Topic::getId, "3"))
                        .firstOrNull();

        ==> Preparing: SELECT t.`id`,t.`stars`,t.`title`,t.`create_time` FROM t_topic t LEFT JOIN t_blog t1 ON t1.`deleted` = ? AND t.`id` = t1.`id` WHERE t.`id` = ? LIMIT 1
        ==> Parameters: false(Boolean),3(String)
        <== Total: 1

        流式結(jié)果大數(shù)據(jù)迭代返回

        try(JdbcStreamResult<BlogEntity> streamResult = easyQuery.queryable(BlogEntity.class).where(o -> o.le(BlogEntity::getStar, 100)).orderByAsc(o -> o.column(BlogEntity::getCreateTime)).toStreamResult()){

                    LocalDateTime begin = LocalDateTime.of(2020, 1, 1, 1, 1, 1);
                    int i = 0;
                    for (BlogEntity blog : streamResult.getStreamIterable()) {
                        String indexStr = String.valueOf(i);
                        Assert.assertEquals(indexStr, blog.getId());
                        Assert.assertEquals(indexStr, blog.getCreateBy());
                        Assert.assertEquals(begin.plusDays(i), blog.getCreateTime());
                        Assert.assertEquals(indexStr, blog.getUpdateBy());
                        Assert.assertEquals(begin.plusDays(i), blog.getUpdateTime());
                        Assert.assertEquals("title" + indexStr, blog.getTitle());
        //            Assert.assertEquals("content" + indexStr, blog.getContent());
                        Assert.assertEquals("http://blog.easy-query.com/" + indexStr, blog.getUrl());
                        Assert.assertEquals(i, (int) blog.getStar());
                        Assert.assertEquals(0, new BigDecimal("1.2").compareTo(blog.getScore()));
                        Assert.assertEquals(i % 3 == 0 ? 0 : 1, (int) blog.getStatus());
                        Assert.assertEquals(0, new BigDecimal("1.2").multiply(BigDecimal.valueOf(i)).compareTo(blog.getOrder()));
                        Assert.assertEquals(i % 2 == 0, blog.getIsTop());
                        Assert.assertEquals(i % 2 == 0, blog.getTop());
                        Assert.assertEquals(false, blog.getDeleted());
                        i++;
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

        ==> Preparing: SELECT `id`,`create_time`,`update_time`,`create_by`,`update_by`,`deleted`,`title`,`content`,`url`,`star`,`publish_time`,`score`,`status`,`order`,`is_top`,`top` FROM `t_blog` WHERE `deleted` = ? AND `star` <= ? ORDER BY `create_time` ASC
        ==> Parameters: false(Boolean),100(Integer)
        <== Time Elapsed: 6(ms)

        自定義VO返回

        List<QueryVO> list = easyQuery
              .queryable(Topic.class)
              //第一個(gè)join采用雙參數(shù),參數(shù)1表示第一張表Topic 參數(shù)2表示第二張表 BlogEntity
              .leftJoin(BlogEntity.class, (t, t1) -> t.eq(t1, Topic::getId, BlogEntity::getId))
              //第二個(gè)join采用三參數(shù),參數(shù)1表示第一張表Topic 參數(shù)2表示第二張表 BlogEntity 第三個(gè)參數(shù)表示第三張表 SysUser
              .leftJoin(SysUser.class, (t, t1, t2) -> t.eq(t2, Topic::getId, SysUser::getId))
              .where(o -> o.eq(Topic::getId, "123"))//單個(gè)條件where參數(shù)為主表Topic
              //支持單個(gè)參數(shù)或者全參數(shù),全參數(shù)個(gè)數(shù)為主表+join表個(gè)數(shù) 鏈?zhǔn)綄懛ㄆ陂g可以通過then來切換操作表
              .where((t, t1, t2) -> t.eq(Topic::getId, "123").then(t1).like(BlogEntity::getTitle, "456")
                      .then(t2).eq(BaseEntity::getCreateTime, LocalDateTime.now()))
              //如果不想用鏈?zhǔn)降?span style="color: #c678dd;line-height: 26px;">then來切換也可以通過lambda 大括號方式執(zhí)行順序就是代碼順序,默認(rèn)采用and鏈接
              .where((t, t1, t2) -> {
                  t.eq(Topic::getId, "123");
                  t1.like(BlogEntity::getTitle, "456");
                  t1.eq(BaseEntity::getCreateTime, LocalDateTime.now());
              })
              .select(QueryVO.class, (t, t1, t2) ->
                      //將第一張表的所有屬性的列映射到vo的列名上,第一張表也可以通過columnAll將全部字段映射上去
                      // ,如果后續(xù)可以通過ignore方法來取消掉之前的映射關(guān)系
                      t.column(Topic::getId)
                              .then(t1)
                              //將第二張表的title字段映射到VO的field1字段上
                              .columnAs(BlogEntity::getTitle, QueryVO::getField1)
                              .then(t2)
                              //將第三張表的id字段映射到VO的field2字段上
                              .columnAs(SysUser::getId, QueryVO::getField2)
              ).toList();

        表單條件動態(tài)查詢

        BlogQuery2Request query = new BlogQuery2Request();
        query.setContent("標(biāo)題");
        query.setPublishTimeEnd(LocalDateTime.now());
        query.setStatusList(Arrays.asList(1,2));

        List<BlogEntity> queryable = easyQuery.queryable(BlogEntity.class)
                .whereObject(query).toList();


        ==> Preparing: SELECT `id`,`create_time`,`update_time`,`create_by`,`update_by`,`deleted`,`title`,`content`,`url`,`star`,`publish_time`,`score`,`status`,`order`,`is_top`,`top` FROM `t_blog` WHERE `deleted` = ? AND `content` LIKE ? AND `publish_time` <= ? AND `status` IN (?,?)
        ==> Parameters: false(Boolean),%標(biāo)題%(String),2023-07-14T22:37:47.880(LocalDateTime),1(Integer),2(Integer)
        <== Time Elapsed: 2(ms)
        <== Total: 0

        基本類型結(jié)果返回

        List<String> list = easyQuery.queryable(Topic.class)
                        .where(o -> o.eq(Topic::getId, "1"))
                        .select(String.class, o -> o.column(Topic::getId))
                        .toList();

        ==> Preparing: SELECT t.`id` FROM `t_topic` t WHERE t.`id` = ?
        ==> Parameters: 1(String)
        <== Time Elapsed: 2(ms)
        <== Total: 1

        分組查詢

        List<TopicGroupTestDTO> topicGroupTestDTOS = easyQuery.queryable(Topic.class)
                        .where(o -> o.eq(Topic::getId, "3"))
                        .groupBy(o->o.column(Topic::getId))
                        .select(TopicGroupTestDTO.class, o->o.columnAs(Topic::getId,TopicGroupTestDTO::getId).columnCount(Topic::getId,TopicGroupTestDTO::getIdCount))
                        .toList();


        ==> Preparing: SELECT t.`id` AS `id`,COUNT(t.`id`) AS `idCount` FROM t_topic t WHERE t.`id` = ? GROUP BY t.`id`
        ==> Parameters: 3(String)
        <== Total: 1

        //groupKeysAs快速選擇并且給別名
        List<TopicGroupTestDTO> topicGroupTestDTOS = easyQuery.queryable(Topic.class)
                        .where(o -> o.eq(Topic::getId, "3"))
                        .groupBy(o->o.column(Topic::getId))
                        .select(TopicGroupTestDTO.class, o->o.groupKeysAs(0, TopicGroupTestDTO::getId).columnCount(Topic::getId,TopicGroupTestDTO::getIdCount))
                        .toList();


        ==> Preparing: SELECT t.`id` AS `id`,COUNT(t.`id`) AS `idCount` FROM t_topic t WHERE t.`id` = ? GROUP BY t.`id`
        ==> Parameters: 3(String)
        <== Total: 1

        原生sql片段

        String sql = easyQuery.queryable(H2BookTest.class)
                    .where(o -> o.sqlNativeSegment("regexp_like({0},{1})", it -> it.expression(H2BookTest::getPrice)
                                    .value("^Ste(v|ph)en$")))
                    .select(o -> o.columnAll()).toSQL();

        SELECT id,name,edition,price,store_id FROM t_book_test WHERE regexp_like(price,?)

        數(shù)據(jù)庫函數(shù)列

        用戶存儲的數(shù)據(jù)是base64結(jié)果,但是內(nèi)存中是普通的字符串或者其他數(shù)據(jù),easy-query提供了無感的使用,譬如pgsql的geo等地理相關(guān)數(shù)據(jù)

        數(shù)據(jù)庫函數(shù)列:

        ?

        https://xuejm.gitee.io/easy-query-doc/guide/adv/column-sql-func-auto.html

        ?

        支持like的高性能加密解密

        用來實(shí)現(xiàn)支持like模式的高性能加密解密,支持emoji和非emoji兩種用戶可以自行選擇

        更多功能比如數(shù)據(jù)追蹤差異更新,數(shù)據(jù)原子更新,分庫分表(老行當(dāng)了肯定要支持),一款本無依賴雙語(java/kotlin)都支持的高性能orm

        github地址:

        https://github.com/dromara/easy-query

        gitee地址:

        https://gitee.com/xuejm/easy-query

        ?? 歡迎加入小哈的星球,你將獲得: 專屬的項(xiàng)目實(shí)戰(zhàn) / 1v1 提問 / Java 學(xué)習(xí)路線 / 學(xué)習(xí)打卡 / 每月贈書 / 社群討論

        • 新項(xiàng)目:《從零手?jǐn)]:仿小紅書(微服務(wù)架構(gòu))》 正在持續(xù)爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17..., 點(diǎn)擊查看項(xiàng)目介紹;
        • 《從零手?jǐn)]:前后端分離博客項(xiàng)目(全棧開發(fā))》 2期已完結(jié),演示鏈接:http://116.62.199.48/;

        截止目前,累計(jì)輸出 47w+ 字,講解圖 2090+ 張,還在持續(xù)爆肝中.. 后續(xù)還會上新更多項(xiàng)目,目標(biāo)是將 Java 領(lǐng)域典型的項(xiàng)目都整一波,如秒殺系統(tǒng), 在線商城, IM 即時(shí)通訊,Spring Cloud Alibaba 等等,戳我加入學(xué)習(xí),已有1600+小伙伴加入(早鳥價(jià)超低)


            
               

        1. 我的私密學(xué)習(xí)小圈子~

        2. SpringBoot + 虛擬線程,鳥槍換大炮!

        3. 開源的國產(chǎn) Web 防火墻工具,火爆了!

        4. 三次輸錯(cuò)密碼后,系統(tǒng)是怎么做到不讓我繼續(xù)嘗試的?

        最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。

        獲取方式:點(diǎn)“在看”,關(guān)注公眾號并回復(fù) Java 領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

        PS:因公眾號平臺更改了推送規(guī)則,如果不想錯(cuò)過內(nèi)容,記得讀完點(diǎn)一下在看,加個(gè)星標(biāo),這樣每次新文章推送才會第一時(shí)間出現(xiàn)在你的訂閱列表里。

        點(diǎn)“在看”支持小哈呀,謝謝啦

        瀏覽 179
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 小舞被黄动漫免费视频 | 欧美成人高潮一二区在线看 | 男女AA视频 | 中国亂倫一級A片 | 女人扒开腿秘 免费网站 |