使用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);
}
}