怎么在ssm项目中使用redis缓存查询数据?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
xml配置:
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
</bean>
<!-- jedis客户端单机版 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
<constructor-arg name="host" value="${redis.hostName}"></constructor-arg>
<constructor-arg name="port" value="${redis.port}"></constructor-arg>
</bean>
<!-- jedis集群版配置 -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
</set>
</constructor-arg>
</bean>
[java] view plain copy
java调用
//service层查询时代码
@Autowired
private JedisClient jedisClient;
@Override
public List<User> list() {
// TODO Auto-generated method stub
try {
String json = jedisClient.hget("user", "list");
if (StringUtils.isNotBlank(json)) {
List<User> users = JsonUtils.jsonToList(json, User.class);
return users;
}
} catch (Exception e) {
e.printStackTrace();
}
//使用持久层查询数据库
List<User> list = userMapper.list();
try {
jedisClient.hset("user", "list", JsonUtils.objectToJson(list));
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
看完上述内容,你们掌握怎么在ssm项目中使用redis缓存查询数据的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!