java - 如果仅使用 GET,则无法读取 Redis 值

标签 java spring redis

在下面的代码片段中,我无法从 redis 中读取 (GET) 值。

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringContext.xml");

   User user = new User(); // java Bean

 RegistrationBean bean = (RegistrationBean) applicationContext.getBean("RegistrationBean");

         User user = new User();
        user.setUserid("200112021");
        user.setName("name1");
        user.setAddress("adress sd");
bean.getRedisTemplate().opsForHash().put("A12", user.hashCode(),user);


User x = (User)bean.getRedisTemplate().opsForHash().get("A12",user.hashCode());
System.out.println(x.getName());

以上代码仅在同时使用 PUT 和 GET 时运行,但如果不使用 PUT 并且仅对特定记录使用 GET 那么我得到 NULL 值请建议?

谢谢 维杰

最佳答案

问题已解决....

我使用了 Spring MVC API。以下是代码片段

public String addcustomer(@ModelAttribute("customer")customerVO customer, ModelMap model) {//从 UI 获取客户数据

    model.addAttribute("QuestionName", customer.getQuestionName());
    model.addAttribute("comments", customer.getComments());
    model.addAttribute("pollID", customer.getPollID());
    model.addAttribute("answer", customer.getAnswers());

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("servletDispatcher-servlet.xml");


  CustomerRepository customerRepository = (CustomerRepository) applicationContext.getBean("customerrepository");

    customerRepository.put(customer);
    System.out.println(" Step 1 output : " + customerRepository.getObjects());

关于java - 如果仅使用 GET,则无法读取 Redis 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353690/

相关文章:

javascript - Spring No 'Access-Control-Allow-Origin' 且登录页面没有显式映射/错误

spring - Thymeleaf:来自 messageKey 的默认消息

java - Spring Batch SplitBuilder 不执行启动流程

redis - 在 Windows Server 2012 上安装和运行 Redis 的最可靠方法是什么? (2018 年更新)

node.js - 如何轻松包装整个 (Redis) 客户端

java - 从选择 ul 获取文本

c# - 我可以执行一个try block 对应的多个catch block 吗?

java - PrintWriter 到多个文件

python - Redis:获取所有哈希值的最佳方式

java - 如何在 Java 中制作 "Pixelated"行