【问题标题】:How to make spring use mysql instead of hsql如何让spring使用mysql而不是hsql
【发布时间】:2016-09-08 11:21:59
【问题描述】:

我从春天开始关注这个指南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:用户缺少权限或找不到对象:人

我已将 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'
}

【问题讨论】:

  • @Jarrodroberson 你投票结束的问题是关于缺课的,我的不是。
  • Imo,这个问题不是链接问题的重复。当然,他们都会从 HSQL 切换到 MySQL,但这是一个 Spring Boot 项目,并且 Boot 默认配置一个嵌入式 HSQL 作为其数据库。这个问题可能有重复,但链接的问题绝对不是那个
  • 你知道如何解决这个@AliDehghani
  • 对于初学者,请删除datasource.xml。你把你的application.properties放在哪里了?发布您的项目结构和build.gradle
  • @AliDehghani 编辑了它

标签: java mysql spring spring-mvc spring-boot


【解决方案1】:

你采取的步骤应该足以让 spring-boot 使用 mysql。

我看到你正在使用 spring-batch,它还带有一个 hsql 依赖项。如果可能有第二个数据源初始化,你检查过日志吗?可能是您看到的 hsql init 是 spring-batch 尝试为其元数据设置数据源。

还有

你有没有尝试添加

spring.datasource.platform=mysql

到 application.properties?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2015-03-17
    • 2019-06-12
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多