如何使用JDBC在Spring会话中初始化模式 [英] How to initialize schema in spring-session with JDBC
问题描述
我正在使用带有Mysql 5.7的Spring Boot 2.0.x,Hibernate和Spring Session Jdbc. 我在开发环境下工作,因此将Hibernate配置为每次都生成架构:
I'm using Spring Boot 2.0.x, Hibernate and Spring Session Jdbc with Mysql 5.7. I'm working under development environment, so Hibernate is configured to generate schemas every time:
spring.jpa.hibernate.ddl-auto=create-drop
它工作正常,但是我在Spring Session上遇到了问题...
我尝试设置了initialize-schema
,但是它不起作用.
And it works fine, but I have a problem with Spring Session...
I tried set initialize-schema
, but it doesn't work.
spring.session.jdbc.initialize-schema=always
是否可以自动生成完整的架构(所有实体和SPRING_SESSION)?
Is it possible to auto generate full schema (all entities and SPRING_SESSION)?
它不适用于MySQL和H2(我尝试了embedded
选项)
It doesn't work for me with MySQL and H2 (I tried embedded
option)
推荐答案
我正在描述这些步骤.它对我有用.
I am describing the steps.It worked for me.
1-将依赖性添加到pom文件中.
1- Add the dependency in your pom file.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
2-将具有相同路径和名称的sql文件添加到项目中的资源"文件下.
2-Add the sql file in your project with same path and same name Under "resources" file.
PATH:/org/springframework/session/jdbc/schema-mysql.sql
schema-mysql.sql
schema-mysql.sql
CREATE TABLE SPRING_SESSION (
PRIMARY_ID CHAR(36) NOT NULL,
SESSION_ID CHAR(36) NOT NULL,
CREATION_TIME BIGINT NOT NULL,
LAST_ACCESS_TIME BIGINT NOT NULL,
MAX_INACTIVE_INTERVAL INT NOT NULL,
EXPIRY_TIME BIGINT NOT NULL,
PRINCIPAL_NAME VARCHAR(100),
CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);
CREATE TABLE SPRING_SESSION_ATTRIBUTES (
SESSION_PRIMARY_ID CHAR(36) NOT NULL,
ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
ATTRIBUTE_BYTES BLOB NOT NULL,
CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
我从spring github官方帐户中获得了sql文件.
I got the sql file from official spring github account.
3-将以下属性添加到application.properties
3-Add below properties into application.properties
spring.session.jdbc.initialize-schema=always
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-mysql.sql
如果它不起作用了,请让我知道.就像我说的那样,它对我有用.
If it don't work again,please let me know.Like I said,It worked for me.
这篇关于如何使用JDBC在Spring会话中初始化模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!