gpt4 book ai didi

java - 如何让spring使用mysql而不是hsql

转载 作者:行者123 更新时间:2023-11-29 21:01:28 27 4
gpt4 key购买 nike

我正在遵循 Spring 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; bad SQL grammar [INSERT INTO people (first_name, last_name) VALUES (?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: 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("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?

关于java - 如何让spring使用mysql而不是hsql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37190061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com