Idea+Redis增删改查(根据给定的UserVO和UserDao,使用Jedis实现接口UserDao)

新建Maven项目,新建几个Java类,项目结构如下

redis

import java.util.List;
import java.util.Scanner;

/**
 * @author spondere
 * @version 1.8
 */
public class redis {
    public static void main(String[] args)throws Exception {
//        int a=10;
//        Scanner input=new Scanner(System.in);
//        while(a!=0){
//            a=input.nextInt();
//            switch (a){
//                case 1:
//                    Insert();
//                    System.out.println("Insert");
//                    break;
//                case 2:
//                    QueryAll();
//                    System.out.println("QueryAll");
//                    break;
//                case 3:
//                    QueryByName();
//                    System.out.println("QueryByName");
//                    break;
//                case 4:
//                    QueryById();
//                    System.out.println("QueryById");
//                    break;
//                case 5:
//                    Update();
//                    System.out.println("Update");
//                    break;
//                case 6:
//                    Delete();
//                    System.out.println("Delete");
//                    break;
//                case 0:
//                    break;
//                default :
//                    break;
//            }
//        }
        String u=null;
        Insert();
        System.out.println("Insert");
        QueryAll();
        System.out.println("QueryAll");
        u=QueryByName();
        System.out.println("QueryByName");
        QueryById(u);
        System.out.println("QueryById");
        Update(u);
        System.out.println("Update");
        Delete(u);
        System.out.println("Delete");

    }



    public static void Insert() throws Exception{

        UserDao userDao = (UserDao) new UserDaoRedisImpl();

        UserVO user1 = new UserVO();
        user1.setUserName("test1");
        user1.setVocation("testt1");
        user1.setPassword("111");
        userDao.insert(user1);

        UserVO user2 = new UserVO();
        user2.setUserName("test2");
        user2.setVocation("testt2");
        user2.setPassword("111");
        userDao.insert(user2);

        UserVO user3 = new UserVO();
        user3.setUserName("test3");
        user3.setVocation("testt3");
        user3.setPassword("111");
        userDao.insert(user3);


        UserVO user4 = new UserVO();
        user4.setUserName("test4");
        user4.setVocation("testt4");
        user4.setPassword("111");
        userDao.insert(user4);

    }


    public static void Delete(String u)throws Exception{
        UserDao userDao = (UserDao) new UserDaoRedisImpl();
        UserVO vo = userDao.queryById(u);
        userDao.delete(vo);
    }

    public static void Update(String u)throws Exception{
        UserDao userDao = (UserDao) new UserDaoRedisImpl();
        System.out.println(userDao);
        UserVO vo = userDao.queryById(u);
        System.out.println(vo);
        vo.setPassword("222");
        vo.setVocation("testt");
        userDao.update(vo);
    }

    public static String QueryByName()throws Exception{
        String userid = null;
        UserDao userDao = (UserDao) new UserDaoRedisImpl();
        List<UserVO> vos = userDao.queryByName("test4");
        for(UserVO vo : vos){
            System.out.println(vo);
            userid=vo.getUserId();
        }
//        System.out.println(userid);
        return userid;
    }

    public static void QueryAll()throws Exception{
        UserDao userDao = (UserDao) new UserDaoRedisImpl();
        List<UserVO> vos = userDao.queryAll();
        for(UserVO vo : vos){
            System.out.println(vo);
        }
    }

    public static void QueryById(String u)throws Exception{
        UserDao userDao = (UserDao) new UserDaoRedisImpl();
        UserVO vo = userDao.queryById(u);
        System.out.println(vo);
    }
}

UserDao

import java.util.List;

/**
 * @author spondere
 * @version 1.8
 */
public interface UserDao {
    public List<UserVO> queryAll () throws Exception;
    public List<UserVO> queryByName (String name) throws Exception;
    public UserVO queryById (String userId) throws Exception;
    public void insert(UserVO user) throws Exception;
    public void update(UserVO user) throws Exception;
    public void delete(UserVO user) throws Exception;
}

UserDaoRedisImpl

import redis.clients.jedis.Jedis;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author spondere
 * @version 1.8
 */
public class UserDaoRedisImpl implements UserDao {
    private Jedis openConnection()throws Exception{
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.auth("123");
        return jedis;
    }


