精通 Spring Boot 系列文(8)
程序IT圈
共 4222字,需浏览 9分钟
·
2020-07-27 18:53
阅读全文,约 9 分钟
Spring Data JPA
使用:将数据访问层接口实现 JpaRepository 接口即可完成 Spring Data JPA 访问数据。
JpaRepository 极大简化了 JPA 作为数据访问的代码。
今天给大家介绍几个案例:
简单条件查询
关联查询和 @Query 查询
@NamedQuery 查询
Specification 查询
案例1:简单条件的查询
1)编辑 pom.xml 文件(与 CrudRepository 接口案例一样)
2)编辑 application.properties 文件(与 CrudRepository 接口案例一样)
3)创建 Student 持久化类
package nx.bean;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
// 学生
@Entity
@Table(name="tb_student") // 学生表
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name ; // 名字
private String address ; // 地址
private int age ; // 年龄
private char sex; // 性别
// getXxx & setXxx 方法
}
4)创建 StudentRepository 数据访问接口
package nx.repository;
import java.util.List;
import nx.bean.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Integer> {
/**
* 通过学生姓名来查询学生对象
* 此方法相当于JPQL语句代码
* select s from Student s where s.name = ?1
*/
Student findByName(String name);
/**
* 通过名字和地址查询学生信息
* 此方法相当于JPQL语句代码
* select s from Student s where s.name = ?1 and s.address=?2
*/
List findByNameAndAddress(String name , String address) ;
/**
* 通过学生姓名模糊查询学生信息
* 此方法相当于JPQL语句代码
* select s from Student s where s.name like ?1
*/
List findByNameLike(String name) ;
}
5)创建 StudentService 业务层类
package nx.service;
import java.util.List;
import javax.annotation.Resource;
import nx.bean.Student;
import nx.repository.StudentRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class StudentService {
// 注入数据访问层接口对象
@Resource
private StudentRepository studentRepository;
// 保存所有
@Transactional
public void saveAll(List students) {
studentRepository.saveAll(students);
}
// 根据名字查找
public Student getStuByName(String name) {
return studentRepository.findByName(name);
}
// 根据名字和地址查找
public List getStusByNameAndAddress(String name,String address) {
return studentRepository.findByNameAndAddress(name,address);
}
// 根据名字模糊查询
public List getStusByNameLike(String name) {
return studentRepository.findByNameLike("%"+name+"%");
}
}
6)创建 StudentController 控制器类
package nx.controller;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import nx.bean.Student;
import nx.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/student")
public class StudentController {
// 注入StudentService
@Resource
private StudentService studentService;
@RequestMapping("/save")
public String save() {
Student s1 = new Student();
s1.setAddress("广州");
s1.setName("帅帅");
s1.setAge(18);
s1.setSex('男');
Student s2 = new Student();
s2.setAddress("广州");
s2.setName("花花");
s2.setAge(17);
s2.setSex('女');
List students = new ArrayList<>();
students.add(s1);
students.add(s2);
studentService.saveAll(students);
return "保存student对象成功";
}
@RequestMapping("/name")
public Student getByName(String name) {
return studentService.getStuByName(name);
}
@RequestMapping("/nameAndAddress")
public List getByNameAndAddress(String name,String address) {
return studentService.getStusByNameAndAddress(name, address);
}
@RequestMapping("/nameLike")
public List getByNameLile(String name) {
return studentService.getStusByNameLike(name);
}
}
7)访问测试
http://localhost:8080/student/nameLike?name=帅
Java后端编程
更多Java推文,关注公众号
评论