Redis使用数据源操作 json对象之间转换

 
依赖
<dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
        <!--阿里巴巴提供的fastson解析器-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
    </dependencies>



代码
实体
package pojo;

import java.io.Serializable;

public class people implements Serializable {
    private int id;
    private String name;
    private String gender;
    private int age;

    public people(int id, String name, String gender, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "people{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}




代码测试
package test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.junit.Before;
import org.junit.Test;
import pojo.people;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

public class redis {
    /**
     * 数据池配置
     * 1数据池配置
     * 2设置条件
     * 3数据池   host   port timeout
     * 4获取源头
     */
    private Jedis jedis;
    @Test
    @Before
    public void init(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(50);
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxIdle(20);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,"114.116.11.98",6379,6000);
        this.jedis=jedisPool.getResource();
    }
    //对象转json

    /**
     * 对象转json
     * 1json.toJSONString
     * 2key   value
     * 打印
     */
    @Test
    public void redis01(){
        people people = new people(1,"张子辉","男",18);
        System.out.println(people);
        String peopleJson = JSON.toJSONString(people);
        System.out.println("转换成json的对象是"+peopleJson);
        jedis.set("people",peopleJson);
        String person = jedis.get("people");
        System.out.println(person);
        jedis.close();
    }
    //json转对象

    /**
     * 1:获取json
     * 2:json.parseobject
     * 3:关闭
     *
     */
    @Test
    public void redis02(){
        String people = jedis.get("people");
        System.out.println("这是json格式"+people);
        pojo.people people1 = JSON.parseObject(people, pojo.people.class);
        System.out.println("json转对象"+people1);
        jedis.close();
    }
    //集合转json  然后json集合转对象

    /**
     * 步骤
     * 1 获取json
     * 2 parse对象  参数  pojo 工具类
     * 3 遍历输出
     * 4 关闭
     */
    @Test
    public void redis03(){
    //对象转成json
        String people3 = jedis.get("people");
        System.out.println("json格式的对象"+people3);
        List<pojo.people> people1 = JSON.parseObject(people3, new TypeReference<List<people>>(){

        });
        for (pojo.people people2 : people1){
            System.out.println(people2);
        }
        System.out.println("转成对象的list json");
        jedis.close();


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值