1. 程式人生 > >使用Redis+java(模仿資料庫)實現物件存取和讀取

使用Redis+java(模仿資料庫)實現物件存取和讀取

實現前要引入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);
        }
}