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. 人生苦短_我用Python_类与对象的概念_006

    Python类与对象的概念类和对象--->万事万物都对象物以类聚.人以群分 --->?划分标准性别分 男女 中性成绩分 优秀 良好 不及格 类->根据类的属性来划分类的实例-> ...

  2. 输出匹配项:grep

    命令格式: grep pattern [file...] When grep encounters a "pattern" in the file, it prints out t ...

  3. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  4. vue项目中echarts使用渐变效果报错echarts is not defined

    解决办法:在当前单组件中在引用一次

  5. http 换成 https

    UPDATE SYS_MENU M SET M.href = ( SELECT CASE WHEN substr(N.href, 0, 5) = 'http:' THEN 'https:'||subs ...

  6. C++ string.replace的使用

    //下面是一个检查一个字符串中是否有'.'的函数,该函数将找到的'.'转化为'_'. inline void checkName(string& name) { std::; while (s ...

  7. SSM - 全局跨域处理

    这几天在开发中编写项目时需要前后端分离,刚好涉及到跨域这个问题,很早之前做项目时也用过,也是在网上找的列子,来源已经无处可寻了,若侵必删! 跨域问题一般出现两者服务器不同或者不同的端口上访问资源时会存 ...

  8. JS知识—面试准备(一)

    1.JS内置类型 分为基本数据类型和Object.基本数据类型有:null,undefined,string,boolean,number,symbol. console.log(typeof nul ...

  9. 经典JS 判断上传文件大小和JS即时同步电脑时间

    我也是新手,还是一个JS笨,有一些网站要实现的功能要自己写么? 答案是不会,去问同事大佬吧,闲简单.就在晚上看了一些其他大佬们写的JS效果, 代码很少.占用网站CPU也小的多.可以一用, 废话少扯.代 ...

  10. DCloud-MUI-JS:相关摘录

    ylbtech-DCloud-MUI-JS:相关摘录 1.返回顶部 1.JS initFun: function() { var oldBack = mui.back; mui.back = func ...