<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>

          Mybatis-Flex優(yōu)雅的 Mybatis 增強(qiáng)框架

          聯(lián)合創(chuàng)作 · 2023-09-30 01:29

          Mybatis-Flex 是一個(gè)優(yōu)雅的 Mybatis 增強(qiáng)框架。

          特征

          • 很輕量,整個(gè)框架只依賴 Mybatis 再無其他第三方依賴
          • Entity 類的基本增刪改查、以及分頁查詢
          • Row 通用映射支持,可以無需實(shí)體類對(duì)數(shù)據(jù)庫進(jìn)行增刪改查
          • 支持多種數(shù)據(jù)庫類型,自由通過方言持續(xù)擴(kuò)展
          • 支持聯(lián)合主鍵,以及不同的主鍵內(nèi)容生成策略
          • 極其友好的 SQL 聯(lián)動(dòng)查詢,IDE 自動(dòng)提示不再擔(dān)心出錯(cuò)
          • 更多小驚喜

          hello world

          第一步:編寫 Entity 實(shí)體類

          @Table("tb_account")
          public class Account {
          
              @Id()
              private Long id;
              private String userName;
              private Date birthday;
              private int sex;
          
              //getter setter
          }
          

          第二步,編寫 Mapper 類,并繼承 BaseMapper

          public interface AccountMapper extends BaseMapper<Account> {
              //只需定義 Mapper 接口即可,可以無任何內(nèi)容。
          }
          

          第三步:開始查詢數(shù)據(jù)

          示例 1:查詢 1 條數(shù)據(jù)

          class HelloWorld {
              public static void main(String... args) {
          
                  HikariDataSource dataSource = new HikariDataSource();
                  dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
                  dataSource.setUsername("username");
                  dataSource.setPassword("password");
          
                  MybatisFlexBootstrap.getInstance()
                          .setDatasource(dataSource)
                          .addMapper(AccountMapper.class)
                          .start();
          
          
                  //示例1:查詢 id=100 條數(shù)據(jù)
                  Account account = MybatisFlexBootstrap.getInstance()
                          .execute(AccountMapper.class, mapper ->
                                  mapper.selectOneById(100)
                          );
              }
          }
          

          示例2:查詢列表

          class HelloWorld {
              public static void main(String... args) {
          
                  HikariDataSource dataSource = new HikariDataSource();
                  dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
                  dataSource.setUsername("username");
                  dataSource.setPassword("password");
          
                  MybatisFlexBootstrap.getInstance()
                          .setDatasource(dataSource)
                          .addMapper(AccountMapper.class)
                          .start();
                  
                  //示例2:通過 QueryWrapper 構(gòu)建條件查詢數(shù)據(jù)列表
                  QueryWrapper query = QueryWrapper.create()
                          .select()
                          .from(ACCOUNT)
                          .where(ACCOUNT.ID.ge(100))
                          .and(ACCOUNT.USER_NAME.like("張").or(ACCOUNT.USER_NAME.like("李")));
          
                  // 執(zhí)行 SQL:
                  // ELECT * FROM `tb_account`
                  // WHERE `tb_account`.`id` >=  100
                  // AND (`tb_account`.`user_name` LIKE '%張%' OR `tb_account`.`user_name` LIKE '%李%' )
                  List<Account> accounts = MybatisFlexBootstrap.getInstance()
                          .execute(AccountMapper.class, mapper ->
                                  mapper.selectListByQuery(query)
                          );
                  
              }
          }
          

          示例3:分頁查詢

          class HelloWorld {
              public static void main(String... args) {
          
                  HikariDataSource dataSource = new HikariDataSource();
                  dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
                  dataSource.setUsername("username");
                  dataSource.setPassword("password");
          
                  MybatisFlexBootstrap.getInstance()
                          .setDatasource(dataSource)
                          .addMapper(AccountMapper.class)
                          .start();
          
                  // 示例3:分頁查詢
                  // 查詢第 5 頁,每頁 10 條數(shù)據(jù),通過 QueryWrapper 構(gòu)建條件查詢
                  QueryWrapper query = QueryWrapper.create()
                          .select()
                          .from(ACCOUNT)
                          .where(ACCOUNT.ID.ge(100))
                          .and(ACCOUNT.USER_NAME.like("張").or(ACCOUNT.USER_NAME.like("李")))
                          .orderBy(ACCOUNT.ID.desc());
          
                  // 執(zhí)行 SQL:
                  // ELECT * FROM `tb_account`
                  // WHERE `tb_account`.`id` >=  100
                  // AND (`tb_account`.`user_name` LIKE '%張%' OR `tb_account`.`user_name` LIKE '%李%' )
                  // ORDER BY `tb_account`.`id` DESC
                  // LIMIT 40,10
                  Page<Account> accounts = MybatisFlexBootstrap.getInstance()
                          .execute(AccountMapper.class, mapper ->
                                  mapper.paginate(5, 10, query)
                          );
          
              }
          }
          
          瀏覽 21
          點(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>
                  骚B视频 欧美大香蕉在线片 | 真人视频,一级A片 | 婷婷五月天色播 | 西西特级444人体大胆图片 | 91无码秘 在线无码观看蜜桃 |