无法使用RedisTemplate类在SpringBoot中获取数据

时间:2018-12-27 20:22:39

标签: spring spring-boot redis spring-data spring-data-redis

模型如下:-

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

public class User {

    private static final long serialVersionUID = 7156526077883281L;

    @Id
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "SEQ_USER", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
    private long id;
    private String name;
    private long followers;

    public String getName() {
        return name;
    }

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

    public long getFollowers() {
        return followers;
    }

    public void setFollowers(long followers) {
        this.followers = followers;
    }

    public User(String name, long followers) {

        this.name = name;
        this.followers = followers;
    }

    @Override
    public String toString() {
        return String.format("User{id=%d, name='%s', followers=%d}", id, name, followers);
    }
}

DAO如下:-

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDAO {
    private static final String KEY = "userKey";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void addFollower(String uname) {
        redisTemplate.opsForList().leftPush(KEY, uname);
    }
    /*public void updateFollower(String followername) {
        redisTemplate.opsForValue().set(KEY, followername);
    }*/
   public String getFollowers() {
        return redisTemplate.opsForValue().get(KEY);
    }
    public void removeFollower(String uname) {
        redisTemplate.opsForList().leftPop(KEY);

    }
    public void deleteUser() {
        redisTemplate.delete(KEY);
    }
}

SpringBoot Application类如下:-

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private UserDAO userDAO;
    Logger logger=LoggerFactory.getLogger("Logger");

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        User user =new User("Rita Ora",30);

        userDAO.addFollower("katy Perry");
        userDAO.addFollower("Swift");
        userDAO.addFollower("Bebe Rexha");

        userDAO.removeFollower("Swift");

        logger.info("Info",userDAO.getFollowers());
    }

}

我正在REDIS Client中使用列表数据结构来获取数据。 我收到的错误如下:-

org.springframework.dao.InvalidDataAccessApiUsageException:WRONGTYPE对持有错误类型值的键进行操作;嵌套异常是redis.clients.jedis.exceptions.JedisDataException:WRONGTYPE对持有错误类型值的键进行的操作     在org.springframework.data.redis.connection.jedis.JedisExceptionC

0 个答案:

没有答案