SpringBoot笔记6---如何读写数据库之使用JpaSpecificationExecutor读写数据库
2018-01-10 18:25
417 查看
关于如何配置数据库以及需要添加哪些依赖包,在《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<User> getByName(String name);
public List<Integer> 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<User, Integer>,
JpaSpecificationExecutor<User> {
}注意:
一定要继承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<User> 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<User> ens = userDao.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> 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<User> userList = ens.getContent();
return userList;
}
@Override
public List<Integer> getAgeByName(final String name) {
// TODO Auto-generated method stub
List<User> userList = userDao.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// TODO Auto-generated method stub
Predicate predicate = cb.equal(root.get("name").as(String.class), name);
return predicate;
}
});
List<Integer> ageList = new ArrayList();
for (User user : userList) {
ageList.add(user.getAge()); // age index is 2
}
return ageList;
}
}
注意: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<User> getByName(String name);
public List<Integer> 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<User, Integer>,
JpaSpecificationExecutor<User> {
}注意:
一定要继承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<User> 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<User> ens = userDao.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> 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<User> userList = ens.getContent();
return userList;
}
@Override
public List<Integer> getAgeByName(final String name) {
// TODO Auto-generated method stub
List<User> userList = userDao.findAll(new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// TODO Auto-generated method stub
Predicate predicate = cb.equal(root.get("name").as(String.class), name);
return predicate;
}
});
List<Integer> ageList = new ArrayList();
for (User user : userList) {
ageList.add(user.getAge()); // age index is 2
}
return ageList;
}
}
相关文章推荐
- SpringBoot笔记3---如何读写数据库之使用CrudRepository中提供的方法来读写数据库
- SpringBoot笔记4---如何读写数据库之使用@Query注解来读写数据库
- JpaSpecificationExecutor<T>不是单独使用
- 如何在数据库中使用存储过程 【数据库高效编程 - 学习笔记 第九章】
- SpringBoot第二讲利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- SpringBoot开发详解(十) -- 使用JPA访问数据库下篇及使用Page进行数据分页
- SpringBoot第二讲 利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- springboot 学习笔记【3】Spring Boot中使用Spring-data-jpa
- 如何在数据库中使用索引 【数据库高效编程 - 学习笔记 第七章】
- SQL Server学习笔记1:如何使用软件以及创建第一个数据库
- 如何在网店数据库中使用视图 【数据库高效编程 - 学习笔记 第八章】
- Spring Data JPA(3)--JpaRepository和JpaSpecificationExecutor的结合使用
- SpringBoot学习之路:03.Spring Boot使用Jpa操作数据库
- SpringBoot笔记5---如何读写数据库之通过Query对象读写数据库
- 如何使用Hadoop读写数据库
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)
- 如何使用VS.NET调试数据库存储过程 longrujun [原作]
- 如何使用 Visual C# .NET 从 DataSet 对象更新数据库
- 如何使用 ADO.NET 轻松操纵数据库
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格