【问题标题】:Spring Boot app configuration with DB2 database使用 DB2 数据库配置 Spring Boot 应用程序
【发布时间】:2020-01-15 12:35:25
【问题描述】:
能否请有经验的人分享有关使用 Spring Boot 应用程序配置 db2 数据库的建议?
创建一个 Spring Boot 应用程序,该应用程序将使用 JpaRepository 访问 db2 表,以使用 Thymeleaf 在 HTML 视图中呈现查询结果。
寻找有关如何配置将使用 Spring Data Jpa 访问 db2 表的 Spring Boot 应用程序的一般说明。具体来说,我的build.gradle 和application.properties 需要什么来实现这一点?
【问题讨论】:
标签:
html
spring-boot
spring-data-jpa
db2
thymeleaf
【解决方案1】:
我认为这可以帮助你我在简单的 poc 中使用它
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:db2://localhost:50000/EXAMPLE
spring.datasource.username=db2inst1
spring.datasource.password=db2inst1-pwd
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
spring.jpa.hibernate.ddl-auto=create-drop
你可以阅读这篇文章可能对你有帮助here
和here 我推荐你第二个
【解决方案2】:
要使用 Spring Boot 和嵌入式内存数据库,例如 H2,您只需将其依赖项添加到您的项目中:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>-->
</dependency>
例如,Spring Data JPA starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
然后 Spring Boot 将自行完成其余的自动配置。
因此您可以开始使用您的实体,例如:
@Entity
public class MyEntity {
@Id
@GeneratedValue
private Integer id;
// other stuff
}
和存储库:
public interface MyEntityRepo extends JpaRepository<MyEntity, Integer> {
}
附加信息: