<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          MyFatMyBatis DAO 增強(qiáng)工具

          聯(lián)合創(chuàng)作 · 2023-09-21 15:13

          簡介 | Intro

          MyFat是MyBatis的DAO功能增強(qiáng)插件,利用全功能持久層工具jSqlBox來補(bǔ)足MyBatis缺少的CRUD功能。

          優(yōu)點(diǎn) | Advantages

          1. 無侵入性,透明式增強(qiáng)。對(duì)于使用MyBatis的項(xiàng)目只需要添加MyFat依賴即可,不用更改任何其它配置文件和代碼,即使使用了其它插件如MyBatis-Plus等,依然可以用MyFat來增強(qiáng)。

          2. 架構(gòu)優(yōu)秀。MyFat的源碼只有7個(gè)類,開發(fā)時(shí)間只有兩周,但實(shí)現(xiàn)的功能絕不比其它MyBatis插件少。這是因?yàn)樗捎玫氖墙M合模式,即將另一個(gè)全功能持久層工具jSqlBox組合到MyBatis中,功能互補(bǔ),而不是從頭開發(fā)。內(nèi)核和插件是獨(dú)立的,互不依賴,可維護(hù)性好 。

          3. 功能全。以下為增強(qiáng)后的功能:
            跨數(shù)據(jù)庫分頁、DDl生成、實(shí)體源碼生成、函數(shù)變換、主鍵生成、多種SQL寫法、DataMapper、ActiveRecord、Tail、實(shí)體越級(jí)關(guān)聯(lián)查詢、主從、分庫分表等。引入了MyFat后,可以說只要你能想到的SQL寫法,一定已經(jīng)包括進(jìn)來了。(這么多種SQL寫法,是因?yàn)榧軜?gòu)導(dǎo)致的,不是為了炫技)

          與其它MyBatis插件的區(qū)別

          1. 目的不一樣,其它插件是為MyBatis錦上添花,這個(gè)插件的目的是讓使用者了解一下jSqlBox的功能,搶一搶MyBatis的市場,希望有朝一日將MyBatis從項(xiàng)目中踢出去,因?yàn)镸yBatis的幾個(gè)主要功能如SqlMapper、用XML管理SQL、條件查詢器,在作者看來都是反模式,在jSqlBox中都有不同的實(shí)現(xiàn)方式。之所以叫MyFat這個(gè)名字,是因?yàn)镸yBatis已經(jīng)夠胖了(1.6M),再給它增點(diǎn)肥也沒關(guān)系了。

          2. ActiveRecord可以只聲明接口,不一定需要繼承類,也不需要定義Mapper,更無侵入性

          3. 沒有專門的分頁方法,但是所有SQL查詢都可以分頁,無侵入性

          4. 支持的SQL寫法更多,如參數(shù)內(nèi)嵌式寫法、實(shí)體越級(jí)關(guān)聯(lián)查詢等

          5. 實(shí)體注解更符合JPA標(biāo)準(zhǔn)

          6. 文檔少,是的,文檔少也是優(yōu)點(diǎn),因?yàn)镸yFat源碼只有7個(gè)類,你指望它會(huì)有多少文檔? 使用MyFat需要查閱jSqlBox的用戶手冊(cè),但是這個(gè)與MyBatis已經(jīng)沒有一毛錢關(guān)系了。

          文檔 | Documentation

          中文JavaDoc

          配置 | Configuration

          MyFat需Java8支持,在pom.xml中加入以下內(nèi)容即可,注意MyFat必須先于MyBatis加載: 

          <dependency>  
             <groupId>com.github.drinkjava2</groupId>  
             <artifactId>myfat</artifactId>  
             <version>1.0.0</version> <!--或最新版-->  
          </dependency>  
          
          <dependency>  
              <groupId>org.mybatis</groupId>  
              <artifactId>mybatis</artifactId>  
              <version>3.4.6</version> <!--或最新版-->  
          </dependency>

          如果在Spring環(huán)境下,則上面的MyBatis依賴項(xiàng)要改成Spring的:

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
          	  <version>1.3.2</version> <!--或最新版--> 
            </dependency>

          示例 | First Example

          以下示例演示了MyFat的使用,至于SqlSessionFactory的配置,與標(biāo)準(zhǔn)MyBatis的配置一模一樣(透明式增強(qiáng)),略去。完整的源碼可見Demo目錄下的myfattest:

          SqlSession session = sqlSessionFactory.openSession(false);
          try {
              // SqlSession有了增強(qiáng)的SQL方法,jSqlBox中的pinte系列方法可用
              Assert.assertEquals(50, session.iQueryForLongValue("select count(*) from user_tb where age>", ques(50),
                      " and age <= ", ques(200)));
          
              Assert.assertEquals(100, session.eCountAll(User.class, " where age>0"));
          
              UserMapper mapper = session.getMapper(UserMapper.class);
          
              // 如果mapper繼承于RootMapper<User>, 也有增強(qiáng)的pintea系方法了
              Assert.assertEquals(100, mapper.aCountAll(" where age>?", param(0), " and age<?", param(200)));
              
              Helper.pagin(2, 15);// 無侵入的分頁
              try {
                  List<User> users = mapper.getUsersOlderThan(50);
                  Assert.assertEquals(15, users.size());
              } finally {
                  Helper.clear();
              }
          
              new User().insert(session.ctx());// ActiveRecord寫法 
              new Tail().putTail("id", "1").insert(session.ctx(), tail("user_tb")); // Tail相當(dāng)于jFinal中的Record
              session.ctx().iExecute("insert into user_tb (id) values(?)", param('a'));
              session.iExecute("insert into user_tb (id) values(?)", param('b'));
              mapper.iExecute("insert into user_tb (id) values(?)", param('c'));
              mapper.insertOneUser("d");
          } catch (Exception e) {
              System.out.println("Exception:" + e.getMessage());
          } finally {
              session.close();
          }

          上例是手工進(jìn)行SqlSession的獲取和關(guān)閉,適用于純MyBatis環(huán)境。如果在Spring或SpringBoot環(huán)境下,則有以下寫法示例:

          @Transactional
              public void insertWithDiv0() {
                  for (int i = 0; i < 100; i++)
                      new Customer().putField("name", "Foo" + i).insert();
                  Assert.assertEquals(15, new Customer().findAll(" where name<>''", pagin(2, 15)).size());
          
                  customerMapper.insertByIdAndName("2", "Sam");
                  customerMapper.iExecute("insert into customer (id,name) values (?,?)", param("3", "Tom"));
                  iExecute("insert into customer (id,name) values (?,?)", param("4", "Cat"));
                  new Customer().insert(); 
                  Assert.assertEquals(104, new Customer().countAll());
                  System.out.println(1 / 0);
              }

          Mapper由Spring注入,采用聲明式事務(wù),不需要手工獲取和管理SqlSession。

          示例2中 new Customer().insert(); 這種ActiveRedord寫法,不需要傳入一個(gè)ctx參數(shù),它的前提是設(shè)定了一個(gè)缺省全局上下文(SqlBoxContext),詳見jSqlBox項(xiàng)目介紹及demo/myfat-springboot示例。

          示例 | Demo

          • myfattest 演示純MyBatis環(huán)境下,手工獲取SqlSesion和Mapper進(jìn)行操作

          • myfat-springboot 演示MyFat在SpringBoot環(huán)境下的配置和使用

          作者其它開源項(xiàng)目 | Other Projects

          期望 | Futures

          歡迎發(fā)issue提出更好的意見或提交PR,幫助完善MyFat

          版權(quán) | License

          Apache 2.0

          關(guān)注我 | About Me

          Github
          碼云

          瀏覽 19
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大香蕉伊人网 | 操逼官网| 女人成人网站 | 欧美色就是色 | 欧美做爰BBB性BBBBB8 |