在Spring Data JPA中加入两个实体

问题描述 投票:0回答:1

我在Mysql数据库中有两个表:Department和Contact。我在apllication.properties文件中连接了我的应用程序。

这是我的数据库:

enter image description here

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

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

enter image description here

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

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

java eclipse spring-boot jpa-2.0
1个回答
1
投票

以下是您发布的代码的代码审查:

命名约定:你应该看一下java naming conventions,类属性应该遵循camel case语法,使用下划线是无视的。

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

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

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

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

请注意,order是许多数据库中的关键字,因此您可能需要更改它。

宁静的约定:我建议你看看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}这样的网址,以便thymeleaf可以替换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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.