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 ...
随机推荐
- 微信小程序中的自定义组件 以及 相关的坑
Step1 我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建一个 components 文件夹,用于存放我们以后开发中的所用组件,今天我们的目的是实现一个 首页 组件, ...
- PHP计算经纬度在百度多边形区域内
最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...
- Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- STM32 TIM3 PWM输出 4路
一.设置TIM3的GPIO为推挽输出 void TIM3_IOConfig(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClo ...
- Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能
一.部署Zabbix监控平台 目标: 本案例要求部署一台Zabbix监控服务器,一台被监控主机,为进一步执行具体的监控任务做准备: 在监控服务器上安装LAMP环境 修改PHP配置文件,满足Zab ...
- iOS Android中 h5键盘遮挡输入框的问题和解决方案
问题发现:在 Android 部分机型 和 iOS部分系统下 键盘会出现遮挡输入框的情况(壳内).问题解决: Android 经过测试,Android 的6.0版本以上均会出现改问题,归根到底是之前的 ...
- nboot,eboot和uboot
nboot,eboot和uboot三者均为bootloader. ----nboot是samsung系列cpu为了能将前4KB程序复制到SRAM中运行,而在wince写的.nboot很小(4k左右), ...
- mongodb在linux 上要注意的一些东西
没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...
- 【SpringBoot】SpringBoot的基础,全面理解bean的生命周期
前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道sprin ...
- exception 打印出异常栈踪迹
Java异常抛出使用e.printStackTrace(),打印出抛出的异常栈踪迹, 如果你在catch中继续抛出这个异常,那么e.printStackTrace()也能跟踪到抛出异常的地方, 使用t ...