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

MyBatis-Ext快速入門實戰(zhàn)

 更新時間:2021年10月26日 11:54:29   作者:Hydra  
MyBatis-Ext是MyBatis的增強擴展,和我們平常用的Mybatis-plus非常類似,本文主要介紹了MyBatis-Ext快速入門實戰(zhàn),感興趣的可以了解一下

最近在工作中,接觸到了一個MyBatis擴展工具包MyBatis-Ext,可以說很大程度上減輕了使用mybatis時的工作量,本文就和大家來分享一下這個輕量的擴展工具。

MyBatis-Ext是MyBatis的增強擴展,和我們平常用的Mybatis-plus非常類似,簡化了MyBatis對單表增刪改查的操作,提供通用的增刪改查,支持函數(shù)式編程,支持分頁查詢,支持用戶自定義通用方法,并且能夠防止SQL注入。集成起來也非常簡單,對MyBatis只做增強不做修改。

以spring-boot項目為例,集成非常簡單。pom導入核心依賴:

<dependency>
    <groupId>tech.wetech.mybatis</groupId>
    <artifactId>mybatis-ext-core</artifactId>
    <version>1.5.2</version>
</dependency>
<dependency>
    <groupId>tech.wetech.mybatis</groupId>
    <artifactId>mybatis-ext-spring-boot-starter</artifactId>
    <version>1.5.2</version>
</dependency>    

需要注意的是,引入mybatis-ext-spring-boot-starter后無需再引入mybatis-spring-boot-starter。
和以往一樣,在application.yml配置一下數(shù)據源:

spring:
  datasource:
    username: dater
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 8
      min-idle: 1
      max-active: 20
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.mybatis.ext.test.mybatisexttest.entity
spring:
  datasource:
    username: dater
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 8
      min-idle: 1
      max-active: 20
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.mybatis.ext.test.mybatisexttest.entity

創(chuàng)建一個映射的實體類:

@Data
@Table(name = "user")
public class User {
    @Id
    String identifycard;
    @Column(name="name")
    String name;
    String money;
    String card;
    String phone;
    String rate;
}

mybatis-ext使用了Jpa的注解,目前實現(xiàn)了@Table、@Id、@Column、@Transient、@Version。其中@Table、@Id是必須添加的注解,其他非必須添加。使用@Table指定數(shù)據表名,@Id指定數(shù)據表主鍵。

查詢的Mapper接口繼承BaseMapper接口,泛型中填寫實體類:

public interface UserMapper extends BaseMapper<User> {
}

我們來看一下能夠直接調用的方法,為在BaseMapper中內置了很多通用方法,可以直接調用,非常簡便:

int deleteByPrimaryKey(PK id);
<S extends T> int insert(S record);
<S extends T> int insertAll(Iterable<S> record);
<S extends T> int insertSelective(S record);
<S extends T> S selectByPrimaryKey(PK id);
<S extends T> Optional<S> selectByPrimaryKeyWithOptional(ID id);
<S extends T> int updateByPrimaryKey(S record);
<S extends T> int updateByPrimaryKeySelective(S record);
<S extends T> List<S> selectAll();
<S extends T> List<S> selectList(S record);
<S extends T> S selectOne(S record);
<S extends T> S selectOneWithOptional(S record);
boolean existsByPrimaryKey(PK id);
<S extends T> int count(S record);
<S extends T> List<S> selectByExample(Example<S, Object> example);
<S extends T> int countByExample(Example<S, Object> example);
<S extends T> int deleteByExample(Example<S, Object> example);
<S extends T> int updateByExample(@Param("record") S record, @Param("example") Example<S, Object> example);
<S extends T> int updateByExampleSelective(@Param("record") S record, @Param("example") Example<S, Object> example);

來進行一下接口調用的測試,先試一下selectAll方法:

@GetMapping("getUser")
public void getUser(){
    List<User> users = userMapper.selectAll();
    for (User user : users) {
        System.out.println(user.getName()+" "+user.getIdentifycard());
    }
}

測試結果:

這樣,通過調用內置方法就實現(xiàn)了不寫sql語句直接進行查詢。同樣,如果想根據主鍵進行查詢也很簡單,直接調用selectByPrimaryKey方法:

@PostMapping("getUserById")
public void getUserByIdentifycard(@RequestBody User user){
    User retUser = userMapper.selectByPrimaryKey(user);
    System.out.println(retUser.toString());
}

查詢結果:

另外,還可以使用Optional包裹查詢,修改一下上面主鍵查詢的方法:

@PostMapping("getUserById")
public void getUserByIdentifycard(@RequestBody User user){
    User retUser = userMapper.selectByPrimaryKeyWithOptional(user)
            .orElseThrow(()->new RuntimeException("未查到數(shù)據"));
    System.out.println(retUser.toString());
}

這樣,在傳入一個不存在的主鍵時,就會直接拋出自定義的異常:

還有其他很多簡單的查詢,大家可以根據上面列出api自行測試一下。此外,還可以使用Criteria,使用邏輯組合,進行函數(shù)式查詢:

@GetMapping("criteriaTest")
public void testCreateCriteria(){
    List<User> list = userMapper.createCriteria()
            .andEqualTo(User::getName, "Trunks")
            .andBetween(User::getMoney, 100, 300)
            .andNotLike(User::getRate, "6")
            .orIn(User::getCard, Arrays.asList("10"))
            .selectList();

    list.forEach(user -> {
        System.out.println(user.toString());
    });
}

