码迷,mamicode.com
首页 > 数据库 > 详细

使用Redis+java(模仿数据库)实现对象存取和读取

时间:2018-12-25 13:17:42      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:shm   redis   depend   --   []   nts   false   util   override   

实现前要引入Redis架包

  maven项目

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

技术分享图片

 

一、创建类:User

package com.wbg.mr.entity;

public class User {
    String uid;
    String userName;
    String passWord;
    String name;

    public User() {
    }

    public User(String uid, String userName, String passWord, String name) {
        this.uid = uid;
        this.userName = userName;
        this.passWord = passWord;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=‘" + uid + ‘\‘‘ +
                ", userName=‘" + userName + ‘\‘‘ +
                ", passWord=‘" + passWord + ‘\‘‘ +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWrod() {
        return passWord;
    }

    public void setPassWrod(String passWord) {
        this.passWord = passWord;
    }

    public String getName() {
        return name;
    }

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

二、创建UserDao类

package com.wbg.mr.dao;

import com.wbg.mr.entity.User;
import redis.clients.jedis.Jedis;

import java.util.*;

public class UserDao {
    private static Jedis jedis;

    public UserDao(Jedis jedis) {
        this.jedis = jedis;
    }

    /**
     * 添加
     */
    public void addUser(User user) {
        //首先保存user-id
        jedis.sadd("useradd", "user-" + user.getUid());
        //-----添加数据----------
        Map<String, String> map = new HashMap<String, String>();
        map.put("uid", user.getUid());
        map.put("userName", user.getUserName());
        map.put("passWord", user.getPassWrod());
        map.put("name", user.getName());
        jedis.hmset("user-" + user.getUid(), map);
    }

    /**
     * 获取单个User
     *
     * @return
     */
    public List<String> getById(String id) {
        if (exists()) {
            return jedis.hmget("user-" + id, "id", "userName", "passWord", "name");
        }
        return null;
    }
    /**
     * 获取全部
     *
     * @return
     */
    public List<User> listAll() {
        List<User> list = new ArrayList<User>();
        User user = null;
        if (exists()) {
            for (String useradd : jedis.smembers("useradd")) {
                user = new User();
                List<String> lists = jedis.hmget(useradd, "id", "userName", "passWord", "name");
                user.setUid(lists.get(0));
                user.setUserName(lists.get(1));
                user.setPassWrod(lists.get(2));
                user.setName(lists.get(3));
                list.add(user);
            }
            return list;
        }
        return null;
    }

    //删除全部
    public boolean delAll() {
        if (exists()) {
            jedis.del("useradd");
            return true;
        }
        return false;
    }

    //判断是否存在
    public boolean exists() {
        return jedis.exists("useradd");
    }
}

三、测试

public class Main {
    private static Jedis jedis =null;
    public static void main(String[] args) {
        //连接本地的 Redis 服务
         jedis = new Jedis("localhost");
        System.out.println("连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行: "+jedis.ping());
        user();
    }
    public static void user(){
        UserDao user = new UserDao(jedis);
        user.delAll();
        user.addUser(new User("21","ldl","123456","刘地林"));
        user.addUser(new User("31","oyl","123456","欧一乐"));
        user.addUser(new User("41","tyj","123456","唐玉棋"));
        user.addUser(new User("51","cs","123456","陈胜"));
        user.addUser(new User("61","gsq","123456","郭世棋"));
        for (User user1 : user.listAll()) {
            System.out.println(user1);
        }
}

 

使用Redis+java(模仿数据库)实现对象存取和读取

标签:shm   redis   depend   --   []   nts   false   util   override   

原文地址:https://www.cnblogs.com/weibanggang/p/10173136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!