spring-boot结合spirng-data-jpa使用H2,Mysql,Oracle数据库的基本配置

  1. pom.xml

    4.0.0

    com.example
    my-springboot1
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.0.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            com.h2database
            h2
            1.4.197
        
        
            mysql
            mysql-connector-java
            6.0.6
        
        
            com.oracle
            ojdbc6
            11.2.0.4.0-atlassian-hosted
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  1. application.properties
server.port=9000
db=oracle
################JPA-DATA的配置############################
##此处不能使用${db}替换
spring.jpa.database-platform=${oracle.dialect}
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

################ DataSource的配置 ############################
spring.datasource.url=${${db}.url}
spring.datasource.driverClassName=${${db}.driverClassName}
spring.datasource.username=${${db}.username}
spring.datasource.password=${${db}.password}
spring.datasource.platform=${${db}.platform}

#打开h2的管理控制台(http://localhost:9000/h2-console)
spring.h2.console.enabled=true

########### h2 #####################################
#h2.url=jdbc:h2:mem:test
h2.url=jdbc:h2:file:./db/test01
#h2.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
h2.driverClassName=org.h2.Driver
h2.username=sa
h2.password=
h2.platform=h2
h2.dialect=org.hibernate.dialect.H2Dialect
########### mysql #####################################
mysql.url=jdbc:mysql://localhost:3306/test
mysql.driverClassName=com.mysql.cj.jdbc.Driver
mysql.username=root
mysql.password=123
mysql.platform=mysql
mysql.dialect=org.hibernate.dialect.MySQL57Dialect

########### oracle #####################################
oracle.url=jdbc:oracle:thin:@10.211.55.4:1521:xe
oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.username=john
oracle.password=123
oracle.platform=oracle
oralce.dialect=org.hibernate.dialect.Oracle10gDialect

#########库及数据的初始化####################################
#spring.datasource.schema=classpath:db/schema.sql
#spring.datasource.data=classpath:db/data.sql

  1. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    @Autowired private BookRepository br;
    @Test public void test1(){
        Book book=new Book();
        book.setBname("tom");
        br.save(book);
    }
}

你可能感兴趣的:(spring-boot结合spirng-data-jpa使用H2,Mysql,Oracle数据库的基本配置)