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<SimpleUserEntity, String> {
	
	@Query( value = " select new SimpleUserEntity(s.userId,s.userName) from SimpleUserEntity s ", countQuery = " select count(s) from SimpleUserEntity s" )
	Page<SimpleUserEntity> 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<SimpleUserEntity> 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<Map<String,Object>> 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<Map<String,Object>> userPage = simpleQueryRepository.userPageDataMap(pageRequest);
	}

  

 (3)返回数据

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值