首页 > 其他分享 >SpringBoot Jpa多条件查询

SpringBoot Jpa多条件查询

时间:2024-05-01 17:22:29浏览次数:21  
标签:SpringBoot Jpa ExampleMatcher 查询 mt example MyEnitty Example

  • Repository
MyRepository extends JpaRepository<MyEntity, Integer>

精确查询:

Example 包装Entity

Pageable pageable = PageRequest.of(current - 1, pageSize);
//myEntity 实体类参数
Example example = Example.of(myEntity);
Page<MyEntity> page = myRepository.findAll(example, pageable);

 

模糊查询

@Test
    void testQuery() {
        MyEnitty MyEnitty = new MyEnitty();
        MyEnitty.setUserName("ab");
        MyEnitty.setAddress("ef");
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                //前綴精确匹配ab%
                .withMatcher("userName", ExampleMatcher.GenericPropertyMatcher::startsWith)
                //前后缀都模糊匹配%ef%
                .withMatcher("address", ExampleMatcher.GenericPropertyMatcher::contains);
        Example<MyEnitty> example = Example.of(MyEnitty, exampleMatcher);
        List<MyEnitty> result = MyEnittyRepository.findAll(example);
        for (MyEnitty mt : result) {
            System.out.println(mt.getUserName + ":" + mt.getAddress);
        }
    }

 



标签:SpringBoot,Jpa,ExampleMatcher,查询,mt,example,MyEnitty,Example
From: https://www.cnblogs.com/coderdxj/p/18169482

相关文章

  • SpringBoot 打包所有依赖
    SpringBoot项目打包的时候可以通过插件spring-boot-maven-plugin来repackage项目,使得打的包中包含所有依赖,可以直接运行。例如:<plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId&g......
  • SpringBoot 循环引用解决办法
    Spring原生是允许并解决了单层循环引用的,但从SpringBoot2.6.0开始,默认禁止Bean之间的循环引用,如果存在循环引用就会启动失败报错。此时要解决循环引用问题有两个办法:设置Spring允许循环引用(推荐)。spring:main:allow-circular-references:true在循环引用......
  • SpringBoot camunda常用代码
    图例: 1:默认排他网关,表达式Type:expression:${number%200==0}2:servicetask(系统自动执行用的最多):常用Delegateexpression${testGateWay}举例:@Component("testGateWay")publicclassTestGateWayimplementsJavaDelegate{@Overridepublicvoidexecute......
  • SpringBoot2.x整合Redis Sentinel
    redissentinel搭建之后,在spring-boot项目中集成。配置在pom.xml文件中添加如下依赖配置(这里spring-boot版本2.2.5),这个版本中,默认使用lettuce作为redis连接池。<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis<......
  • [WUSTCTF2020]颜值成绩查询
    [WUSTCTF2020]颜值成绩查询打开环境是一个成绩查询的页面1.手工注入输入1发现有admin的账号和得分输入1'会提示学号不存在1/**/or/**/1=1#过滤了空格1/**/order/**/by/**/3#存在1/**/order/**/by/**/4#不存在由此得知有3个字段1/**/union/**/select/**/1,2,......
  • SQL查询优化
    当多表连接数据量只有几十万时还没有优化的必要,当数据量达到几百万或几千万时就有必要了,常见于日报,实时数据。最重要的优化是走索引。可以通过查询计划查看SQL语句中开销大的查询部分是不是全表扫描。但有时候根据业务逻辑写的SQL刚好避开了索引列。这时候需要走一些弯路。可以......
  • springboot动态查询
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.lian.mapper.......
  • 记录springboot项目多module(java/scala)打包代码
    java项目不太熟,之前一直使用的idea中的打包方式:BuildArtifacts...,现在使用maven打包,一直报下面的common等依赖包找不到,花了很多时间,在此记录下解决过程。解决办法是先将依赖包deploy到远程仓库,然后在打包的时候先install,再package,就可以打包成功。但还有一个问题没解决:由于我的......
  • springboot~AutoConfigureAfter如何控制Bean的注入顺序
    这个文章主要介绍一下@AutoConfigureAfter在spring框架中的作用,在使用过程中,很多开发人员在使用它的时候都出现了问题,问题比较多的就是它们的注册顺序总不是我们预期的,下面介绍一下正常的使用方法。@AutoConfigureAfter用在配置类上面,即需要在@Configuration修饰的类上,而不是@Co......
  • Mybatis Plus 多表联查(包含分页关联查询,图文讲解)
    本章将学习如何通过MybatisPlus实现多表关联查询,以及分页关联查询。表结构本文以查询用户所下订单,来演示MybatisPlus的关联查询,数据库表除了前面小节中已经定义好的用户表外,再额外创建一张订单表,然后插入一些测试数据,执行脚本如下:DROPTABLEIFEXISTSuser;CREATETA......