1. public Page<Map<String, Object>> resourceList(TeachingInfo teachingInfo, Pageable pageable) {
  2. ...
  3. //int offset = (pageable.getPageNumber() - 1) * pageable.getPageSize();
  4. List<Map<String, String>> result = resourceInfoRepository.findResourceByCondition(chapterId, contentType, courseId, diffLevel, name, type, resourceType, pageable.getOffset(), pageable.getPageSize());
  5. int total = resourceInfoRepository.findResourceCountByCondition(chapterId, contentType, courseId, diffLevel, name, type, resourceType);
  6. //MDStringUtils.formatHumpNameForList(result)将List中map的key值命名方式由下划线转为驼峰命名
  7. return new PageImpl<>(MDStringUtils.formatHumpNameForList(result), pageable, total);
  8. }
  9. //if里的不为null是(!='')
  10. @Query(value = "select distinct ri.*, chapter_name, type " +
  11. "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 " +
  12. "where 1=1 " +
  13. "and IF (?1 != '', chapter_id = ?1, 1=1) " +
  14. "and IF (?2 != '', content_type = ?2, 1=1) " +
  15. "and IF (?3 != '', course_id = ?3, 1=1) " +
  16. "and IF (?4 != '', diff_level = ?4, 1=1) " +
  17. "and IF (?5 != '', ri.name like %?5%, 1=1) " +
  18. "and IF (?6 != '', ti.type = ?6, 1=1) " +
  19. "and IF (?7 != '', resource_type = ?7, 1=1) limit ?8,?9", nativeQuery = true)
  20. List<Map<String, String>> findResourceByCondition(Long chapterId, String contentType, Long courseId, String diffLevel, String name, Integer type, String resourceType, long offset, int size);
  21. @Query(value = "select count(distinct ri.id) " +
  22. "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 " +
  23. "where 1=1 " +
  24. "and IF (?1 != '', chapter_id = ?1, 1=1) " +
  25. "and IF (?2 != '', content_type = ?2, 1=1) " +
  26. "and IF (?3 != '', course_id = ?3, 1=1) " +
  27. "and IF (?4 != '', diff_level = ?4, 1=1) " +
  28. "and IF (?5 != '', ri.name like %?5%, 1=1) " +
  29. "and IF (?6 != '', ti.type = ?6, 1=1) " +
  30. "and IF (?7 != '', resource_type = ?7, 1=1) ", nativeQuery = true)
  31. int findResourceCountByCondition(Long chapterId, String contentType, Long courseId, String diffLevel, String name, Integer type, String resourceType);
  32. /**
  33. * 将List中map的key值命名方式格式化为驼峰
  34. *
  35. * @param
  36. * @return
  37. */
  38. public static List<Map<String, Object>> formatHumpNameForList(List<Map<String, String>> list) {
  39. List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
  40. for (Map<String, String> o : list) {
  41. newList.add(formatHumpName(o));
  42. }
  43. return newList;
  44. }
  45. public static Map<String, Object> formatHumpName(Map<String, String> map) {
  46. Map<String, Object> newMap = new HashMap<String, Object>();
  47. Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  48. while (it.hasNext()) {
  49. Map.Entry<String, String> entry = it.next();
  50. String key = entry.getKey();
  51. String newKey = toFormatCol(key);
  52. newMap.put(newKey, entry.getValue());
  53. }
  54. return newMap;
  55. }
  56. public static String toFormatCol(String colName) {
  57. StringBuilder sb = new StringBuilder();
  58. String[] str = colName.toLowerCase().split("_");
  59. int i = 0;
  60. for (String s : str) {
  61. if (s.length() == 1) {
  62. s = s.toUpperCase();
  63. }
  64. i++;
  65. if (i == 1) {
  66. sb.append(s);
  67. continue;
  68. }
  69. if (s.length() > 0) {
  70. sb.append(s.substring(0, 1).toUpperCase());
  71. sb.append(s.substring(1));
  72. }
  73. }
  74. return sb.toString();
  75. }

