项目需要将Jedis整合到spring中,正好spring项目中包括了spring-data-redis项目,并对jedis进行了封装。极大的方便了redis的操作。
所需的基本包:
spring的配置文件中加入redis的配置:
配置好后就可以直接在项目中使用redisTemplate和stringRedisTemplate(为了简便字符串操作,spring为提供了单独stringRedisTemplate模板):
@Component(“jedis”)
@Service
public class SpringJedisService {
@Autowired
private RedisTemplate<String, String> template;
@Autowired
private StringRedisTemplate strTemplate;
public void addStrKV(String key,String value){
strTemplate.opsForValue().set("sTest", "string one");
template.opsForList().leftPush("lTest", "number one");
System.out.println(template.opsForValue().get("sTest") + "---" + template.opsForList().leftPop("lTest"));
}
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-mvc.xml");
SpringJedisService sj = (SpringJedisService)context.getBean("jedis");
sj.addStrKV("", "");
}
}
简单的配置和使用就是这样了