Empolyee与Department的单向@ManytoOne级联查询

1.数据表

1)employee表:

department_id添加外键约束
插入4条记录:
在这里插入图片描述

2)department表:


插入2条记录:
在这里插入图片描述

2.JavaBean

1)Employee.Java

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Entity	//数据表实体
@Data	//Lombok简化getter/setter,constructor,toString代码
public class Employee {

    @Id		//对应数据表中的主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)		//对应数据表中的自动递增
    private Integer id;
    private String name;
    private String email;
    private Integer gender; //1 for ‘男’, 0 for '女'
    private Date birth;
    //  原本该有一个private Integer department_id;	该外键字段将由由@JoinColumn注解生成

    @ManyToOne(targetEntity = Department.class) //关联的实体对象,单向的多对一
    @JoinColumn(name = "department_id")         //映射的外键字段
    private Department department;

}

2)Department.Java

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity  //告诉jpa,这是一个实体类
@Data
public class Department {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private String name;

	//	如果需要查询一个部门中的所有员工,则需要一对多注解@OnetoMany,并设置一些级联、加载的属性
	//	private List<Employee> employees; List或Set
}

3.Dao

继承JpaRepository来完成对数据库的操作

1)EmployeeDao.java

import com.demo.springboot.entities.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeDao extends JpaRepository<Employee, Integer> {

}

2)EmployeeDao.java

import com.demo.springboot.entities.Employee;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeDao extends JpaRepository<Employee, Integer> {

}

4.Controller

package com.demo.springboot.controller;

import com.demo.springboot.dao.DepartmentDao;
import com.demo.springboot.dao.EmployeeDao;
import com.demo.springboot.entities.Department;
import com.demo.springboot.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Controller
public class EmployeeController {

    private EmployeeDao employeeDao;

    @Autowired
    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    private DepartmentDao departmentDao;

    @Autowired
    public void setDepartmentDao(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    /**查询所有员工信息
     * @param model 
     * @return
     */
    @GetMapping("/emps")
    public String list(Model model) {
        List<Employee> all = employeeDao.findAll();
        model.addAttribute("employees", all);  //放到请求域中
        return "employee/list";    //返回到list.html页面(Thymeleaf模板引擎的视图解析原理)
    }

	//...省略其他方法...//

}

5.list.html页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
   <!--省略了css的引入-->
</head>

<body>
		......
        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
            <h2><a class="btn btn-primary" th:href="@{/emp}">员工添加</a></h2>
            <div class="table-responsive">
                <table class="table table-striped table-sm">
                    <thead>
                    <tr>
                        <th>#id</th>
                        <th>lastName</th>
                        <th>email</th>
                        <th>gender</th>
                        <th>departmentName</th>
                        <th>birth</th>
                        <th>options</th>
                    </tr>
                    </thead>
                    <tbody>
                        <!-- 遍历employees -->
                        <tr th:each="emp:${employees}">
                            <td th:text="${emp.id}"></td>
                            <td th:text="${emp.name}"></td>
                            <td th:text="${emp.email}"></td>
                            <td th:text="${emp.getGender()}==1? '男':'女'"></td>
                            <td th:text="${emp.department.name}"></td>
                            <td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td>
                            <td>
                                <a class="btn btn-sm btn-info" th:href="@{/emp}+${emp.id}">修改</a>
                                <a class="btn btn-sm btn-danger delBtn" style="color: white">删除</a>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </main>
    </div>
</div>

<!--省略了js的引入-->

</body>

</html>

6.查询结果显示

在这里插入图片描述
Hibernate执行的sql语句:

Hibernate: select employee0_.id as id1_1_, employee0_.birth as birth2_1_, employee0_.department_id as departme6_1_, employee0_.email as email3_1_, employee0_.gender as gender4_1_, employee0_.name as name5_1_ from employee employee0_
Hibernate: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from department department0_ where department0_.id=?
Hibernate: select department0_.id as id1_0_0_, department0_.name as name2_0_0_ from department department0_ where department0_.id=?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值