Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1289|回复: 0
打印 上一主题 下一主题

Spring-data-jpa详解 全方位介绍

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2018-6-5 09:26:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
https://blog.csdn.net/zombres/article/details/80403462

其中自定义sql动态查询语句
  1. public class StudentRepositoryImpl {
  2.    
  3.     @PersistenceContext
  4.     private EntityManager em;
  5.     @SuppressWarnings("unchecked")
  6.     public Page<Student> search(User user) {
  7.         String dataSql = "select t from User t where 1 = 1";
  8.         String countSql = "select count(t) from User t where 1 = 1";
  9.         
  10.         if(null != user && !StringUtils.isEmpty(user.getName())) {
  11.             dataSql += " and t.name = ?1";
  12.             countSql += " and t.name = ?1";
  13.         }
  14.         
  15.         Query dataQuery = em.createQuery(dataSql);
  16.         Query countQuery = em.createQuery(countSql);
  17.         
  18.         if(null != user && !StringUtils.isEmpty(user.getName())) {
  19.             dataQuery.setParameter(1, user.getName());
  20.             countQuery.setParameter(1, user.getName());
  21.         }long totalSize = (long) countQuery.getSingleResult();
  22.         Page<User> page = new Page();
  23.         page.setTotalSize(totalSize);
  24.         List<User> data = dataQuery.getResultList();
  25.         page.setData(data);
  26.         return page;
  27.     }
  28.    
  29. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-8 12:22 , Processed in 0.071985 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表