Spring Data Jpa 简单分页查询(1)

      ibatis发展到MyBatis,已经越来越像Hibernate,就像有人调侃,nosql技术系发展到最后就是关系型数据库。

 

      嘿嘿。

 

关于Spring data jpa的简单分页查询可以参考SpringDataJpa的官方文档。

 

1:实体类和表的关系

(1)表数据结构

create table T_USER
(
  ID       VARCHAR2(50),
  USERID   VARCHAR2(50),
  USERNAME VARCHAR2(50),
  PASSWORD VARCHAR2(50)
)

 

 

(2)实体类

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@SuppressWarnings("serial")
@Entity
@Table(name = "T_USER")
@Data
public class SimpleUserEntity implements Serializable {

	@Id
	@Column( name = "ID", length = 50,unique = true,nullable = false)
	private String id;
	
	@Column( name = "USERID", length = 50,unique = true,nullable = false)
	private String userId;
	
	@Column( name = "USERNAME", length = 50,nullable = false)
	private String userName;
	
	@Column( name = "PASSWORD", length = 50,nullable = false)
	private String passWord;
	
}

 

 

1:new Object(feild1,feild2)的用法

 

(1)该方式需保证对应实体类的方法,有对应的构造方法。

	public SimpleUserEntity(String userId,String userName){
		this.userId = userId;
		this.userName = userName;
	}

 

 

 

(2)新建Repository接口

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;


public interface SimpleQueryRepository extends PagingAndSortingRepository {
	
	@Query( value = " select new SimpleUserEntity(s.userId,s.userName) from SimpleUserEntity s ", countQuery = " select count(s) from SimpleUserEntity s" )
	Page userPageData(Pageable page);
	
}

 

 (3)测试用例

 

import lombok.extern.slf4j.Slf4j;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.slfinance.shanlinbao.repository.SimpleQueryRepository;
import com.slfinance.shanlinbao.repository.SimpleUserEntity;

@ContextConfiguration(locations = { "classpath:/application-jpa.xml"})
@ActiveProfiles("dev")
@Slf4j
public class SimpleQueryRepositoryTest extends AbstractJUnit4SpringContextTests {

	@Autowired
	SimpleQueryRepository simpleQueryRepository;
	
	final int pageNum = 0, pageSize = 10;
	
	@Test
	public void testFindPage(){
		
		Order order = new Order(Direction.DESC,"id"); //字段排序O
		PageRequest pageRequest = new PageRequest(pageNum, pageSize, new Sort(order));
		Page userPage = simpleQueryRepository.userPageData(pageRequest);
	}
}

 (3)返回数据

        {
            "id": null,
            "userId": "A004",
            "userName": "jack4",
            "passWord": null
        }

 

 

2:new Map(feild1 as otherAlias1,feild2 ad otherAlias2)

有时有些业务需要将查询出的字段更改别名,通过new Map() 方式也是思路之一。

(1)接口SimpleQueryRepository新增如下方法 

	@Query( value = " select new Map(s.userId as userNo,s.userName as name) from SimpleUserEntity s ", countQuery = " select count(s) from SimpleUserEntity s" )
	Page> userPageDataMap(Pageable page);

 (2)SimpleQueryRepositoryTest新增方法

	@Test
	public void testFindPageForMap(){
		Order order = new Order(Direction.DESC,"id"); //字段排序O
		PageRequest pageRequest = new PageRequest(pageNum, pageSize, new Sort(order));
		Page> userPage = simpleQueryRepository.userPageDataMap(pageRequest);
	}

  

 (3)返回数据

        {
            "userNo": "A004",
            "name": "jack4"
        }

 

 

 

 

你可能感兴趣的:(Hibernate,JPA)