Redis数据库整合

Redis使用

1.查询数据,
  1.1连接redis,Int变为String
    Jedis jedis=new Jedis(host, Integer.parseInt(port));
  1.2 查询出来,String转Item实体类
    ObjectMapper objectMapper=new ObjectMapper();
    Item item=objectMapper.readValue(json,Item.class);
2.插入数据
          //连接redis
           Jedis jedis=new Jedis(host,Integer.parseInt(port));
            //用jackson把对象转成字符串
            ObjectMapper objectMapper=new ObjectMapper();
            String json=objectMapper.writeValueAsString(item);
            //2.4 调用set()把字符串保存到redis
            String result=jedis.set(String.valueOf(itemId),json);
Redis基于linux,必须转化数据
	 Jedis jedis=new Jedis(host,Integer.parseInt(port));
	 //读取数据,传进来int型,redis识别必须转化
      String json=jedis.get(String.valueOf(itemId));
      //如果有
      if(StringUtils.isEmpty(json)==false){
            //转化,json转化,实例化一个ObjectMapper
            ObjectMapper objectMapper=new ObjectMapper();
            //读取成功,转化为Item实体类
            Item item=objectMapper.readValue(json,Item.class);
            jedis.close();
            return item;
        }
package com.tedu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tedu.mapper.ItemMapper;
import com.tedu.pojo.Item;
import com.tedu.pojo.ItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.List;

@RestController
public class ItemController {
    @Value("${redis.host}")
    String host;
    @Value("${redis.port}")
    String port;
    @Autowired
    ItemMapper itemMapper;

    //查数据
    @RequestMapping("/selectById")
    public Item get(Integer itemId) throws Throwable{
        //先从redis中查数据,查不到,在去数据库里
        Jedis jedis=new Jedis(host, Integer.parseInt(port));
        String json=jedis.get(String.valueOf(itemId));
        if(StringUtils.isEmpty(json)==false){
            //转化
            ObjectMapper objectMapper=new ObjectMapper();
            Item item=objectMapper.readValue(json,Item.class);
            jedis.close();
            return item;
        }
        ItemExample itemExample=new ItemExample();
        ItemExample.Criteria criteria = itemExample.or();
        criteria.andItemIdEqualTo(itemId);
        List<Item> itemList= itemMapper.selectByExample(itemExample);
        return itemList.get(0);
    }

    @RequestMapping("/item/insert")
    public String insert(Integer itemId) throws  Throwable{
        //1,从mysql中查询商品
        //1.1 创建example生成where orderby
        ItemExample itemExample=new ItemExample();
        //1.2 创建criteria生成where itemId=1
        ItemExample.Criteria criteria=itemExample.or();
        criteria.andItemIdEqualTo(itemId);
        //1.3使用接口的代理对象执行sql
        List<Item> itemList=itemMapper.selectByExample(itemExample);
        //1.4从集合中取到一件商品
        Item item=itemList.get(0);
        //2,把商品保存到redis中
        //2.1判断有没有取到商品
        if (item!=null) {
            //2.2连接redis
            Jedis jedis=new Jedis(host,Integer.parseInt(port));
            //2.3 用jackson把对象转成字符串
            ObjectMapper objectMapper=new ObjectMapper();
            String json=objectMapper.writeValueAsString(item);
            //2.4 调用set()把字符串保存到redis
            String result=jedis.set(String.valueOf(itemId),json);
            //2.5 关闭连接
            jedis.close();
            return result;
        }
        return "failure";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值