Springboot 2.1.1.RELEASE 版本 session保存到MySQL里面
1,pom.xml添加依赖:
<!-- 数据库session管理 --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-jdbc</artifactId> </dependency>
2,application.properties添加配置:
spring.session.store-type=jdbc
3,数据库里创建两张表:
DROP TABLE IF EXISTS `SPRING_SESSION`;
CREATE TABLE `SPRING_SESSION` (
`PRIMARY_ID` char(36) NOT NULL DEFAULT '',
`SESSION_ID` char(36) NOT NULL DEFAULT '',
`CREATION_TIME` bigint(20) NOT NULL,
`LAST_ACCESS_TIME` bigint(20) NOT NULL,
`MAX_INACTIVE_INTERVAL` int(11) NOT NULL,
`PRINCIPAL_NAME` varchar(100) DEFAULT NULL,
`EXPIRY_TIME` bigint(20) DEFAULT NULL,
PRIMARY KEY (`PRIMARY_ID`) USING BTREE,
KEY `SPRING_SESSION_IX1` (`LAST_ACCESS_TIME`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `SPRING_SESSION_ATTRIBUTES`;
CREATE TABLE `SPRING_SESSION_ATTRIBUTES` (
`SESSION_PRIMARY_ID` char(36) NOT NULL DEFAULT '',
`ATTRIBUTE_NAME` varchar(100) NOT NULL DEFAULT '',
`ATTRIBUTE_BYTES` blob,
KEY `SPRING_SESSION_ATTRIBUTES_IX1` (`SESSION_PRIMARY_ID`) USING BTREE,
CONSTRAINT `SPRING_SESSION_ATTRIBUTES_ibfk_1` FOREIGN KEY (`SESSION_PRIMARY_ID`) REFERENCES `SPRING_SESSION` (`PRIMARY_ID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;