jpa多表联查动态_Jpa动态多表多条件联合查询,并对查询结果进行分页

public Page> resourceList(TeachingInfo teachingInfo, Pageable pageable) {

...

//int offset = (pageable.getPageNumber() - 1) * pageable.getPageSize();

List> 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> 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> formatHumpNameForList(List> list) {

List> newList = new ArrayList>();

for (Map o : list) {

newList.add(formatHumpName(o));

}

return newList;

}

public static Map formatHumpName(Map map) {

Map newMap = new HashMap();

Iterator> it = map.entrySet().iterator();

while (it.hasNext()) {

Map.Entry 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();

}

标签:ri,resource,String,Jpa,多表多,查询,type,id,name

来源: https://www.cnblogs.com/zys-blog/p/11302122.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JPA实现多表聚合函数分页并且查询条件动态的方法如下: 1. 在Repository中定义方法,使用JPQL查询语句实现多表查询和聚合函数,并使用`Pageable`参数实现分页。同时,使用`CriteriaBuilder`和`CriteriaQuery`实现动态查询条件,例如: ``` @Repository public interface TableARepository extends JpaRepository<TableA, Long> { Page<Object[]> multiTableAggregationQuery( @Param("id") Long id, @Param("name") String name, Pageable pageable ) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class); Root<TableA> rootA = cq.from(TableA.class); Join<TableA, TableB> joinB = rootA.join("tableBs"); List<Predicate> predicates = new ArrayList<>(); if (id != null) { predicates.add(cb.equal(rootA.get("id"), id)); } if (name != null) { predicates.add(cb.like(joinB.get("name"), "%" + name + "%")); } cq.multiselect( rootA, cb.countDistinct(joinB) ); cq.groupBy(rootA); if (!predicates.isEmpty()) { cq.where(cb.and(predicates.toArray(new Predicate[0]))); } TypedQuery<Object[]> query = entityManager.createQuery(cq); query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize()); query.setMaxResults(pageable.getPageSize()); return new PageImpl<>(query.getResultList(), pageable, count()); } } ``` 2. 调用定义的方法,传入分页参数和查询条件参数,例如: ``` Pageable pageable = PageRequest.of(0, 10); Page<Object[]> result = tableARepository.multiTableAggregationQuery(1L, "name", pageable); ``` 注意:使用JPQL查询语句需要注意SQL注入问题,因此需要对查询参数进行验证和过滤。同时,使用JPQL查询语句可以提高代码的可移植性,但是需要注意性能问题。如果查询结果较大,建议采用分页查询的方式,避免一次性加载过多数据导致内存溢出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值