PagingAndSortingRepository

使用该接口可以大大简化排序和分页操作。

配置文件 




    

    
        
        
        
        
    
    
        
        
            
                
                
                
            
        
        
            
                com.fpy.pojo
            
        
    

    
    
        
    
    
    
    
    
    
    

 

实体类:

package com.fpy.pojo;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="user_id")
    private int id;
    private String name;
    private int age;
    private Date birth;

    public User() {
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public Date getBirth() {
        return this.birth;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof User)) return false;
        final User other = (User) o;
        if (!other.canEqual((Object) this)) return false;
        if (this.id != other.id) return false;
        final Object this$name = this.name;
        final Object other$name = other.name;
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        if (this.age != other.age) return false;
        final Object this$birth = this.birth;
        final Object other$birth = other.birth;
        if (this$birth == null ? other$birth != null : !this$birth.equals(other$birth)) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        result = result * PRIME + this.id;
        final Object $name = this.name;
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        result = result * PRIME + this.age;
        final Object $birth = this.birth;
        result = result * PRIME + ($birth == null ? 43 : $birth.hashCode());
        return result;
    }

    public String toString() {
        return "User(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", birth=" + this.birth + ")";
    }
}

dao接口:

public interface UserDao extends PagingAndSortingRepository {

}

单元测试:

package com.fpy.test;

import com.fpy.dao.UserDao;
import com.fpy.pojo.User;
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.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {


    @Autowired
    UserDao userDao;
    @org.junit.Test
    public void count() {
        long count = userDao.count();
        System.out.println(count);
    }

    /**
     * 分页测试+排序测试
     */
    @org.junit.Test
    public void page() {
        /*
        size: 每页显示的条数

        page:第几页 0代表第一页

        sort:排序规则 将所有数据按照规则进行排序
             单个规则:Sort sort = new Sort(Sort.Direction.DESC,"age");
             多个规则:谁在前 先按照谁进行排序
               Sort.Order  order2 = new Sort.Order(Sort.Direction.DESC,"age");
               Sort.Order  order1 = new Sort.Order(Sort.Direction.DESC,"id");
               Sort sort = new Sort(order2,order1);

         注意:如果实体类中的字段与表名不符 以实体类字段为主
         例子: 实体类中为id  表中的字段是 user_id
         */

        Sort.Order  order2 = new Sort.Order(Sort.Direction.DESC,"age");
        Sort.Order  order1 = new Sort.Order(Sort.Direction.DESC,"id");
        Sort sort = new Sort(order2,order1);
        Pageable pageable = new PageRequest(0,9,sort);
        Page pages = userDao.findAll(pageable);

        System.out.println("查询总页数:"+pages.getTotalPages());
        System.out.println("查询总记录数:"+pages.getTotalElements());
        System.out.println("查询当前第几页:"+pages.getNumber()+1);//注意 0开始
        System.out.println("查询当前页面的集合:"+pages.getContent());
        System.out.println("查询当前页面的记录数:"+pages.getNumberOfElements());

        List content = pages.getContent();
        for (User user : content) {
            System.out.println(user);
        }
    }
}

PagingAndSortingRepository_第1张图片

你可能感兴趣的:(SpringData)