TXT__ 2020-05-12 09:09 采纳率: 0%
浏览 406

在spring框架里把数据存到Redis中失败了,但是写个小demo又是成功的

这个是运行的代码

@Autowired
        private RedisTemplate<String,Object> redisTemplate;

        @Override
        public List<TbContent> findByCategoryId(Long categoryId) {
            //首先从redis中取数据
            List<TbContent> list = (List<TbContent>)redisTemplate.boundHashOps("guanggao").get(categoryId);
            //1.取到了直接返回,不走数据库
            if(list != null && list.size() > 0){
                System.out.println("直接走reids,不去查询数据库了:" + list);
                return list;
            }
            //2.取不到,走数据库
            TbContentExample example=new TbContentExample();
            example.setOrderByClause("sort_order desc");//通过降序排列
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);//根据分类id查询
            //只查询state是1的表示启用的广告
            criteria.andStatusEqualTo("1");//开启的广告
            list = contentMapper.selectByExample(example);
            //查询了数据库后,将数据放入到redis,下次就不走数据库了
            redisTemplate.boundHashOps("guanggao").put(categoryId, list);
            System.out.println("走数据库,并且存入到reids中:" + list);
            return list;
        }

这个是日志的结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@591987d4] was not registered for synchronization because synchronization is not active
直接走reids,不去查询数据库了:[com.jd.pojo.TbContent@3f0fdfb8, com.jd.pojo.TbContent@7a0d463d, com.jd.pojo.TbContent@72d4fc40]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca4e929] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.jdbc.JDBC4Connection@489b7465] will not be managed by Spring
JDBC Connection [com.mysql.jdbc.JDBC4Connection@397574e] will not be managed by Spring
==>  Preparing: select id, category_id, title, url, pic, status, sort_order from tb_content WHERE ( category_id = ? and status = ? ) order by sort_order desc 
==>  Preparing: select id, category_id, title, url, pic, status, sort_order from tb_content WHERE ( category_id = ? and status = ? ) order by sort_order desc 
==> Parameters: 6(Long), 1(String)
==> Parameters: 6(Long), 1(String)
<==      Total: 0
<==      Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@591987d4]
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca4e929]
走数据库,并且存入到reids中:[]
走数据库,并且存入到reids中:[]

希望大佬能帮忙看看问题在哪里?

  • 写回答

3条回答

  • dabocaiqq 2020-05-12 09:29
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题