怎么在ssm项目中使用redis缓存查询数据?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

xml配置:

<beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig"><propertyname="maxTotal"value="${redis.maxTotal}"></property><propertyname="maxIdle"value="${redis.maxIdle}"></property><propertyname="testOnBorrow"value="${redis.testOnBorrow}"></property><propertyname="maxWaitMillis"value="${redis.maxWaitMillis}"></property></bean><!--jedis客户端单机版--><beanid="jedisPool"class="redis.clients.jedis.JedisPool"><constructor-argname="poolConfig"ref="poolConfig"></constructor-arg><constructor-argname="host"value="${redis.hostName}"></constructor-arg><constructor-argname="port"value="${redis.port}"></constructor-arg></bean><!--jedis集群版配置--><beanid="jedisCluster"class="redis.clients.jedis.JedisCluster"><constructor-argname="poolConfig"ref="poolConfig"></constructor-arg><constructor-argname="nodes"><set><beanclass="redis.clients.jedis.HostAndPort"><constructor-argname="host"value="127.0.0.1"></constructor-arg><constructor-argname="port"value="7001"></constructor-arg></bean><beanclass="redis.clients.jedis.HostAndPort"><constructor-argname="host"value="127.0.0.1"></constructor-arg><constructor-argname="port"value="7002"></constructor-arg></bean><beanclass="redis.clients.jedis.HostAndPort"><constructor-argname="host"value="127.0.0.1"></constructor-arg><constructor-argname="port"value="7003"></constructor-arg></bean><beanclass="redis.clients.jedis.HostAndPort"><constructor-argname="host"value="127.0.0.1"></constructor-arg><constructor-argname="port"value="7004"></constructor-arg></bean><beanclass="redis.clients.jedis.HostAndPort"><constructor-argname="host"value="127.0.0.1"></constructor-arg><constructor-argname="port"value="7005"></constructor-arg></bean></set></constructor-arg></bean>[java]viewplaincopyjava调用//service层查询时代码@AutowiredprivateJedisClientjedisClient;@OverridepublicList<User>list(){//TODOAuto-generatedmethodstubtry{Stringjson=jedisClient.hget("user","list");if(StringUtils.isNotBlank(json)){List<User>users=JsonUtils.jsonToList(json,User.class);returnusers;}}catch(Exceptione){e.printStackTrace();}//使用持久层查询数据库List<User>list=userMapper.list();try{jedisClient.hset("user","list",JsonUtils.objectToJson(list));}catch(Exceptione){e.printStackTrace();}returnlist;}

看完上述内容,你们掌握怎么在ssm项目中使用redis缓存查询数据的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!