Replace hsqldb with MySQL

2019-05-29 07:25发布

问题:

I have a Spring REST project configured with hsqldb. I would like to change it to MySQL. I have MySQL server installed and running, but I don't know how to modify this pom:

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>

PS.: Project im talking about comes as a source code for 'Spring REST' book: http://www.apress.com/9781484208243

source code download link:

http://www.apress.com/downloadable/download/sample/sample_id/1704/

回答1:

As far I see you are using Spring Boot on this, so you can easy change the databases changing the drivers dependencies from:

 <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
</dependency>

To

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

The driver version will be version on parent pom. Then specify the parameters on properties

spring.datasource.url=jdbc:mysql://localhost:port/yourdb
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # we can ommit this if we want, Spring Boot will deduce based on the classpath

For more configuration on databases you can see the properties available on appendix here