Hibernate的各种数据库配置以及常用配置项说明

一、Hibernate常用数据库简单配置

1、Mysql数据库配置

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=CTT
spring.datasource.username = username
spring.datasource.password = password

2、SQLServer数据库配置

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.driver-class-name = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url = jdbc:sqlserver://localhost:1433;DatabaseName=dbname
spring.datasource.username = username
spring.datasource.password = password

3、Oracle数据库配置

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@localhost:1521:dbname
spring.datasource.username = username
spring.datasource.password = password

二、配置项说明(以Mysql为例)

spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=CTT
spring.datasource.username = username
spring.datasource.password = password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

1、spring.jpa.hibernate.ddl-auto,根据实体类自动生成数据库表和字段,一般选用update

create-----启动时删数据库中的表,然后创建,退出时不删除数据表 
create-drop-----启动时删数据库中的表,然后创建,退出时删除数据表 如果表不存在报错 
update-----如果启动时表格式不一致则更新表,原有数据保留 
validate-----项目启动表结构进行校验 如果不一致则报错

2、spring.jpa.properties.hibernate.dialect,数据库方言,各数据库不同,可根据实际数据库版本调整,如mysql5.7

3、spring.jpa.hibernate.naming.physical-strategy,数据库命名策略

在hibernate5之后,采用implicit-strategy和physical-strategy分别控制命名策略,具体参考我的另一篇博文Hibernate数据库配置项中命名策略的简单说明

4、spring.datasource.driver-class-name,指定JDBC驱动程序

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值