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多条件联合查询,并对查询结果进行分页的更多相关文章

  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. find命令进阶(三):xargs

    The xargs command performs an interesting function. It accepts input from standard input and convert ...

  2. php基于SQLite实现的分页功能示例

    php基于SQLite实现的分页功能. 这里操作数据库文件使用的是前面文章<PHP基于PDO实现的SQLite操作类>中的SQLite数据库操作类. 代码: <?php class ...

  3. window 安装iis和使用

    一.IIS安装(服务名World Wide Web Publishing Service) 实验环境:win2008R2虚拟机1.首先打开虚拟机,然后选中管理工具->服务器管理器. 2.选中角色 ...

  4. 华为交换机telnet配置

    1.在路由器上和交换机相连的借口上配置一个IP地址:比如192.168.1.1 24 2.在交换机上配置如下:<switch>system-view[switch]vlan 10[swit ...

  5. css3D动画

    css3D动画 前言 最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下. 在阅读这篇博客之前,请先自行了解一下css 3D的属性,例如:transform-style,transf ...

  6. get the deadlock information from sql server

    https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...

  7. __int128使用

    输入输出模板: __int128无法使用cin和cout进行输入输出,所以只能自己写一个输入输出的模板: #include <bits/stdc++.h> using namespace ...

  8. 转载:AWR介绍使用

    转载自 http://www.cnblogs.com/lanzi/archive/2011/03/07/1975096.html 自动工作负载库(Automatic Workload Reposito ...

  9. ACM中java的使用 (转)

    ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...

  10. Java并发:搞定线程池(中)

    向线程池提交任务 1.1 execute()     用于提交不需要返回值的任务,所以无法判断任务是否被线程池执行成功.输入的是一个Runnable实例. public void execute(Ru ...