SpringBoot笔记6---如何读写数据库之使用JpaSpecificationExecutor读写数据库

关于如何配置数据库以及需要添加哪些依赖包,在《SpringBoot笔记3---如何读写数据库之使用CrudRepository中提供的方法来读写数据库》中有详细说明,这里不再赘述。本文基于SpringBoot笔记3一文的基础上,说明如何通过JpaSpecificationExecutor来读写数据库。

注意:JpaSpecificationExecutor一个重要的特点是可以实现分页读取数据库。


1 新建UserPersistence类如下:

package com.gm.springboot_test.persistence;

import java.util.List;

import com.gm.springboot_test.entity.User;

public interface UserPersistence {
	public List getByName(String name);
	public List getAgeByName(String name);
}

2 新建UserDao类如下:

package com.gm.springboot_test.dao;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.gm.springboot_test.entity.User;

@Repository
public interface UserDao extends CrudRepository, 
JpaSpecificationExecutor {
	
}
注意: 一定要继承CrudRepository和JpaSpecification类。


3 新建UserPersistenceImpl类如下:

package com.gm.springboot_test.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

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.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import com.gm.springboot_test.dao.UserDao;
import com.gm.springboot_test.entity.User;
import com.gm.springboot_test.persistence.UserPersistence;

@Repository
public class UserPersistenceImpl implements UserPersistence {
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public List getByName(final String name) {
		// TODO Auto-generated method stub
		int pageIndex = 0;
		int pageSize = 5;
		
		Pageable pageable = new PageRequest(pageIndex, pageSize, new Sort("age"));
		
		Page ens = userDao.findAll(new Specification() {

			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Predicate predicate = cb.equal(root.get("name").as(String.class), name);
				return predicate;
			}
			
		}, pageable);
		
		List userList = ens.getContent();
		return userList;
	}

	@Override
	public List getAgeByName(final String name) {
		// TODO Auto-generated method stub
		List userList = userDao.findAll(new Specification() {

			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Predicate predicate = cb.equal(root.get("name").as(String.class), name);
				return predicate;
			}
			
		});
		
		List ageList = new ArrayList();
		for (User user : userList) {
			ageList.add(user.getAge());	// age index is 2
		}
		
		return ageList;
	}
}



你可能感兴趣的:(SpringBoot,SpringBoot,数据库)