精通 Spring Boot 系列 08

简介: 精通 Spring Boot 系列 08

阅读全文,约 9 分钟

这是江帅帅的第010篇原创

Spring Data JPA

使用:将数据访问层接口实现 JpaRepository 接口即可完成 Spring Data JPA 访问数据。

JpaRepository 极大简化了 JPA 作为数据访问的代码。

今天给大家介绍几个案例:

  1. 简单条件查询
  2. 关联查询和 @Query 查询
  3. @NamedQuery 查询
  4. 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<Student> findByNameAndAddress(String name , String address);
 
    /**
     * 通过学生姓名模糊查询学生信息 
     * 此方法相当于JPQL语句代码
     * select s from Student s where s.name like ?1
     */
    List<Student> 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<Student> students) {
        studentRepository.saveAll(students);
    }
 
    // 根据名字查找
    public Student getStuByName(String name) {
        return studentRepository.findByName(name);
    }
 
    // 根据名字和地址查找
    public List<Student> getStusByNameAndAddress(String name,String address) {
        return studentRepository.findByNameAndAddress(name,address);
    }
 
    // 根据名字模糊查询
    public List<Student> 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<Student> 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<Student> getByNameAndAddress(String name,String address) {
        return studentService.getStusByNameAndAddress(name, address);
    }
 
    @RequestMapping("/nameLike")
    public List<Student> getByNameLile(String name) {
        return studentService.getStusByNameLike(name);
    }   
}
7)访问测试

http://localhost:8080/student/nameLike?name=帅

目录
相关文章
|
13天前
|
Java Spring
精通 Spring Boot 系列 09
精通 Spring Boot 系列 09
23 0
|
13天前
|
Java Spring
精通 Spring Boot 系列 10
精通 Spring Boot 系列 10
23 0
|
13天前
|
存储 Java Maven
精通 Spring Boot 系列 01
精通 Spring Boot 系列 01
7 0
|
9月前
|
XML Java 数据库连接
为什么越来越多的人选择Spring Boot?
我们都知道,Spring是一个非常经典的应用框架,与其说是Java开发不如说是Spring开发,为什么现在越来越多的人会选择用Spring Boot呢?。要回答这个问题,还需要从Java Web开发的发展历史开始说起。
65 0
|
8月前
|
存储 缓存 算法
Spring Boot 中的 ConcurrentMapCacheManager
Spring Boot 中的 ConcurrentMapCacheManager
|
10月前
|
Java Spring
Spring Boot 3.0
Spring Boot 3.0
574 0
|
XML NoSQL Java
欢迎光临Spring Boot时代(一)2
欢迎光临Spring Boot时代(一)2
欢迎光临Spring Boot时代(一)2
|
Java Spring 容器
【Spring Boot系列1】一文带你了解Spring Boot(下)
这篇文章其实只讲述了SpringBoot的执行流程,如果只是纯粹使用SpringBoot,也可以不用去了解这些,因为只需要知道怎么用就可以,但是学习一门技术,还是需要多去深究一下,不能仅仅停留在会使用的基础上。
137 0
【Spring Boot系列1】一文带你了解Spring Boot(下)
|
XML 监控 Java
【Spring Boot系列1】一文带你了解Spring Boot(上)
这几天一直没有更新,本来上一篇文章打算写MyBatis和实际项目的应用,当我把项目代码看完后,发现里面很多地方还是不太理解,就先不写了,等过两个月,实力水平提上来后,我再写这块内容。
261 0
【Spring Boot系列1】一文带你了解Spring Boot(上)
|
Java Spring
Spring Boot中使用@JsonComponent
Spring Boot中使用@JsonComponent