查詢結果:

也可以使用Example進行查詢:

@GetMapping("exampleTest")
public void testExample(){
    Example<User> example=Example.of(User.class);
    example.createCriteria()
            .andEqualTo(User::getName, "Trunks")
            .andBetween(User::getMoney, 100, 300)
            .andNotLike(User::getRate, "6")
            .orIn(User::getCard, Arrays.asList("10"));

    example.setDistinct(true);
    List<User> list = userMapper.selectByExample(example);
    list.forEach(user -> {
        System.out.println(user.toString());
    });
}

結果與使用Criteria結果相同。另外,還可以將多個條件組合使用:

GetMapping("testExampleWithSub")
public void selectByExampleWithSub(){
    try (SqlSession session = sqlSessionFactory.openSession()) {
        UserMapper userMapper1 = session.getMapper(UserMapper.class);
        Example<User> example=Example.of(User.class);
        example.and()
                .andEqualTo(User::getName, "Trunks");
        example.and()
                .andEqualTo(User::getCard,"10");
        example.and()
                .andLessThanOrEqualTo(User::getRate,300);

        Criteria<User> criteria=new Criteria<>();
        criteria.andIsNotNull(User::getPhone);
        example.and(criteria);
        List<User> list = userMapper1.selectByExample(example);

        list.forEach(user -> {
            System.out.println(user.toString());
        });
    }
}

結果:

除了上面介紹的這些功能與基礎的sql增刪改查外,MyBatis-Ext還實現(xiàn)了很多其他功能,例如排序和分頁,并且支持自定義通用接口方法等等,大家可以在使用中通過實踐繼續(xù)探索一下。

最后

到此這篇關于MyBatis-Ext快速入門實戰(zhàn)的文章就介紹到這了,更多相關MyBatis-Ext 入門內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用maven整合Spring+SpringMVC+Mybatis框架詳細步驟(圖文)

    使用maven整合Spring+SpringMVC+Mybatis框架詳細步驟(圖文)

    這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • SpringMVC+Mysql實例詳解(附demo)

    SpringMVC+Mysql實例詳解(附demo)

    本篇文章主要介紹了SpringMVC+Mysql實例詳解(附demo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。
    2016-12-12
  • 超細致講解Spring框架 JdbcTemplate的使用

    超細致講解Spring框架 JdbcTemplate的使用

    在之前的Javaweb學習中,學習了手動封裝JdbcTemplate,其好處是通過(sql語句+參數(shù))模板化了編程。而真正的JdbcTemplate類,是Spring框架為我們寫好的。它是 Spring 框架中提供的一個對象,是對原始 Jdbc API 對象的簡單封裝。
    2021-09-09
  • Java實現(xiàn)排列組合算法的兩種方案

    Java實現(xiàn)排列組合算法的兩種方案

    Java排列組合算法是一種用于生成所有可能的排列和組合的算法,在Java中,可以使用遞歸或迭代的方式實現(xiàn)排列組合算法,本文給大家介紹了Java實現(xiàn)排列組合算法的兩種方案,需要的朋友可以參考下
    2024-04-04
  • Java哈希表的概念及實現(xiàn)完整代碼

    Java哈希表的概念及實現(xiàn)完整代碼

    這篇文章主要介紹了Java哈希表的概念及實現(xiàn)的相關資料,哈希表是一種高效查找數(shù)據的結構,通過哈希函數(shù)將關鍵字映射到數(shù)組的索引位置,當發(fā)生沖突時,可以通過閉散列或開散列(鏈地址法)來解決,需要的朋友可以參考下
    2024-11-11
  • 微信支付java版本之獲取Access_token

    微信支付java版本之獲取Access_token

    這篇文章主要介紹了微信支付java版本之獲取Access_token,java如何獲取Access_token,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Java JSON轉成List結構數(shù)據

    Java JSON轉成List結構數(shù)據

    這篇文章主要介紹了Java JSON轉成List結構數(shù)據,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • JDK8 new ReentrantLock((true)加鎖流程

    JDK8 new ReentrantLock((true)加鎖流程

    這篇文章主要介紹了java面試中常遇到的問題JDK8 new ReentrantLock((true)加鎖流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Java生成訂單號或唯一id的高并發(fā)方案(4種方法)

    Java生成訂單號或唯一id的高并發(fā)方案(4種方法)

    本文主要介紹了Java生成訂單號或唯一id的高并發(fā)方案,包括4種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-01-01
  • java的JIT 工作原理簡單介紹

    java的JIT 工作原理簡單介紹

    這篇文章主要介紹了java的JIT 工作原理簡單介紹的相關資料,需要的朋友可以參考下
    2017-03-03

最新評論

神农架林区| 大埔区| 仁化县| 尖扎县| 志丹县| 铜梁县| 宣武区| 四平市| 新竹市| 红河县| 勐海县| 子洲县| 临猗县| 嘉荫县| 哈尔滨市| 浪卡子县| 乌拉特后旗| 凤阳县| 桂平市| 唐河县| 兖州市| 瑞安市| 清徐县| 定襄县| 绵竹市| 象州县| 牟定县| 湘西| 离岛区| 湘阴县| 鄱阳县| 霍林郭勒市| 黑龙江省| 朝阳市| 资源县| 景东| 棋牌| 阿拉善左旗| 河东区| 潜山县| 石阡县|