mysql - 具有 JDBC 配置的 Spring-Session : Table 'test.spring_session' doesn't exist

标签 mysql jdbc spring-boot spring-session

我尝试运行 this example但不使用 Redis,而是使用我本地的 MySQL 服务器。

我已经像这样编辑了这个 spring boot 应用程序:

Gradle :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
    }
}

apply plugin: 'spring-boot'

apply from: JAVA_GRADLE


//this 'if' statement is because I was getting error: Execution failed for task ':samples:findbyusername:findMainClass'.
//> Could not find property 'main' on task ':samples:findbyusername:run'.
if (!hasProperty('mainClass')) {
    ext.mainClass = 'sample.FindByUsernameApplication'
}

tasks.findByPath("artifactoryPublish")?.enabled = false

group = 'samples'

dependencies {
    compile("org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion") 
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.2'
    compile group: 'org.springframework.session', name: 'spring-session', version: '1.2.0.RELEASE'



    compile project(':spring-session'),
            "org.springframework.boot:spring-boot-starter-web",
            "org.springframework.boot:spring-boot-starter-thymeleaf",
            "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect",
            "org.springframework.security:spring-security-web:$springSecurityVersion",
            "org.springframework.security:spring-security-config:$springSecurityVersion",
            "com.maxmind.geoip2:geoip2:2.3.1",
            "org.apache.httpcomponents:httpclient"

    testCompile "org.springframework.boot:spring-boot-starter-test",
                "org.assertj:assertj-core:$assertjVersion"

    integrationTestCompile gebDependencies,
            "org.spockframework:spock-spring:$spockVersion"

}



def reservePort() {
    def socket = new ServerSocket(0)
    def result = socket.localPort
    socket.close()
    result
}

application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/TEST?characterEncoding=UTF-8&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=but

spring.thymeleaf.cache=false
spring.template.cache=false

HttpSessionConfig.java

@EnableJdbcHttpSession // <1>
public class HttpSessionConfig {
}

应用程序在 tomcat 上启动,但是当我在浏览器中点击 localhost 时,我得到:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 23 21:14:31 CEST 2016
There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; bad SQL grammar [INSERT INTO SPRING_SESSION(SESSION_ID, CREATION_TIME, LAST_ACCESS_TIME, MAX_INACTIVE_INTERVAL, PRINCIPAL_NAME) VALUES (?, ?, ?, ?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: Table 'test.spring_session' doesn't exist

我不记得读过任何有关手动创建此表的内容,所以我认为 spring 会为我处理它...

编辑: 我实际上尝试过manually create tables然后应用程序运行正常。但我想我不应该手动执行此操作。

最佳答案

Spring Session 附带了大多数主要 RDBMS 的数据库模式脚本(位于 org.springframework.session.jdbc 包中),但是需要注意为 Spring Session JDBC 支持创建数据库表由用户自己决定。

提供的脚本可以原封不动地使用,但是一些用户可能会选择修改它们以满足他们的特定需求,使用提供的脚本作为引用。

一种选择是使用数据库迁移工具(例如 Flyway)来处理数据库表的创建。

由于您使用的是 Spring Boot,您可能会感兴趣有一个待定的 PR 来添加对 Spring Session JDBC 模式自动初始化的支持:https://github.com/spring-projects/spring-boot/pull/5879

如果文档误导您认为表应该由 Spring Session 本身自动创建,请考虑报告问题,以便我们在必要时更新文档:https://github.com/spring-projects/spring-session/issues

关于mysql - 具有 JDBC 配置的 Spring-Session : Table 'test.spring_session' doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398905/

相关文章:

mysql - mysql连接查询的通用字段

mysql - 如何在准备语句插入操作期间插入自动增量字段的下一个值?

java - 使用 Spring JDBC 轻松事务?

Spring 启动: GroupId and ArtifactId based on a property dynamically

java - Maven 在 Eclipse 中找不到 Artifact 命令 spring-boot-starter-parent

mysql - Joomla - 使用单个查询插入多行

mysql - SQL 每个 FieldID 仅返回 1 行

java - 打开 Office 数据库错误(运行时显示异常)

java - 避免 OLTP 上的 Spring Batch 作业

mysql - 使用flume将数据从mySQL复制到Hbase : how?