2022-02-11 16:35

SpringBoot集成Redis

少尉

JavaEE

(757)

(0)

收藏

一、首先,启动Redis

image.png 

二、新建一个Springboot项目

image.png 

勾选常用的依赖,然后点击Finish

 image.png

三、在配置文件application.properties中添加redis和Myssql连接配置

image.png

四、在测试类中添加下面的代码:

@SpringBootTest class RedisDemoApplicationTests {
  @Autowired
  private RedisTemplate redisTemplate;
  @Test
  void contextLoads() {
    redisTemplate.opsForValue().set("testKey","testValue");
    System.out.println(redisTemplate.opsForValue().get("testKey"));
    
     }
}

五、运行测试方法,就会在控制台输出:

image.png 

上面我们往Redis里面存放了一个字符串,下面我们看看如何从数据库中查询一个对象放到Redis中。

一、在MySQL中新建一个表,并添加测试数据。

image.png 

image.png 

二、Redis工具包类

@Component
public class RedisUtils {
@Resource
RedisTemplate redisTemplate;   
//key-value是对象的
        
//判断是否存在key
        
public boolean hasKey(String key){
    return redisTemplate.hasKey(key);
        
}
        
//从redis中获取值
        
public Object get(String key){
     return  redisTemplate.opsForValue().get(key);
        
}
        
//向redis插入值
        
public boolean set(final String key,Object value){
   boolean result = false;
   try{
     redisTemplate.opsForValue().set(key,value);               
          result = true;
            
      }
      catch (Exception e){                
          e.printStackTrace();
            
      }
   return result;
        
    }
}

三、vo类ProductType

productrType需要实现序列化的接口Serializable

public class ProductType implements Serializable {
    private Integer id;
    private String title;
    private Integer sort;
    private Boolean enable;
    public Integer getId() {
        return id;
    
}
    public void setId(Integer id) {
        this.id = id;
    
}
    public String getTitle() {
        return title;
    
}
    public void setTitle(String title) {
        this.title = title;
    
}
    public Integer getSort() {
        return sort;
    
}
    public void setSort(Integer sort) {
        this.sort = sort;
    
}
    public Boolean getEnable() {
        return enable;
    
}
    public void setEnable(Boolean enable) {
        this.enable = enable;
    
}
}

四、ProductTypeMapper类

使用注解的方法操作数据库

@Mapper
public interface ProductTypeMapper {
    @Select("select * from product_type where id=#{id}")
    ProductType findById(int id);
}

五、service包

ProductTypeService接口:

public interface ProductTypeService {
    ProductType findById(int id);
}

接口实现类 ProductTypeServiceImpl:

第一次从数据库查询数据,查询出来之后就放到Redis,后面查询都从Redis查询。减少数据库访问的压力。

@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private RedisUtils redisUtils;
    @Override
    public ProductType findById(int id) {
        ProductType productType;
        String key="productType"+id;
        if(redisUtils.hasKey(key)){
            productType=(ProductType) redisUtils.get(key);
            System.out.println("查询的是缓存");
        
}
        else{
            productType=productTypeMapper.findById(id);
            redisUtils.set(key,productType);
            System.out.println("查询的是数据库");
        
}
        return productType;
    
}
}

六、控制器 ProductTypeController

@RestController
@RequestMapping("/productType")
public class ProductTypeController {
    @Resource
    private ProductTypeService productTypeService;
    @RequestMapping("get/{id}")
    public ProductType get(@PathVariable("id") Integer id){
        ProductType productType=productTypeService.findById(id);
        return productType;
    
}
}

启动运行,可以发现,第一次查询的是数据库,后面查询的都是缓存

image.png

0条评论

点击登录参与评论