【问题标题】:Joining Two Entities in Spring Data JPA在 Spring Data JPA 中加入两个实体
【发布时间】:2019-03-22 10:32:30
【问题描述】:

Mysql 数据库中有两个表:Department 和 Contact。我在 appllication.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

我可以使用 thymeleaf 显示第一个实体:表中的部门:

我想要做的是:当我点击第 1 行中的查看按钮时,动态显示所有属于 ICT 的员工,对于 PWD 也是如此。

我已经在github上传了项目: https://github.com/sammizodev/Jpa_two_tables

【问题讨论】:

  • 非常感谢。但这不是我的作业。我对java真的很陌生,这就是全部。我试过用一张桌子写很多简单的应用程序。这只是一种爱好。

标签: java eclipse spring-boot jpa-2.0


【解决方案1】:

这是您发布的代码的代码审查:

命名约定:你应该看看java naming conventions,类属性应该遵循驼峰式语法,下划线的使用被忽略。

以上不需要影响您的数据库架构,因为您可以使用@Column 来做您的表字段和类属性之间的映射,例如:

@Id
@Column(name="dept_id")
private int id;

@Column(name="dept_name")
private String name;

@Column(name="dept_order")
private String Order;

注意,订单在很多数据库中都是关键字,所以你可能需要更改它。

Restful Conventions:我建议你看看restful API design,然后你就可以了解如何构造你的应用程序来访问某些资源。

根据惯例,你有一个资源(部门),你需要这些 URI:

  • URI: GET /department - /department/list.html - 渲染部门表
  • URI: GET /department/{id} - /department/show.html - 渲染一个部门的详细信息(联系表)。

例如,您有 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>

另外,请记住将ContactService 连接到您的DemoController

public DemoController(DepartmentService departmentService, ContactService contactService) {
        this.departmentService = departmentService;
        this.contactService = contactService;
    }

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 2013-11-27
    • 2016-10-10
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    相关资源
    最近更新 更多