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

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

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

最近在工作中,接觸到了一個(gè)MyBatis擴(kuò)展工具包MyBatis-Ext,可以說(shuō)很大程度上減輕了使用mybatis時(shí)的工作量,本文就和大家來(lái)分享一下這個(gè)輕量的擴(kuò)展工具。

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

以spring-boot項(xiàng)目為例,集成非常簡(jiǎn)單。pom導(dǎo)入核心依賴:

<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后無(wú)需再引入mybatis-spring-boot-starter。
和以往一樣,在application.yml配置一下數(shù)據(jù)源:

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)建一個(gè)映射的實(shí)體類:

@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的注解,目前實(shí)現(xiàn)了@Table、@Id、@Column、@Transient、@Version。其中@Table、@Id是必須添加的注解,其他非必須添加。使用@Table指定數(shù)據(jù)表名,@Id指定數(shù)據(jù)表主鍵。

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

public interface UserMapper extends BaseMapper<User> {
}

我們來(lái)看一下能夠直接調(diào)用的方法,為在BaseMapper中內(nèi)置了很多通用方法,可以直接調(diào)用,非常簡(jiǎn)便:

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

來(lái)進(jìn)行一下接口調(diào)用的測(cè)試,先試一下selectAll方法:

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

測(cè)試結(jié)果:

這樣,通過(guò)調(diào)用內(nèi)置方法就實(shí)現(xiàn)了不寫sql語(yǔ)句直接進(jìn)行查詢。同樣,如果想根據(jù)主鍵進(jìn)行查詢也很簡(jiǎn)單,直接調(diào)用selectByPrimaryKey方法:

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

查詢結(jié)果:

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

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

這樣,在傳入一個(gè)不存在的主鍵時(shí),就會(huì)直接拋出自定義的異常:

還有其他很多簡(jiǎn)單的查詢,大家可以根據(jù)上面列出api自行測(cè)試一下。此外,還可以使用Criteria,使用邏輯組合,進(jìn)行函數(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());
    });
}

查詢結(jié)果:

也可以使用Example進(jìn)行查詢:

@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());
    });
}

結(jié)果與使用Criteria結(jié)果相同。另外,還可以將多個(gè)條件組合使用:

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());
        });
    }
}

結(jié)果:

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

最后

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

相關(guān)文章

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

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

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

    SpringMVC+Mysql實(shí)例詳解(附demo)

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

    超細(xì)致講解Spring框架 JdbcTemplate的使用

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

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

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

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

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

    微信支付java版本之獲取Access_token

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

    Java JSON轉(zhuǎn)成List結(jié)構(gòu)數(shù)據(jù)

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

    JDK8 new ReentrantLock((true)加鎖流程

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

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

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

    java的JIT 工作原理簡(jiǎn)單介紹

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

最新評(píng)論

永济市| 杂多县| 仁怀市| 嫩江县| 三门峡市| 宜君县| 林州市| 新田县| 台江县| 甘孜县| 都江堰市| 孝义市| 乡城县| 长垣县| 伊金霍洛旗| 福泉市| 阳西县| 洞口县| 南安市| 乾安县| 平湖市| 景宁| 镇巴县| 永川市| 滨州市| 太仆寺旗| 任丘市| 庆城县| 唐河县| 响水县| 宜兰县| 佛山市| 万全县| 建始县| 和林格尔县| 山西省| 天祝| 浦县| 遵化市| 洪江市| 东莞市|