本帖最后由 shuzhen526 于 2013-04-01 14:16:52 编辑

解决方案 »

  1.   

    下载springside例子,继承了分页demo
      

  2.   

    我想用spring data jpa的接口中提供的分页
      

  3.   

    想用就用呗。createQuery,setParameter,setFirstResult,setMaxResults,getResultList,不就这么几步吗。
      

  4.   


    @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源码 看用的上不,或者看看从这里面悟出些道道不
      

  5.   

    楼主,问题如何解决的,jpa之前木有用过!
      

  6.   

    taskDao中的findAll怎么不贴全?
      

  7.   

    分页应该在写sql的时候,配置开始id和结束id就行了吧
      

  8.   

    谢谢各位啊。问题早就解决了,由于时间关系没来得及结贴,再次感谢。PagingAndSortingRepository这个接口中有带条件的分布查询方法,根据它的规范并传进去相应的参数即可。
      

  9.   

    SearchFilter  需要自己构造对吗?
      

  10.   

    时间太久了,我现在手上没有源码。dao层的接口要继承这个PagingAndSortingRepository接口,它里面有分页排序查询的接口,传相应的参数进去即可,不用自己来实现可直接调用。
      

  11.   


    这个里面的参数具体怎么传啊。我是要TIME  between *** and *** and name= *** 怎么传值进去。。  
      

  12.   


    这个里面的参数具体怎么传啊。我是要TIME  between *** and *** and name= *** 怎么传值进去。。  
      

  13.   


    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>
      

  14.   

    spring data jpa 没玩过啊!
      

  15.   


    这个里面的参数具体怎么传啊。我是要TIME  between *** and *** and name= *** 怎么传值进去。。  
    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
    }
    接口名的规范:
      

  16.   

    spring data jpa相关代码demo下载:http://www.zuidaima.com/share/kjpa-p1-s1.htm