使用SpringBoot和Spring JPA来查询具有多个列主键的表
我们将尝试在Spring-JPA中对具有多个列作为主键的表进行搜索处理。
1. 形成
sample-compositepk
│ build.gradle
└─src
├─main
├─java
│ └─com
│ └─stone
│ └─sample
│ SampleCompositePkApplication.java
│ SampleController.java
│ ServletInitializer.java
│ TCalendar.java
│ TCalendarPK.java
│ TCalendarRepository.java
└─resources
│ application.properties
│ hibernate.properties
│
├─static
└─templates
index.html
2. 依赖关系
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
3. 数据库连接配置
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=testuser
spring.datasource.password=testuser
spring.jpa.hibernate.ddl-auto=none
hibernate.jdbc.lob.non_contextual_creation = true
本次样本中,我们将使用PostgreSQL作为数据库。
三维数据库
create table tcalendar(
yyyymm int not null
,dd int not null
,plans character varying(128)
,primary key(yyyymm,dd)
)
作为示例,我将创建一个日程表。
主键使用年份和日期指定。
insert into public.tcalendar values
(201804,1,'1日目の予定')
,(201804,2,'2日目の予定')
,(201804,3,'3日目の予定')
,(201804,4,'4日目の予定')
,(201804,5,'5日目の予定')
,(201804,6,'6日目の予定')
,(201804,7,'7日目の予定')
,(201804,8,'8日目の予定')
,(201804,9,'9日目の予定')
,(201804,10,'10日目の予定')
,(201804,11,'11日目の予定')
,(201804,12,'12日目の予定')
,(201804,13,'13日目の予定')
,(201804,14,'14日目の予定')
,(201804,15,'15日目の予定')
既然准备工作已经完成,接下来我们要开始创建应用程序。
4. 实体
package com.stone.sample;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="tcalendar")
@IdClass(TCalendarPK.class)
public class TCalendar {
/** */
@Id
@Column (name="yyyymm")
private int yyyymm;
/** */
@Id
@Column (name="dd")
private int dd;
/** */
@Column (name="plans")
private String plans;
// setter,getter
}
在@IdClass中指定了定义了主键的类。
5. 组合主键类
package com.stone.sample;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class TCalendarPK implements Serializable {
@Column (name="yyyymm")
private int yyyymm;
@Column (name="dd")
private int dd;
//以下、省略
//setter, getter
//hashcode,equalsメソッド
}
在使用Eclipse时,您可以右键单击并选择源代码,然后自动创建setter、getter、hashcode和equals方法。
仓库。
package com.stone.sample;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface TCalendarRepository extends JpaRepository<TCalendar, TCalendarPK>{
@Query("select o from TCalendar o where o.yyyymm = :yyyymm and o.dd = :dd")
TCalendar findByCompositePrimaryKey(@Param("yyyymm")int yyyymm,@Param("dd")int dd);
}
7. 控制器
package com.stone.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SampleController {
@Autowired
TCalendarRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
TCalendar entity = repository.findByCompositePrimaryKey(201804, 5);
model.addAttribute("entity", entity);
return "index";
}
}
用中文原生方式解释以下内容,只需要一种选择:
8. HTML
答案: HTML 是一种标记语言。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta charset="utf-8" />
<style>
table {border: solid 1px #000000; border-collapse: collapse;}
</style>
</head>
<body>
<h1>Springboot JPA Sample(Composite primary key)</h1>
<table border="1">
<thead>
<tr>
<td>年月</td>
<td>日</td>
<td>予定</td>
</tr>
</thead>
<tbody>
<tr >
<td th:text="${entity.yyyymm}"></td>
<td th:text="*{entity.dd}"></td>
<td th:text="*{entity.plans}"></td>
</tr>
</tbody>
</table>
</body>
</html>
实现完成
9. 我会执行并确认一下。
对项目进行右键点击→选择”运行”→选择”Spring Boot 应用程序”。
访问http://localhost:8080/,
会显示以下的界面。

以上内容如下。 .)