缓存配置
package com.project.web.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import java.time.Duration;
@Slf4j
@Configuration
@EnableCaching
public class CacheConfig {
CacheManager cacheManager(RedisConnectionFactory connectionFactory)
{
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter,defaultCacheConfig);
return redisCacheManager;
}
}
邮件配置
package com.project.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class MailConfig {
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("smtp");
javaMailSender.setHost("smtp.qq.com");
javaMailSender.setUsername("[email protected]");
javaMailSender.setPassword("xxxxxx");
javaMailSender.setPort(465);
return javaMailSender;
}
}
mybatis配置
package com.project.web.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.project")
public class MybatisConfig {
}
redis配置
package com.project.web.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
@Slf4j
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)
public class RedisConfig {
/**
*
* 解决hashmap显示问题
* @return
*/
// @Bean
// public RedisSerializer springSessionDefaultRedisSerializer() {
// // new Jackson2JsonRedisSerializer<>(Object.class);
// return new StringRedisSerializer();
// }
@Primary
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer( new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
@Bean
public CookieSerializer cookieSerializer(){
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
cookieSerializer.setCookiePath("/");
cookieSerializer.setCookieName("UserSessionID");
cookieSerializer.setUseHttpOnlyCookie(true);
return cookieSerializer;
}
}
启动项
package com.project.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.project")
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
server.port=80
#配置数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boolib
spring.datasource.username=root
spring.datasource.password=root
#配置输出sql语句
logging.level.com.project.dao=debug
#配置mybatis
mybatis.type-aliases-package=com.project.model
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapperLocations=classpath*:mapper/*.xml
spring.redis.host=182.61.38.81
spring.redis.port=6379
spring.redis.database=0
4.0.0
com.project
web
0.0.1-SNAPSHOT
jar
web
Demo project for Spring Boot
com.project
parent
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
com.project
service
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
com.project.web.WebApplication
ZIP
repackage
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/config/generatorConfig.xml
true
true