<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 開發(fā)神器:Fast MyBatis 超好用

          共 2952字,需瀏覽 6分鐘

           ·

          2022-04-24 08:38

          fastmybatis 是一個 mybatis 開發(fā)框架,其宗旨為:簡單、快速、有效。零配置快速上手,無需編寫 xml 文件即可完成 CRUD 操作。同時支持 mysql、sqlserver、oracle、postgresql、sqlite。

          支持自定義 SQL,對于基本的增刪改查不需要寫 SQL,對于其它特殊 SQL(如統(tǒng)計 SQL )可寫在 xml 中,支持與 Spring-Boot 集成,依賴 starter 即可,支持插件編寫,支持 ActiveRecord 模式,提供通用 Service,輕量級,無侵入性,是官方 MyBatis 的一種擴展

          快速開始(SpringBoot)

          • 新建一個 Spring boot 項目
          • pom.xml 添加 fastmybatis-spring-boot-starter
          <dependency>
          ????<groupId>net.oschina.durcframeworkgroupId>
          ????<artifactId>fastmybatis-spring-boot-starterartifactId>
          ????<version>最新版本version>
          dependency>
          • 增刪改查例子

          假設(shè)數(shù)據(jù)庫有張?t_user?表,添加對應(yīng)的實體類?TUser.java和 Mapper 類TUserMapper.java

          /**
          ?*?增刪改查例子
          ?*/

          @RestController
          public?class?CrudController?{

          ????@Autowired
          ????private?UserService?userService;


          ????/**
          ?????*?分頁查詢
          ?????*?http://localhost:8080/user/page?id=10
          ?????*?http://localhost:8080/user/page?pageIndex=1&pageSize=5
          ?????*
          ?????*?@param?param
          ?????*?@return
          ?????*/

          ????@GetMapping("/user/page")
          ????public?Result>?page(UserParam?param)?{
          ????????Query?query?=?param.toQuery();
          ????????PageInfo?pageInfo?=?userService.page(query);
          ????????return?Result.ok(pageInfo);
          ????}

          ????/**
          ?????*?新增記錄,這里為了方便演示用了GET方法,實際上應(yīng)該使用POST
          ?????*?http://localhost:8080/user/save?username=jim
          ?????*
          ?????*?@param?user
          ?????*?@return
          ?????*/

          ????@GetMapping("/user/save")
          ????public?Result?save(TUser?user)?{
          ????????userService.saveIgnoreNull(user);
          ????????//?返回添加后的主鍵值
          ????????return?Result.ok(user.getId());
          ????}

          ????/**
          ?????*?修改記錄,這里為了方便演示用了GET方法,實際上應(yīng)該使用POST
          ?????*?http://localhost:8080/user/update?id=10&username=jim
          ?????*
          ?????*?@param?user?表單數(shù)據(jù)
          ?????*?@return
          ?????*/

          ????@GetMapping("/user/update")
          ????public?Result?update(TUser?user)?{
          ????????userService.updateIgnoreNull(user);
          ????????return?Result.ok();
          ????}

          ????/**
          ?????*?刪除記錄,這里為了方便演示用了GET方法,實際上應(yīng)該使用DELETE
          ?????*?http://localhost:8080/user/delete?id=10
          ?????*
          ?????*?@param?id?主鍵id
          ?????*?@return
          ?????*/

          ????@GetMapping("/user/delete")
          ????public?Result?delete(Integer?id)?{
          ????????userService.deleteById(id);
          ????????return?Result.ok();
          ????}
          }
          • UserService.java
          //?實現(xiàn)通用接口
          @Service
          public?class?UserService?implements?IService<TUser/*實體類*/,?Integer/*主鍵類型*/>?{

          }
          • TUserMapper.java
          public?interface?TUserMapper?extends?CrudMapper<TUser/*實體類*/,?Integer/*主鍵類型*/>?{

          }

          service和mapper不用寫一行代碼就能實現(xiàn)各種數(shù)據(jù)庫操作,非常方便。

          Mapper方法列表

          Mapper方法列表
          Mapper方法列表
          Mapper方法列表
          Mapper方法列表
          Mapper方法列表

          Query查詢對象

          //查詢姓名為張三,并且年齡為22歲的用戶:
          Query?query?=?new?Query().eq("username","張三").eq("age",22);
          List?users?=?mapper.list(query);

          //查詢年齡為10,20,30的用戶:
          Query?query?=?new?Query().in("age",Arrays.asList(10,20,30));
          List?users?=?mapper.list(query);

          //查詢注冊日期大于2017-11-11的用戶:
          Date?regDate?=?...
          Query?query?=?new?Query().gt("reg_date",regDate);
          List?users?=?mapper.list(query);

          //查詢性別為男的,年齡大于等于20歲的用戶,按年齡降序:
          Query?query?=?new?Query().eq("gender",1).ge("age",20).orderby("age",Sort.DESC);
          List?users?=?mapper.list(query);

          //分頁查詢:
          Query?query?=?new?Query().eq("age",10).page(1,10);?//?第一頁,每頁10條數(shù)據(jù)
          List?users?=?mapper.list(query);

          //查詢總記錄數(shù):
          Query?query?=?new?Query().eq("age",10).page(1,10);?//?第一頁,每頁10條數(shù)據(jù)
          long?total?=?mapper.getCount(query);?//?該條件下總記錄數(shù)
          ?

          開源地址:https://gitee.com/durcframework/fastmybatis

          相關(guān)文檔:https://durcframework.gitee.io/fastmybatis/#/

          ?


          瀏覽 22
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  色操色| 午夜影院一区二区三区 | 色一区二区三区 | 国产麻豆网 | 精品九九九九九九九九九九九 |