在springboot的application.properties中,配置各种数据库的方法:


mysql

  1. spring.jpa.database=MYSQL
  2. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  3. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  4. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydbname?useSSL=false
  5. spring.datasource.username=user1
  6. spring.datasource.password=user1

  1. <dependency>
  2.     <groupId>mysql</groupId>
  3.     <artifactId>mysql-connector-java</artifactId>
  4.     <version>5.1.34</version>
  5. </dependency>




oracle


  1. spring.jpa.database = oracle
  2. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
  3. spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
  4. spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:xe
  5. spring.datasource.username=user1
  6. spring.datasource.password=user1


  1. <dependency>
  2.     <groupId>com.oracle</groupId>
  3.     <artifactId>ojdbc7</artifactId>
  4.     <version>12.1.0.2.0</version>
  5. </dependency>





sqlserver


  1. spring.jpa.database = sql_server
  2. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServerDialect
  3. spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
  4. spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433;databaseName=mydbname
  5. spring.datasource.username=user1
  6. spring.datasource.password=user1

  1. <dependency>
  2.     <groupId>com.microsoft.sqlserver</groupId>
  3.     <artifactId>mssql-jdbc</artifactId>
  4.     <version>6.1.0.jre8</version>
  5. </dependency>





连接池设置

  1. spring.datasource.max-active=20
  2. spring.datasource.max-idle=8
  3. spring.datasource.min-idle=8
  4. spring.datasource.initial-size=10

  5. spring.jpa.show-sql=true

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

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


各种数据库对应的validationQuery

  1. # sqlserver
  2. spring.datasource.validationQuery=SELECT 1
  3. # oracle
  4. spring.datasource.validationQuery=SELECT 1 FROM DUAL
  5. # mysql
  6. spring.datasource.validationQuery=SELECT 1 FROM DUAL






12-08 12:23