一 redis7.x 整合redisTemplate
1.1 项目结构
1.2 配置操作
1.2.1 pom文件
<!--SpringBoot与Redis整合依赖-->
<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>
1.2.2 配置文件
spring:
redis:
database: 0
host: 192.168.152.141
port: 6379
password: 123456
timeout: 3000
lettuce:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
如图:
1.2.3 业务类
1.service
package com.ljf.redis.service;
public interface OrderService {
public String addOrder();
public String getOrderById(Integer keyId);
}
实现层
package com.ljf.redis.service.impl;
import com.ljf.redis.service.OrderService;
import io.netty.util.SuppressForbidden;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
/**
* @auther zzyy
* @create 2022-12-14 20:51
*/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService
{
public static final String ORDER_KEY = "ord:";
@Resource private RedisTemplate redisTemplate;
//@Resource private StringRedisTemplate StringRedisTemplate;
public String 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);
log.info("***key:{}",key);
log.info("***value:{}",value);
return "返回值:"+key+value;
}
public String getOrderById(Integer keyId)
{
return (String) redisTemplate.opsForValue().get(ORDER_KEY + keyId);
//return StringRedisTemplate.opsForValue().get(ORDER_KEY + keyId);
}
}
2.controller
@RestController
@Slf4j
public class OrderController {
@Resource
private OrderService orderService;
@RequestMapping(value = "/order/add",method = RequestMethod.GET)
public String addOrder()
{
return orderService.addOrder();
}
@RequestMapping(value = "/order/{keyId}",method = RequestMethod.GET)
public String getOrderById(@PathVariable Integer keyId)
{
return orderService.getOrderById(keyId);
}
}
1.2.4 序列化工具类
@Configuration
public class RedisConfig
{
/**
* redis序列化的工具配置类,下面这个请一定开启配置
* 127.0.0.1:6379> keys *
* 1) "ord:102" 序列化过
* 2) "\xac\xed\x00\x05t\x00\aord:102" 野生,没有序列化过
* this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
* this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法
* this.redisTemplate.opsForSet(); //提供了操作set的所有方法
* this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法
* this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法
* @param lettuceConnectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)
{
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//设置key序列化方式string
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
1.2.5 启动类
@SpringBootApplication
@MapperScan("com.ljf.redis.dao")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println("服务启动成功!!!!!!!");
}
}
1.2.6 测试类
1.添加
2.查询
3.服务端查询