package com.atguigu.boot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig
{
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
package com.atguigu.boot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig
{
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>boot3-01</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 所有SpringBoot都必须继承-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</parent>
<dependencies>
<!-- redis相关场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入redis-template-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- 引入swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 引入junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
</dependency>
<!-- 引入lettuce-->
<!-- <dependency>-->
<!-- <groupId>io.lettuce</groupId>-->
<!-- <artifactId>lettuce-core</artifactId>-->
<!-- <version>6.2.6.RELEASE</version>-->
<!-- </dependency>-->
</dependencies>
<!-- SpringBoot打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>boot3-01</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 所有SpringBoot都必须继承-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</parent>
<dependencies>
<!-- redis相关场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入redis-template-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- 引入swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 引入junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
</dependency>
<!-- 引入lettuce-->
<!-- <dependency>-->
<!-- <groupId>io.lettuce</groupId>-->
<!-- <artifactId>lettuce-core</artifactId>-->
<!-- <version>6.2.6.RELEASE</version>-->
<!-- </dependency>-->
</dependencies>
<!-- SpringBoot打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.atguigu.boot.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${spring.swagger2.enable}")
private Boolean enable;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("com.atguigu.boot"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springBoot利用Swagger2构建api接口文档"+"\t"
+ DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()))
.description("springBoot+Redis整合")
.version("1.0")
.termsOfServiceUrl("https://blog.csdn.net/2201_75960169")
.build();
}
}
package com.atguigu.boot.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${spring.swagger2.enable}")
private Boolean enable;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("com.atguigu.boot"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springBoot利用Swagger2构建api接口文档"+"\t"
+ DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()))
.description("springBoot+Redis整合")
.version("1.0")
.termsOfServiceUrl("https://blog.csdn.net/2201_75960169")
.build();
}
}
server.port=7777
spring.application.name=redis7_study
logging.level.root=info
logging.level.com.atguigu.boot=info
logging.pattern.console=%d{yyyy-MM-dd HH:mm.SSS} [%thread] %-5level %logger- %msg%n
logging.file.name=C:/myLog/redis_study.log
logging.pattern.file=%d{yyyy-MM-dd HH:mm.SSS} [%thread] %-5level %logger- %msg%n
spring.swagger2.enable=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
#连接几号库
spring.redis.database=0
spring.redis.host=192.168.92.129
spring.redis.port=6379
spring.redis.password=abc123
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=5000
server.port=7777
spring.application.name=redis7_study
logging.level.root=info
logging.level.com.atguigu.boot=info
logging.pattern.console=%d{yyyy-MM-dd HH:mm.SSS} [%thread] %-5level %logger- %msg%n
logging.file.name=C:/myLog/redis_study.log
logging.pattern.file=%d{yyyy-MM-dd HH:mm.SSS} [%thread] %-5level %logger- %msg%n
spring.swagger2.enable=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
#连接几号库
spring.redis.database=0
spring.redis.host=192.168.92.129
spring.redis.port=6379
spring.redis.password=abc123
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=5000
package com.atguigu.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
// 启动项目的主入口
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
package com.atguigu.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
// 启动项目的主入口
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
package com.atguigu.boot.service;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@Service
public class OrderService {
public static final String ORDER_KEY = "ord:";
@Resource
private RedisTemplate redisTemplate;
// 支持正常序列化的子类,因为默认情况下,spring和redis采用的序列化方式不一样
// private StringRedisTemplate stringRedisTemplate;
public void addOrder(){
int keyId = ThreadLocalRandom.current().nextInt(1000)+1;
String serialNo = UUID.randomUUID().toString();
String key = ORDER_KEY + keyId;
String value = "京东订单" + serialNo;
redisTemplate.opsForValue().set(key,value);
// stringRedisTemplate.opsForValue().set(key,value);
System.out.println(key);
System.out.println(value);
}
public String getOrderById(Integer keyId){
Object o = redisTemplate.opsForValue().get(ORDER_KEY + keyId);
// Object o = stringRedisTemplate.opsForValue().get(ORDER_KEY + keyId);
return (String) o;
}
}
package com.atguigu.boot.service;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@Service
public class OrderService {
public static final String ORDER_KEY = "ord:";
@Resource
private RedisTemplate redisTemplate;
// 支持正常序列化的子类,因为默认情况下,spring和redis采用的序列化方式不一样
// private StringRedisTemplate stringRedisTemplate;
public void addOrder(){
int keyId = ThreadLocalRandom.current().nextInt(1000)+1;
String serialNo = UUID.randomUUID().toString();
String key = ORDER_KEY + keyId;
String value = "京东订单" + serialNo;
redisTemplate.opsForValue().set(key,value);
// stringRedisTemplate.opsForValue().set(key,value);
System.out.println(key);
System.out.println(value);
}
public String getOrderById(Integer keyId){
Object o = redisTemplate.opsForValue().get(ORDER_KEY + keyId);
// Object o = stringRedisTemplate.opsForValue().get(ORDER_KEY + keyId);
return (String) o;
}
}
package com.atguigu.boot.controller;
import com.atguigu.boot.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "订单接口")
public class OrderController {
@Resource
private OrderService orderService;
@ApiOperation("新增订单接口")
@RequestMapping(value = "/order/add",method = RequestMethod.POST)
public void addOrder(){
orderService.addOrder();
}
@ApiOperation("根据keyID获取订单信息")
@RequestMapping(value = "/order/{keyID}",method = RequestMethod.GET)
public String getOrder(@PathVariable Integer keyID){
return orderService.getOrderById(keyID);
}
}
package com.atguigu.boot.controller;
import com.atguigu.boot.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "订单接口")
public class OrderController {
@Resource
private OrderService orderService;
@ApiOperation("新增订单接口")
@RequestMapping(value = "/order/add",method = RequestMethod.POST)
public void addOrder(){
orderService.addOrder();
}
@ApiOperation("根据keyID获取订单信息")
@RequestMapping(value = "/order/{keyID}",method = RequestMethod.GET)
public String getOrder(@PathVariable Integer keyID){
return orderService.getOrderById(keyID);
}
}