firemail
标题:
Spring-data-jpa详解 全方位介绍
[打印本页]
作者:
java
时间:
2018-6-5 09:26
标题:
Spring-data-jpa详解 全方位介绍
https://blog.csdn.net/zombres/article/details/80403462
其中自定义sql动态查询语句
public class StudentRepositoryImpl {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("unchecked")
public Page<Student> search(User user) {
String dataSql = "select t from User t where 1 = 1";
String countSql = "select count(t) from User t where 1 = 1";
if(null != user && !StringUtils.isEmpty(user.getName())) {
dataSql += " and t.name = ?1";
countSql += " and t.name = ?1";
}
Query dataQuery = em.createQuery(dataSql);
Query countQuery = em.createQuery(countSql);
if(null != user && !StringUtils.isEmpty(user.getName())) {
dataQuery.setParameter(1, user.getName());
countQuery.setParameter(1, user.getName());
}long totalSize = (long) countQuery.getSingleResult();
Page<User> page = new Page();
page.setTotalSize(totalSize);
List<User> data = dataQuery.getResultList();
page.setData(data);
return page;
}
}
复制代码
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3