Java多连接查询
我试图用SpringBoot在Java中创建一个REST服务 我有两个表主题和课程,我想从主题中检索名称和描述,从课程中检索价格。 这两个表之间的连接是使用课程中的主题ID建立的 结果必须是JSONJava多连接查询,java,spring,spring-boot,jpa,Java,Spring,Spring Boot,Jpa,我试图用SpringBoot在Java中创建一个REST服务 我有两个表主题和课程,我想从主题中检索名称和描述,从课程中检索价格。 这两个表之间的连接是使用课程中的主题ID建立的 结果必须是JSON [ { "id": "course1", "name": "name course1", "description": "course1" }, { "id": "course2", "name": "course2 name", "description": "course2" }, { "id"
[
{
"id": "course1",
"name": "name course1",
"description": "course1"
},
{
"id": "course2",
"name": "course2 name",
"description": "course2"
},
{
"id": "course3",
"name": "course3 name",
"description": "course3"
}
]
查询如下。
我知道使用DTO和JPA是可能的,但我需要为一个简单的数据库查询编写大量代码
多谢各位
package com.example.course;
import com.example.topic.Topic;
import javax.persistence.*;
@Entity
@Table(name = "topic", schema = "topic")
public class Topic {
@Id
private String id;
private String name;
private String description;
public Topic() {
}
public Topic(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public String getDescription() {
return description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.example.topic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "course", schema = "topic")
public class Course {
@Id
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
@Column(name="PRICE")
private Integer price;
@ManyToOne
@JoinColumn(name="TOPIC_ID", nullable=false)
private Topic topic;
public Course() {
}
public Course(String id, String name, String description, String topicId) {
this.id = id;
this.name = name;
this.description = description;
this.topic = new Topic(topicId, "", "");
}
public String getDescription() {
return description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setDescription(String description) {
this.description = description;
}
public Topic getTopic() {
return topic;
}
public void setTopic(Topic topic) {
this.topic = topic;
}
public void setPrice(Integer price) { this.price = price; }
public Integer getPrice() { return price; }
}
package com.example.dto;
public class TopicDescDTO {
private String id;
private String name;
private String description;
public TopicDescDTO(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public TopicDescDTO() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
select t.name, t.description, c.price as course_price
from topic.course c
inner join topic.topic t on t.id = c.topic_id
利用Spring Data JPA提供的投影界面:
public interface TopicDescDTO{
Integer getPrice();
String getName();
String getDescription();
}
然后在您的存储库中找到一个合适的方法:
@Query(select t.name, t.description, c.price
from Course c
inner join c.topic t)
List<TopicDescDTO> getTopicDescs();
Spring将为您进行映射。谢谢,这样更好,但我想知道是否有更简单的方法来实现这一点。没有新的接口和原生SQL。为什么这么简单的任务需要原生查询。在可能的情况下使用JPA并独立于数据库这只是一个只有2个表的简单示例。在我的项目中,我有更多的表联接。