Jpa动态多表if多条件联合查询,并对查询结果进行分页的更多相关文章

  1. JPA的多表复杂查询

    转 JPA的多表复杂查询:详细篇 原文链接: https://mp.weixin.qq.com/s/7J6ANppuiZJccIVN-h0T3Q 2017-11-10 从小爱喝AD钙  最近工作中由于 ...

  2. Hibernate JPA 动态criteria语句针对null查询条件的特殊处理

    最近原Hibernate项目需要添加一个条件,结构有点类似下面的格式,学生和房间是多对一的关系,现在要查询所有没有房间的学生. Class Student{ @ManyToOne Room room; ...

  3. Spring Data JPA 动态拼接条件的通用设计模式

    import java.sql.Timestamp;import java.util.ArrayList;import java.util.List;import javax.persistence. ...

  4. SpringBoot+SpringDataJPA如何实现自定义查询[多表,多条件,分页,自定义sql封装]

    举个例子:我们要在已经搭建好了的JPA环境下实现联合多表,多条件,多排序条件,分页查询一个表格数据,下面的表格 返回类MyJSON: public class MyJSON { private Str ...

  5. jpa动态创建EntityManagerFactory 态设置数据库连接 EntityManager;

    //jpa动态创建EntityManagerFactory 态设置数据库连接EntityManager;createEntityManagerFactory(String persistenceUni ...

  6. 动态增加表单元素并获取元素的text和value提交

    以上是效果图 需求是这样的: 专家设置好条件,然后设备检测到达到相应的条件之后,设备发出提醒给用户. 这就需要专家设置好能看懂的条件之后,然后把给专家看的,正常人能看懂的条件和发送的设备的,设备能够识 ...

  7. mysql中关于关联索引的问题——对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引?

    情况描述:在MySQL的user表中,对a,b,c三个字段建立联合索引,那么查询时使用其中的2个作为查询条件,是否还会走索引? 根据查询字段的位置不同来决定,如查询a,     a,b    a,b, ...

  8. spring jpa 动态查询(Specification)

    //dao层 继承 扩展仓库接口JpaSpecificationExecutor (JPA 2引入了一个标准的API)public interface CreditsEventDao extends ...

  9. 学习动态性能表(1)--v$sysstat

    由动态性能表学到的 第一篇--v$sysstat  2007.5.23 按照OracleDocument中的描述,v$sysstat存储自数据库实例运行那刻起就开始累计全实例(instance-wid ...

随机推荐

  1. 微信小程序中的自定义组件 以及 相关的坑

    Step1 我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建一个 components 文件夹,用于存放我们以后开发中的所用组件,今天我们的目的是实现一个 首页 组件, ...

  2. PHP计算经纬度在百度多边形区域内

    最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...

  3. 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 ...

  4. STM32 TIM3 PWM输出 4路

    一.设置TIM3的GPIO为推挽输出 void TIM3_IOConfig(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClo ...

  5. Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能

    一.部署Zabbix监控平台 目标: 本案例要求部署一台Zabbix监控服务器,一台被监控主机,为进一步执行具体的监控任务做准备: 在监控服务器上安装LAMP环境    修改PHP配置文件,满足Zab ...

  6. iOS Android中 h5键盘遮挡输入框的问题和解决方案

    问题发现:在 Android 部分机型 和 iOS部分系统下 键盘会出现遮挡输入框的情况(壳内).问题解决: Android 经过测试,Android 的6.0版本以上均会出现改问题,归根到底是之前的 ...

  7. nboot,eboot和uboot

    nboot,eboot和uboot三者均为bootloader. ----nboot是samsung系列cpu为了能将前4KB程序复制到SRAM中运行,而在wince写的.nboot很小(4k左右), ...

  8. mongodb在linux 上要注意的一些东西

    没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...

  9. 【SpringBoot】SpringBoot的基础,全面理解bean的生命周期

    前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道sprin ...

  10. exception 打印出异常栈踪迹

    Java异常抛出使用e.printStackTrace(),打印出抛出的异常栈踪迹, 如果你在catch中继续抛出这个异常,那么e.printStackTrace()也能跟踪到抛出异常的地方, 使用t ...