Spring Data Jpa簡易ページングクエリ(1)

5011 ワード

ibatisはMyBatisに発展し、Hibernateのようになってきた.
 
へへへ.
 
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(feild 1,feild 2)の使い方
 
(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)SimpleQueryRepository Test新規メソッド
	@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"
        }