[英]How to make spring use mysql instead of hsql
我正在關注春季https://spring.io/guides/gs/batch-processing/的本指南。
只要我使用hsql,一切正常,但我想切換到mysql。 問題是spring總是試圖啟動並使用hsql實例。
以下是代碼:
hello.ressources中的application.properties:
spring.datasource.url=jdbc:mysql://xxx:3306/xxx
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
hello.ressources中的datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxx:3306/xxx" />
<property name="username" value="user" />
<property name="password" value="pass" />
</bean>
</beans>
在我的BatchConfiguration類中,有一個@Autowired
屬性
@Autowired
public DataSource dataSource;
Intelij將其顯示為連接到datasource.xml
。
但是,當我運行應用程序時,它首先記錄
o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
哪個不應該出現然后拋出錯誤說:
PreparedStatementCallback; 錯誤的SQL語法[INSERT INTO people(first_name,last_name)VALUES(?,?)]; 嵌套異常是java.sql.SQLSyntaxErrorException:user缺少未找到的權限或對象:PEOPLE
我已將schema-all.sql更改為schema-mysql.sql並將代碼更改為
DROP TABLE IF EXISTS people;
CREATE TABLE people (
person_id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT ,
first_name VARCHAR(20),
last_name VARCHAR(20)
);
在我的build.gradle中,我添加了
classpath 'mysql:mysql-connector-java:5.1.6'
構建依賴項並將hsql部分從compile deps更改為
compile("mysql:mysql-connector-java:5.1.6")
我怎么能在這里使用mysql並禁用hsql?
編輯:
項目結構: http : //imgur.com/HEuiw9W
我刪除了datasource.xml。
這是build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
classpath 'mysql:mysql-connector-java:5.1.6'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-batch-processing'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-batch")
compile("mysql:mysql-connector-java:5.1.6")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
你采取的步驟應該足以使spring-boot使用mysql。
我看到你正在使用spring-batch,它還附帶了一個hsql依賴項。 如果可能有第二個數據源初始化,你有沒有檢查過日志? 可能是你看到的hsql init是spring-batch試圖為其元數據設置數據源。
也
你試過添加嗎?
spring.datasource.platform=mysql
到application.properties?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.