标签:
好了费话不多说了,介绍下spring 结合redis是怎么操作数据的 这里我用了maven管理,由于简单嘛,依赖下包就行了..不用单独去依赖包,成了我的习惯
好了,下面是pom的代码
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>redis</groupId>
- <artifactId>redis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <build>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
在pom里添加了redis spring客户端的依赖,那么所有的jar包都会帮你自动下载下来,是不是很方便啊,哈
下面是spring-context.xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
-
- <context:annotation-config />
- <context:component-scan base-package="com.mkfree.**" />
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
- <bean id="redisService" class="com.mkfree.redis.test.RedisService">
- </bean>
这里配置了一个跟spring 集成的redis客户端,ip port password自己定哦,这里我在redis配置文件了定义了需求密码认证,你们先不要用吧,为了简单起见
下面是RedisService,里面包含了对redis的方法,还有更多的方法没有去使用,这里当一个入门的小例子吧
- package com.mkfree.redis.test;
-
- import java.util.Set;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
-
- import redis.clients.jedis.Jedis;
-
- public class RedisService {
-
-
- public void del(byte [] key){
- this.getJedis().del(key);
- }
-
- public void del(String key){
- this.getJedis().del(key);
- }
-
-
- public void set(byte [] key,byte [] value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
-
- public void set(String key,String value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
-
- public void set(String key,String value){
- this.getJedis().set(key, value);
- }
-
- public void set(byte [] key,byte [] value){
- this.getJedis().set(key, value);
- }
-
- public String get(String key){
- String value = this.getJedis().get(key);
- return value;
- }
-
- public byte[] get(byte [] key){
- return this.getJedis().get(key);
- }
-
-
- public Set<String> keys(String pattern){
- return this.getJedis().keys(pattern);
- }
-
-
- public boolean exists(String key){
- return this.getJedis().exists(key);
- }
-
- public String flushDB(){
- return this.getJedis().flushDB();
- }
-
- public long dbSize(){
- return this.getJedis().dbSize();
- }
-
- public String ping(){
- return this.getJedis().ping();
- }
-
- private Jedis getJedis(){
- if(jedis == null){
- return jedisConnectionFactory.getShardInfo().createResource();
- }
- return jedis;
- }
- private RedisService (){
-
- }
-
- private static Jedis jedis;
- @Autowired
- @Qualifier("jedisConnectionFactory")
- private JedisConnectionFactory jedisConnectionFactory;
- }
下面是测试代码TestRedis.java
- package com.mkfree.redis.test;
-
- import java.util.Set;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestRedis {
-
- public static void main(String[] args) throws InterruptedException {
- ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
-
- RedisService redisService = (RedisService) app.getBean("redisService");
-
- String ping = redisService.ping();
- System.out.println(ping);
-
-
- long dbSizeStart = redisService.dbSize();
- System.out.println(dbSizeStart);
-
- redisService.set("username", "oyhk");
- String username = redisService.get("username");
- System.out.println(username);
- redisService.set("username1", "oyhk1", 1);
- String username1 = redisService.get("username1");
- System.out.println(username1);
- Thread.sleep(2000);
- String liveUsername1 = redisService.get("username1");
- System.out.println(liveUsername1);
-
-
- boolean exist = redisService.exists("username");
- System.out.println(exist);
-
-
- Set<String> keys = redisService.keys("*");
- System.out.println(keys);
-
-
- redisService.set("username2", "oyhk2");
- String username2 = redisService.get("username2");
- System.out.println(username2);
- redisService.del("username2");
- String username2_2 = redisService.get("username2");
- System.out.println(username2_2);
-
-
- long dbSizeEnd = redisService.dbSize();
- System.out.println(dbSizeEnd);
-
-
-
- }
- }
spring 结合 redis 例子 (转)
标签:
原文地址:http://www.cnblogs.com/-lpf/p/4272426.html