![](/img/trans.png)
[英]Joining two table entities in Spring Data JPA with where clause
[英]Joining Two Entities in Spring Data JPA
我在Mysql數據庫中有兩個表:Department和Contact。 我在apllication.properties文件中連接了我的應用程序。
這是我的數據庫:
pom.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
這是我的聯系方式:
@Entity
@Table(name="contact")
public class Contact {
@Id
@Column(name="contact_id")
private int Contact_id;
@Column(name="emp_name")
private String Emp_name;
@Column(name="mobile")
private String Mobile;
@Column(name="landline_office")
private String Landline_office;
@Column(name="landline_res")
private String Landline_res;
@Column(name="fax")
private String Fax;
@Column(name="email")
private String Email;
@ManyToOne(cascade= {CascadeType.PERSIST,CascadeType.MERGE,
CascadeType.DETACH,CascadeType.REFRESH})
@JoinColumn(name="department_dept_id")
private Department department;
... constructors and getters and setters
這是我的部門課程:
@Entity
@Table(name="department")
public class Department {
@Id
@Column(name="dept_id")
private int Dept_id;
@Column(name="dept_name")
private String Dept_name;
@Column(name="order")
private String Order;
@Column(name="home")
private int Home;
@OneToMany(mappedBy="department",
cascade= {CascadeType.PERSIST,CascadeType.MERGE,
CascadeType.DETACH,CascadeType.REFRESH})
private List<Contact> contacts;
public Department() {
}
...getters and setters and constructors
我可以使用百里香在表中顯示第一個實體:部門:
我想要做的是:當我單擊行1中的“查看”按鈕時,動態顯示所有屬於ICT的員工,對於PWD,則如此。
我已經在github中上傳了該項目: https : //github.com/sammizodev/Jpa_two_tables
這是您發布的代碼的代碼回顧:
命名約定 :您應該看一下Java的命名約定 ,類屬性應遵循駝峰式語法,忽略下划線的使用。
上面的內容不需要影響您的數據庫架構,因為您可以使用@Column
在表字段和類屬性之間進行映射,例如:
@Id
@Column(name="dept_id")
private int id;
@Column(name="dept_name")
private String name;
@Column(name="dept_order")
private String Order;
注意,order是許多數據庫中的關鍵字,因此您可能需要更改它。
寧靜的約定 :我建議您看一下寧靜的API設計 ,然后您可以了解如何構造應用程序以訪問某些資源。
根據約定,您只有一個資源(部門),因此需要以下URI:
例如,您具有GET /departments_list
來呈現您的部門列表,而您需要將其更改為GET /departments
並且您的模板應命名為list.html。
@GetMapping("/departments")
public String listDepartments(Model model) {
List<Department> departments = departmentService.findAll();
model.addAttribute("departments",departments);
return "/departments/list"; // Your current thymeleaf template
}
然后,您將需要GET /departments/{id}
來呈現部門詳細信息,包括聯系人列表。
因此,在部門列表模板上,您應該建立如下鏈接:
<a th:href="@{/home/contact/{departmentId}(departmentId=${tempDepartment.dept_id})}"
class="btn btn-info btn-sm">View</a>
注意,您需要提供/home/contact/{departmentId}
類的url,以便tymeleaf可以替換id屬性,否則將作為參數接收。
在您的控制器上,您需要更新到聯系人的映射,以將id包含為路徑變量:
@GetMapping("/departments/{id}")
public String listContacts(@PathVariable("id") int theId, Model theModel) {
Department department = departmentService.findById(theId);
theModel.addAttribute("department",department);
return "/departments/show";
}
如果您的Department類加載了渴望的聯系人,則可以在show.html
模板中訪問前端中的列表。
<tr th:each="contact : ${department.contacts}">
<td th:text="${contact.contact_id}" />
<td th:text="${contact.emp_name}" />
<td th:text="${contact.mobile}" />
<td th:text="${contact.landline_office}" />
</tr>
另外,請記住在DemoController
處DemoController
ContactService
。
public DemoController(DepartmentService departmentService, ContactService contactService) {
this.departmentService = departmentService;
this.contactService = contactService;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.