Jpa动态多表if多条件联合查询,并对查询结果进行分页
public Page<Map<String, Object>> resourceList(TeachingInfo teachingInfo, Pageable pageable) {
...
//int offset = (pageable.getPageNumber() - 1) * pageable.getPageSize();
List<Map<String, String>> result = resourceInfoRepository.findResourceByCondition(chapterId, contentType, courseId, diffLevel, name, type, resourceType, pageable.getOffset(), pageable.getPageSize());
int total = resourceInfoRepository.findResourceCountByCondition(chapterId, contentType, courseId, diffLevel, name, type, resourceType);
//MDStringUtils.formatHumpNameForList(result)将List中map的key值命名方式由下划线转为驼峰命名
return new PageImpl<>(MDStringUtils.formatHumpNameForList(result), pageable, total);
}
//if里的不为null是(!='')
@Query(value = "select distinct ri.*, chapter_name, type " +
"from resource_info ri left join teaching_resource_info tri on tri.resource_id = ri.id left join teaching_info ti on ti.id = tri.teaching_id " +
"where 1=1 " +
"and IF (?1 != '', chapter_id = ?1, 1=1) " +
"and IF (?2 != '', content_type = ?2, 1=1) " +
"and IF (?3 != '', course_id = ?3, 1=1) " +
"and IF (?4 != '', diff_level = ?4, 1=1) " +
"and IF (?5 != '', ri.name like %?5%, 1=1) " +
"and IF (?6 != '', ti.type = ?6, 1=1) " +
"and IF (?7 != '', resource_type = ?7, 1=1) limit ?8,?9", nativeQuery = true)
List<Map<String, String>> findResourceByCondition(Long chapterId, String contentType, Long courseId, String diffLevel, String name, Integer type, String resourceType, long offset, int size);
@Query(value = "select count(distinct ri.id) " +
"from resource_info ri left join teaching_resource_info tri on tri.resource_id = ri.id left join teaching_info ti on ti.id = tri.teaching_id " +
"where 1=1 " +
"and IF (?1 != '', chapter_id = ?1, 1=1) " +
"and IF (?2 != '', content_type = ?2, 1=1) " +
"and IF (?3 != '', course_id = ?3, 1=1) " +
"and IF (?4 != '', diff_level = ?4, 1=1) " +
"and IF (?5 != '', ri.name like %?5%, 1=1) " +
"and IF (?6 != '', ti.type = ?6, 1=1) " +
"and IF (?7 != '', resource_type = ?7, 1=1) ", nativeQuery = true)
int findResourceCountByCondition(Long chapterId, String contentType, Long courseId, String diffLevel, String name, Integer type, String resourceType);
/**
* 将List中map的key值命名方式格式化为驼峰
*
* @param
* @return
*/
public static List<Map<String, Object>> formatHumpNameForList(List<Map<String, String>> list) {
List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
for (Map<String, String> o : list) {
newList.add(formatHumpName(o));
}
return newList;
}
public static Map<String, Object> formatHumpName(Map<String, String> map) {
Map<String, Object> newMap = new HashMap<String, Object>();
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
String newKey = toFormatCol(key);
newMap.put(newKey, entry.getValue());
}
return newMap;
}
public static String toFormatCol(String colName) {
StringBuilder sb = new StringBuilder();
String[] str = colName.toLowerCase().split("_");
int i = 0;
for (String s : str) {
if (s.length() == 1) {
s = s.toUpperCase();
}
i++;
if (i == 1) {
sb.append(s);
continue;
}
if (s.length() > 0) {
sb.append(s.substring(0, 1).toUpperCase());
sb.append(s.substring(1));
}
}
return sb.toString();
}
Jpa动态多表if多条件联合查询,并对查询结果进行分页的更多相关文章
- JPA的多表复杂查询
转 JPA的多表复杂查询:详细篇 原文链接: https://mp.weixin.qq.com/s/7J6ANppuiZJccIVN-h0T3Q 2017-11-10 从小爱喝AD钙 最近工作中由于 ...
- Hibernate JPA 动态criteria语句针对null查询条件的特殊处理
最近原Hibernate项目需要添加一个条件,结构有点类似下面的格式,学生和房间是多对一的关系,现在要查询所有没有房间的学生. Class Student{ @ManyToOne Room room; ...
- Spring Data JPA 动态拼接条件的通用设计模式
import java.sql.Timestamp;import java.util.ArrayList;import java.util.List;import javax.persistence. ...
- SpringBoot+SpringDataJPA如何实现自定义查询[多表,多条件,分页,自定义sql封装]
举个例子:我们要在已经搭建好了的JPA环境下实现联合多表,多条件,多排序条件,分页查询一个表格数据,下面的表格 返回类MyJSON: public class MyJSON { private Str ...
- jpa动态创建EntityManagerFactory 态设置数据库连接 EntityManager;
//jpa动态创建EntityManagerFactory 态设置数据库连接EntityManager;createEntityManagerFactory(String persistenceUni ...
- 动态增加表单元素并获取元素的text和value提交
以上是效果图 需求是这样的: 专家设置好条件,然后设备检测到达到相应的条件之后,设备发出提醒给用户. 这就需要专家设置好能看懂的条件之后,然后把给专家看的,正常人能看懂的条件和发送的设备的,设备能够识 ...
- mysql中关于关联索引的问题——对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引?
情况描述:在MySQL的user表中,对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引? 根据查询字段的位置不同来决定,如查询a, a,b a,b, ...
- spring jpa 动态查询(Specification)
//dao层 继承 扩展仓库接口JpaSpecificationExecutor (JPA 2引入了一个标准的API)public interface CreditsEventDao extends ...
- 学习动态性能表(1)--v$sysstat
由动态性能表学到的 第一篇--v$sysstat 2007.5.23 按照OracleDocument中的描述,v$sysstat存储自数据库实例运行那刻起就开始累计全实例(instance-wid ...
随机推荐
- zookeeper,通过python连接找出其对应的hive
#通过zookpeer找到对应的hive from kazoo.client import KazooClient zkClient = KazooClient(hosts="ip1:218 ...
- mysql Got a packet bigger than 'max_allowed_packet' bytes
背景 数据库备份执行SQL文件时,执行到图片表插入图片数据时错误: 错误提示:Got a packet bigger than 'max_allowed_packet' bytes 原因分析及解决 m ...
- vue项目中axios的封装和使用
一.axios的功能特点 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 http请求 支持 Promise API 拦截请求和响应 转换请求和响应数据 支持多种请求 ...
- 改计算机名导致 Oracle因目标主机或对象不存在
手贱修改了计算机名, 结果导致登陆oracle数据库报如下错误,一查资料,说是修改了计算机名导致的,需要进到oracle安装目录: \oracle\product\10.2.0\db_1\NETWOR ...
- HihoCoder - 1664 (单调队列)
题目:https://vjudge.net/contest/319166#problem/B 题意: 一个01间隔矩阵,求一个方阵的最大边长,这个方阵的要求是里面01分隔,不能有01相邻 思路:同 ...
- css如何让<a>标签,根据输入的内容长度调整宽度,宽度自适应,那位大仙帮帮忙
css如何让<a>标签,根据输入的内容长度调整宽度,宽度自适应,那位大仙帮帮忙 5 样式 .ceshi{float:left; margin-left:13px; width:180px; ...
- printf ("%*.*s")
小数点.后“*”表示输出位数,具体的数据来自参数表printf格式字符串中,与宽度控制和精度控制有关的常量都可以换成变量,方法就是使用一个“*”代替那个常量,然后在后面提供变量给“*”. 同样,小数点 ...
- 用Python实现一个大数据搜索引擎
用Python实现一个大数据搜索引擎 搜索是大数据领域里常见的需求.Splunk和ELK分别是该领域在非开源和开源领域里的领导者.本文利用很少的Python代码实现了一个基本的数据搜索功能,试图让大家 ...
- Java + selenium window()接口方法介绍
在浏览器启动的代码中,有一段关于window接口的调用,这篇文章就是来解释介绍这个接口的.代码如下 driver.manage().window().maxmize(); window接口主要是用来控 ...
- Codeforces #576 Rectangle Painting 1 | div1D | div2F | DP | Rustlang
原题链接 大意 n*n正方形 有黑有白 每次可以选择一个 矩形把它全变成白色,代价是max(长,宽) 求吧 整个正方形 全变白 的最小代价 数据范围 n <= 50 题解 首先如果 我们刷了两个 ...