Tom*_*zyk 5 mysql jdbc spring-boot spring-session
我尝试运行此示例,但不使用Redis,而是使用我的本地MySQL服务器.
我编辑了这个春季启动应用程序,如下所示:
摇篮:
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
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
HttpSessionConfig.java
@EnableJdbcHttpSession // <1>
public class HttpSessionConfig {
}
Run Code Online (Sandbox Code Playgroud)
应用程序从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
Run Code Online (Sandbox Code Playgroud)
我不记得读过关于手动创建这个表的任何内容所以我认为spring会为我处理它...
编辑:我实际上尝试手动创建表,然后应用程序运行正常.但我想我不应该手动这样做.
Ved*_*vic 11
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
小智 10
At least as of now, you don't have to create tables manually.
In my test, tables were created automatically after adding the following line into the file application.properties when this file appears like shown above.
spring.session.jdbc.initialize-schema=always
I found this beautiful line from the following stackoverflow page.
很抱歉,我不知道是否有必要在 2016 年或 2017 年手动创建表。当我知道这一点或获得一些更有成效的相关信息时,我会更新这个答案。我只是希望没有人会想到使用 2019 年或更高版本的最新 Spring Framework 版本不可能自动创建会话表。
| 归档时间: |
|
| 查看次数: |
10030 次 |
| 最近记录: |