利用spring Data Jpa进行分页和排序

spring Data Jpa:

       Spring Data JPA:Spring Data JPA 是 spring data 项目下的一个模块。提供了一套基于 JPA标准操作数据库的简化方案。底层默认的是依赖 Hibernate JPA 来实现的。
     Spring Data JPA 的技术特点:我们只需要定义接口并集成 Spring Data JPA 中所提供的接
口就可以了。不需要编写接口实现类。

spring  data  jpa的接口继承结构:

            利用spring Data Jpa进行分页和排序_第1张图片

 

由图可以知道接口PagingAndSortingRepository的接口中提供分页和排序

    自定义接口继承PagingAndSortingRepository

import com.bjsxt.pojo.Users;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface UsersDao extends PagingAndSortingRepository {//自定义接口

}

spring  data jpa的配置文件:



      
      
      
        
            
            
            
            
        
    
        
            
            
                
                    
                    
                    
                
            
            
            
                
                    com.bjsxt.pojo
                
            
        

    
    
       

    
    
    
    
    

    
    
    

连接数据库的配置信息:

jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.driver.class=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

实体类:

package com.bjsxt.pojo;

import javax.persistence.*;

@Entity
@Table(name = "t_users")
public class Users {
    @Id//主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)//主键自增
    @Column(name = "userId")
    private  Integer  userId;
    @Column(name = "name")
    private  String name;
    @Column(name = "gender")
    private  String  gender;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Users{" +
                "userId=" + userId +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

实现类:

package com.bjsxt.test;


import com.bjsxt.dao.UsersDao;
import com.bjsxt.pojo.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserDaoImplTest {
    @Autowired
    private UsersDao usersDao;

    /**
     * 分页
     */

@Test
    public   void   testPage(){
        int page=1;//当前页的索引,默认是0开始的,不是1
        int size=3;//每页显示的数据
        Pageable pageable=new PageRequest(page,size);
        Page pa = this.usersDao.findAll(pageable);
        System.out.println("总数目:"+ pa.getTotalElements());
        System.out.println("共"+pa.getTotalPages()+"页");
        List content = pa.getContent();
        for (Users user:
             content) {
            System.out.println(user);
        }
    }

    /**
     * 排序
     */
    @Test
    public  void   sortUser(){
       //多列排序
        //sort类封装排序规则以及指定的排序字段
         //direction 排序规则
          //properties:指定的做排序的属性
        Sort.Order order=new Sort.Order(Sort.Direction.DESC,"gender");
        Sort.Order order1=new Sort.Order(Sort.Direction.DESC,"userId");
        Sort sort=new Sort(order,order1);
     List list= (List) this.usersDao.findAll(sort);
        for (Users user:
             list) {
            System.out.println(user);
        }

    }

}

 

你可能感兴趣的:(spring_data,spring,data,jpa,排序和分页)