1.数据表
1)employee表:
插入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=?