    private String generateId(){
        return System.currentTimeMillis()+"";
    }

    @Override
    public List<UserVO> queryAll() throws Exception{
        // TODO Auto-generated method stub
        Jedis jedis = openConnection();

        String globalKey = UserVO.class.getName();
        long length = jedis.llen(globalKey);
        List<String> idlist = jedis.lrange(globalKey, 0, length - 1);

        List<UserVO> userlist = new ArrayList<>();
        for(String id : idlist){
            UserVO vo = queryById0(jedis,id);
            userlist.add(vo);
        }
        return userlist;
    }

    @Override
    public List<UserVO> queryByName(String name) throws Exception{
        // TODO Auto-generated method stub

        Jedis jedis = openConnection();

        long length = jedis.llen(name);
        List<String> idlist = jedis.lrange(name, 0, length - 1);

        List<UserVO> userlist = new ArrayList<>();
        for(String id : idlist){
            UserVO vo = queryById0(jedis,id);
            userlist.add(vo);
        }
        return userlist;
    }

    @Override
    public UserVO queryById(String userId) throws Exception{
        // TODO Auto-generated method stub
        Jedis jedis = openConnection();

        UserVO vo = queryById0(jedis,userId);

        jedis.close();

        return vo;
    }

    private UserVO queryById0(Jedis jedis,String userId){
        // TODO Auto-generated method stub
        Map<String,String> mapValue =jedis.hgetAll(userId);
        UserVO vo = mapToVO(mapValue);
        vo.setUserId(userId);
        return vo;
    }

    private UserVO mapToVO(Map<String,String> mapValue){
        UserVO vo = new UserVO();
        vo.setUserName(mapValue.get("userName"));
        vo.setPassword(mapValue.get("password"));
        vo.setVocation(mapValue.get("vocation"));
        return vo;

    }

    @Override
    public void insert(UserVO user) throws Exception{
        // TODO Auto-generated method stub
        user.setUserId("USER:"+generateId());

        Jedis jedis = openConnection();
        jedis.hset(user.getUserId(), "userName", user.getUserName());
        jedis.hset(user.getUserId(), "password", user.getPassword());
        jedis.hset(user.getUserId(), "vocation", user.getVocation());

        String globalKey = UserVO.class.getName();
        jedis.lpush(globalKey, user.getUserId());

        String nameKey = user.getUserName();
        jedis.lpush(nameKey, user.getUserId());

        jedis.close();
    }

    @Override
    public void update(UserVO user)throws Exception {
        // TODO Auto-generated method stub
        Jedis jedis = openConnection();
        jedis.hset(user.getUserId(), "password", user.getPassword());
        jedis.hset(user.getUserId(), "vocation", user.getVocation());

        String oldname = jedis.hget(user.getUserId(), "userName");
        String newname = user.getUserName();
        if(newname.equals(oldname)){
            jedis.lrem(oldname, 1, user.getUserId());
            jedis.lpush(newname, user.getUserId());
        }

        jedis.close();

    }

    @Override
    public void delete(UserVO user) throws Exception{
        // TODO Auto-generated method stub
        Jedis jedis = openConnection();

        String username = jedis.hget(user.getUserId(), "userName");
        jedis.lrem(username, 1, user.getUserId());

        String globalKey = UserVO.class.getName();
        jedis.lrem(globalKey,1 , user.getUserId());

        jedis.del(user.getUserId());

        jedis.close();

    }
}

UserVO

/**
 * @author spondere
 * @version 1.8
 */
public class UserVO {
    private String userName;
    private String password;
    private String userId;
    private String vocation;

    public String getVocation() {
        return vocation;
    }

    public void setVocation(String vocation) {
        this.vocation = vocation;
    }

    public String getUserName() {
        return userName;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "UserVO{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", userId='" + userId + '\'' +
                ", vocation='" + vocation + '\'' +
                '}';
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>RedistProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--    <verbal>true</verbal>-->
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>
            UTF-8
        </project.build.sourceEncoding>
    </properties>
</project>

运行结果:

Redis数据库:

最后修改:2021 年 05 月 11 日
如果觉得我的文章对你有用,请随意赞赏