精通 Spring Boot 系列 10

简介: 精通 Spring Boot 系列 10

阅读全文,约 9 分钟

这是江帅帅的第012篇原创

案例3:NamedQuery 查询

使用 NameQuery 就是一个名称映射一个查询语句的查询操作。

1)编辑 pom.xml 文件(与 CrudRepository 接口案例一样)
2)创建 Student 和 Clazz 持久化类

先看 Student 类。

@Entity
@Table(name="tb_student")
// 查询班级下的学生
@NamedQuery(name="Student.findStudentsByClazzName",query="select s from Student s where s.clazz.name = ?1")
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;
 
    // 学生与班级是多对一的关系,这里配置的是双向关联
    @ManyToOne(fetch=FetchType.LAZY,
            targetEntity=Clazz.class
            )
    @JoinColumn(name="clazzId",referencedColumnName="code")
    private Clazz clazz ;
    public Student() {
 
    }
    public Student(String name, String address, int age, char sex,
            Clazz clazz) {
        super();
        this.name = name;
        this.address = address;
        this.age = age;
        this.sex = sex;
        this.clazz = clazz;
    }
 
    // setXxx 和 getXxx 方法
}

再来看看 Clazz 类。

@Entity
@Table(name="tb_clazz")
public class Clazz implements Serializable{
 
    private static final long serialVersionUID = 1L;
 
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int code ;
    private String name ;
 
    // 班级与学生是一对多的关联
    @OneToMany(
               fetch=FetchType.LAZY,
               targetEntity=Student.class,
               mappedBy="clazz"
            )     
    private Set<Student> students = new HashSet<>();
 
    public Clazz() {
 
    }
    // 班级对象
    public Clazz(String name) {
        this.name = name;
    }
 
    // setXxx 和 getXxx 方法
}
3)创建 StudentRepository 和 ClazzRepository 数据访问接口

先看 StudentRepository 类。

public interface StudentRepository extends JpaRepository<Student, Integer> {
    /**
     * 查询班级下的所有学生
     */
    List<Student> findStudentsByClazzName(String clazzName);
}

再看 ClazzRepository 类。

public interface ClazzRepository extends JpaRepository<Clazz, Integer> {    
}
4)创建 ShcoolService 业务层
@Service
public class ShcoolService {
    // 注入数据访问层接口对象 
    @Resource
    private StudentRepository studentRepository;
    @Resource
    private ClazzRepository clazzRepository;
 
    @Transactional
    public void saveClazzAll(List<Clazz> clazzs) {
        clazzRepository.saveAll(clazzs);
    }
    @Transactional
    public void saveStudentAll(List<Student> students) {
        studentRepository.saveAll(students);
    }
 
    public List<Map<String, Object>> getStusByClazzName(String clazzName) {
        // 查询班级下的所有学生
        List<Student> students = studentRepository.findStudentsByClazzName(clazzName);
        List<Map<String, Object>>  results = new ArrayList<>(); 
        // 遍历查询出的学生对象,提取姓名,年龄,性别信息
        for(Student student:students){
            Map<String , Object> stu = new HashMap<>(); 
            stu.put("name", student.getName());
            stu.put("age", student.getAge());
            stu.put("sex", student.getSex());
            results.add(stu);
        }
        return results;
    }
}
5)创建 StudentController 控制器类
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private ShcoolService shcoolService;
    @RequestMapping("/save")
    public String save() {
 
        Clazz clazz1 = new Clazz("架构师001班");
        Clazz clazz2 = new Clazz("架构师002班");
        // 保存班级对象数据
        List<Clazz> clazzs = new ArrayList<>();
        clazzs.add(clazz1);
        clazzs.add(clazz2);
        shcoolService.saveClazzAll(clazzs);
 
        Student s1 = new Student("帅帅","广州",18,'男',clazz1);
        Student s2 = new Student("小黄","广州",17,'女',clazz1);
        Student s3 = new Student("小红","广州",15,'男',clazz2);
 
        List<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        shcoolService.saveStudentAll(students);
        return "保存学生成功";
    }
    /**
     * 查询某个班级下所有的学生姓名,年龄,性别
     */
    @RequestMapping("/getClazzStus")
    public List<Map<String, Object>> getClazzStus(String clazzName){
        return shcoolService.getStusByClazzName(clazzName);
    } 
}
6)测试

http://localhost:8080/student/save

目录
相关文章
|
13天前
|
JSON Java fastjson
精通 Spring Boot 系列 05
精通 Spring Boot 系列 05
18 0
|
13天前
|
存储 安全 Java
精通 Spring Boot 系列 14
精通 Spring Boot 系列 14
11 0
|
13天前
|
存储 Java Maven
精通 Spring Boot 系列 01
精通 Spring Boot 系列 01
7 0
|
13天前
|
Java 关系型数据库 数据库连接
精通 Spring Boot 系列 07
精通 Spring Boot 系列 07
18 0
|
13天前
|
Java 数据库 Spring
精通 Spring Boot 系列 12
精通 Spring Boot 系列 12
24 0
|
13天前
|
安全 Java 应用服务中间件
精通 Spring Boot 系列 03
精通 Spring Boot 系列 03
23 0
|
12月前
|
开发框架 Java 开发者
spring boot介绍
spring boot介绍
|
Java Spring 容器
spring boot到底帮我们做了那些事?
要是想在spring boot初始化的时候搞点事情的化,那么有3种方法: 1.创建ApplicationContextInitializer的实现类 2.创建ApplicationListener的实现类 3.创建ApplicationRunner和CommandLineRunner的实现类
spring boot到底帮我们做了那些事?
|
Java 测试技术 API
Spring Boot的TestRestTemplate使用
Spring Boot的TestRestTemplate使用
|
XML 监控 安全
告诉你,Spring Boot 真是个牛逼货!
现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下。