SpringData JPA实现CRUD,分页与多参数排序
Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库、Map-Reduce 框架、云数据服务等等,SpringData JPA是简化创建 JPA 数据访问层和跨存储的持久层功能,通过一个接口的继承即可实现简单的CRUD。
数据库的连接与项目的构建不说了,可以参考:springboot实战SpringDataJPA。我采用的是eclipse构建的普通的springboot项目。
一、创建表与实体类映射
@Entity @Table(name = "student") public class Student implements Serializable { @Id // id自动生成 @GeneratedValue @Column(name = "id") private Long id; @Column(name = "name") private String name; @Column(name = "clas") private String clas; @Column(name = "interest") private String interest; // 省略了getter(),setter()方法
二、创建StudentMapper 继承JpaRepository<Student, Long>,第一个参数是实体,第二个参数是主键的类型
public interface StudentMapper extends JpaRepository<Student, Long> { // int queryCount(); // 自定义的方法实现findAll @Query(value = "select * from student where id < ?1", nativeQuery = true) public List<Student> findAll(Long id); }
三、Controller内的代码如下:
@Resource private FirstPageService firstPageService; @RequestMapping(value = "/", method = RequestMethod.GET) @Fooish(tags = { "this_is_method" }) String home() { return firstPageService.getString(); } @RequestMapping(value = "/add", method = RequestMethod.GET) String addStudent(Student student) { return firstPageService.addStudent(student); } @RequestMapping(value = "/delete", method = RequestMethod.GET) String deleteStudent(Long id) { return firstPageService.deleteStudent(id); } @RequestMapping(value = "/update", method = RequestMethod.GET) String updateStudent(Student student) { return firstPageService.updateStudent(student); } @RequestMapping(value = "/list", method = RequestMethod.GET) List<Student> listStudent() { return firstPageService.listStudent(); } // 采用的是自定义的方法 @RequestMapping(value = "/list2", method = RequestMethod.GET) List<Student> list2Student(Long id) { return firstPageService.nativeQuery(id); } // 分页查询 @RequestMapping(value = "/pageRequest", method = RequestMethod.GET) List<Student> pageRequest() { return firstPageService.pageRequest(); } // 分页与排序查询 @RequestMapping(value = "/pageAndSortRequest", method = RequestMethod.GET) List<Student> pageAndSortRequest() { return firstPageService.pageAndSortRequest(); }
四、服务层firstPageService代码如下:
@Resource private StudentMapper studentMapper; public String getString() { // return name; // int a = studentMapper.queryCount(); // return a + ""; return "Hellow World!"; } public String addStudent(Student stu) { studentMapper.save(stu); return "添加成功"; } public String deleteStudent(Long id) { studentMapper.delete(id); return "删除成功"; } public String updateStudent(Student stu) { // 如果有就更新,没有就添加 studentMapper.save(stu); return "更新成功"; } public List<Student> listStudent() { return studentMapper.findAll(); } public List<Student> nativeQuery(Long id) { return studentMapper.findAll(id); }
// 分页查询 public List<Student> pageRequest() { PageRequest pageRequest = new PageRequest(1, 3); Page<Student> stuPage = studentMapper.findAll(pageRequest); List<Student> stuList = stuPage.getContent(); return stuList; }
// 分页与排序 public List<Student> pageAndSortRequest() { Sort sort1 = new Sort(Sort.Direction.DESC, "id"); Sort sort2 = new Sort(Sort.Direction.ASC, "name"); // 把两个排序的条件取取与 Sort sort = sort1.and(sort2); PageRequest pageRequest = new PageRequest(1, 2, sort); Page<Student> stuPage = studentMapper.findAll(pageRequest); List<Student> stuList = stuPage.getContent(); return stuList; }
通过源码的继承与实现关系可以看出,JpaRepository接口继承了PagingAndSortingRepository,QueryByExampleExecutor两个接口,PagingAndSortingRepository接口继承了CrudRepository。CrudRepository中包含了简单的数据库增,删,改,查,这四个操作还是比较简单的,下面主要介绍分页与排序;
下面的是PagingAndSortingRepository的源码:
public interface PagingAndSortingRepository extends CrudRepository { public abstract Iterable findAll(Sort sort); public abstract Page findAll(Pageable pageable); }
这个接口提供了分页与排序的基本方法,想要实现分页查询,要实现Pageable接口,而PageRequest是该接口的一个实现类,该类的构造方法:
// 单纯的分页 public PageRequest(int page, int size) { this(page, size, null); } public transient PageRequest(int page, int size, Sort.Direction direction, String properties[]) { this(page, size, new Sort(direction, properties)); } // 分页与排序,要实现Sort类 public PageRequest(int page, int size, Sort sort) { super(page, size); this.sort = sort; }
五、按照id倒序,name顺序,查询第二页,每页显示两个的查询结果: