• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot hibernate 中各种数据库的连接设置

springboot 海叔叔 4年前 (2021-08-24) 30次浏览 已收录 0个评论

本文简单介绍springboot hibernate 中各种数据库的连接设置。在springboot的application.properties中,配置各种数据库的方法:

1、mysql

spring.jpa.database=MYSQL
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydbname?useSSL=false
spring.datasource.username=user1
spring.datasource.password=user1
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.34</version>
</dependency>

2、oracle

spring.jpa.database = oracle
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:xe
spring.datasource.username=user1
spring.datasource.password=user1
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2.0</version>
</dependency>

3、sqlserver

spring.jpa.database = sql_server
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServerDialect
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;databaseName=mydbname
spring.datasource.username=user1
spring.datasource.password=user1
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

4、连接池设置

spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update

# physical_naming_strategy 避免java属性映射到数据库字段时,驼峰转下划线
spring.jpa.properties.hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

各种数据库对应的validationQuery

# sqlserver
spring.datasource.validationQuery=SELECT 1
# oracle
spring.datasource.validationQuery=SELECT 1 FROM DUAL
# mysql
spring.datasource.validationQuery=SELECT 1 FROM DUAL

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot hibernate 中各种数据库的连接设置
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址