spring mvc + spring data jpa 带条件的查询分页的问题

檸檬香草 2013-04-01 02:15:37
如题,spring data jpa用的是hibernate的实现。dao层继承了JpaRepository接口。初接触spring data jpa,求大神给小女子一个带条件的查询分页的demo。感激不尽!
...全文
60024 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
caoxinhe577886 2014-08-05
  • 打赏
  • 举报
回复
ad
yaerfeng 2014-08-03
  • 打赏
  • 举报
回复
spring data jpa相关代码demo下载:http://www.zuidaima.com/share/kjpa-p1-s1.htm
檸檬香草 2013-12-12
  • 打赏
  • 举报
回复
引用 18 楼 Y19900528 的回复:
[quote=引用 17 楼 shuzhen526 的回复:]
时间太久了,我现在手上没有源码。dao层的接口要继承这个PagingAndSortingRepository接口,它里面有分页排序查询的接口,传相应的参数进去即可,不用自己来实现可直接调用。


这个里面的参数具体怎么传啊。我是要TIME between *** and *** and name= *** 怎么传值进去。。 [/quote]
public interface UserRepository extends CrudRepository<User,Integer>{//继承你需要的接口

User findByNameAndTimeBetween(String name,Date startDate,Date endDate);//在这里声明一下你需要的接口,当然,这个接口名得按照框架的规范来写。有的接口里已经存在的方法了,就不需要声明了。

@Query( " select u from User u where u.firstname = ? " )
List < User > findByFirstname(String firstname);//当然,如果实在满足不了你的需求,你也可以这样来自己写查询语句
//那个分页的,dao层的接口要继承这个PagingAndSortingRepository接口,然后直接调用方法传参数就可以了,实在不懂的加我qq吧,173636035
}

接口名的规范:
a_jian 2013-12-12
  • 打赏
  • 举报
回复
spring data jpa 没玩过啊!
Y19900528 2013-12-12
  • 打赏
  • 举报
回复
引用 20 楼 whos2002110 的回复:

package com.mediaadx.dynamix.authority.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.mediaadx.dynamix.account.entity.UserAccount;
import com.mediaadx.dynamix.authority.entity.Security;

public interface ISecurityDAO extends PagingAndSortingRepository<Security, Long> {

    Page<Security> findByUserNameLike(String name, Pageable pageable);
}

注意里面使用Page,Pageable是spring的类,Security是个entity里面有属性user<UserAccount>,UserAccount里面有个name<String>
大神,能加下QQ说么。。。416192245
panzhixiong_cn 2013-12-12
  • 打赏
  • 举报
回复
学习帖,收藏了。
whos2002110 2013-12-11
  • 打赏
  • 举报
回复

package com.mediaadx.dynamix.authority.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.mediaadx.dynamix.account.entity.UserAccount;
import com.mediaadx.dynamix.authority.entity.Security;

public interface ISecurityDAO extends PagingAndSortingRepository<Security, Long> {

    Page<Security> findByUserNameLike(String name, Pageable pageable);
}

注意里面使用Page,Pageable是spring的类,Security是个entity里面有属性user<UserAccount>,UserAccount里面有个name<String>
Y19900528 2013-12-11
  • 打赏
  • 举报
回复
引用 17 楼 shuzhen526 的回复:
时间太久了,我现在手上没有源码。dao层的接口要继承这个PagingAndSortingRepository接口,它里面有分页排序查询的接口,传相应的参数进去即可,不用自己来实现可直接调用。
这个里面的参数具体怎么传啊。我是要TIME between *** and *** and name= *** 怎么传值进去。。
Y19900528 2013-12-11
  • 打赏
  • 举报
回复
引用 17 楼 shuzhen526 的回复:
时间太久了,我现在手上没有源码。dao层的接口要继承这个PagingAndSortingRepository接口,它里面有分页排序查询的接口,传相应的参数进去即可,不用自己来实现可直接调用。
这个里面的参数具体怎么传啊。我是要TIME between *** and *** and name= *** 怎么传值进去。。
檸檬香草 2013-11-25
  • 打赏
  • 举报
回复
时间太久了,我现在手上没有源码。dao层的接口要继承这个PagingAndSortingRepository接口,它里面有分页排序查询的接口,传相应的参数进去即可,不用自己来实现可直接调用。
u011575083 2013-11-19
  • 打赏
  • 举报
回复
详细点吧 美女!!
东边小铺 2013-08-15
  • 打赏
  • 举报
回复
SearchFilter 需要自己构造对吗?
东边小铺 2013-08-14
  • 打赏
  • 举报
回复
美女 求demo 急用啊
东边小铺 2013-08-14
  • 打赏
  • 举报
回复
到底怎么用啊 美女
檸檬香草 2013-06-15
  • 打赏
  • 举报
回复
引用 4 楼 lvzg_005 的回复:

@Component
public interface TaskDao extends PagingAndSortingRepository<Task, Long>, JpaSpecificationExecutor<Task> {

	Page<Task> findByUserId(Long id, Pageable pageRequest);

	@Modifying
	@Query("delete from Task task where task.user.id=?1")
	void deleteByUserId(Long id);
}

public Page<Task> getUserTask(Long userId, Map<String, Object> searchParams, int pageNumber, int pageSize,
			String sortType) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
		Specification<Task> spec = buildSpecification(userId, searchParams);

		return taskDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		} else if ("title".equals(sortType)) {
			sort = new Sort(Direction.ASC, "title");
		}

		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Task> buildSpecification(Long userId, Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		filters.put("user.id", new SearchFilter("user.id", Operator.EQ, userId));
		Specification<Task> spec = DynamicSpecifications.bySearchFilter(filters.values(), Task.class);
		return spec;
	}
帮你贴了一部分springside源码 看用的上不,或者看看从这里面悟出些道道不
跟这个差不多。就是这么用的。
檸檬香草 2013-06-15
  • 打赏
  • 举报
回复
引用 3 楼 forgetsam 的回复:
想用就用呗。createQuery,setParameter,setFirstResult,setMaxResults,getResultList,不就这么几步吗。
跟这个差不多。就是这么用的。
檸檬香草 2013-06-15
  • 打赏
  • 举报
回复
谢谢各位啊。问题早就解决了,由于时间关系没来得及结贴,再次感谢。PagingAndSortingRepository这个接口中有带条件的分布查询方法,根据它的规范并传进去相应的参数即可。
ZHOU西口 2013-06-04
  • 打赏
  • 举报
回复
分页应该在写sql的时候,配置开始id和结束id就行了吧
henghengdh 2013-06-04
  • 打赏
  • 举报
回复
taskDao中的findAll怎么不贴全?
ro_nice 2013-05-15
  • 打赏
  • 举报
回复
楼主,问题如何解决的,jpa之前木有用过!
加载更多回复(6)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