Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在java中的redis-client.在此之前,希望已经了解redis的基本使用和Maven的使用。建立MavenProject之后,在POM.xml中添加jedis和spring-data-redis的依赖如下:
- <</span>dependency>
-
<</span>groupId>redis.clients</</span>groupId> -
<</span>artifactId>jedis</</span>artifactId> -
<</span>version>2.0.0</</span>version> -
<</span>type>jar</</span>type> -
<</span>scope>compile</</span>scope> - </</span>dependency>
-
- <</span>dependency>
-
<</span>groupId>org.springframework.data</</span>groupId> -
<</span>artifactId>spring-data-redis</</span>artifactId> -
<</span>version>1.0.0.RELEASE</</span>version> - </</span>dependency>
Redis连接数据库参数如下:applicationContext-redis.properties
- #redis
config - redis.pool.maxActive=100
- redis.pool.maxIdle=20
- redis.pool.maxWait=1000
- redis.pool.testOnBorrow=true
- redis.hostname=localhost
- redis.port=6379
- redis.password=
在上下文配置中使用key-value读取方式读取properties中的值:
-
- <</span>bean
id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> -
<</span>property name="maxActive" value="${redis.pool.maxActive}" /> -
<</span>property name="maxIdle" value="${redis.pool.maxIdle}" /> -
<</span>property name="maxWait" value="${redis.pool.maxWait}" /> -
<</span>property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> - </</span>bean>
-
- <</span>bean
id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> -
<</span>property name="hostName" value="${redis.hostname}" /> -
<</span>property name="port" value="${redis.port}" /> -
<</span>property name="password" value="${redis.password}" /> -
<</span>property name="poolConfig" ref="jedisPoolConfig" /> - </</span>bean>
- >
- <</span>bean
id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />
上面redisTemplate已经基本配置完成。
接下来创建User类,必须实现或者间接实现Serializable接口:
Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。
- public
class User implements Serializable { -
-
private static final long serialVersionUID = -7898194272883238670L; -
-
public User() { -
} -
-
public User(String id) { -
} -
-
public User(String id, String name) { -
this.id = id; -
this.name = name; -
} -
-
private String id; -
-
private String name; -
-
public String getId() { -
return id; -
} -
-
public void setId(String id) { -
this.id = id; -
} -
-
public String getName() { -
return name; -
} -
-
public void setName(String name) { -
this.name = name; -
} -
-
public String toString() { -
return "User [id=" + id + ", name=" + name + "]"; -
} - }
创建userService类来操作redis增删查改缓存对象。
- public
class UserService { -
@Autowired -
RedisTemplate redisTemplate; -
public void put(User user) { -
//redisTemplate.opsForValue().put('key'); -
redisTemplate.opsForValue().put('key', user,'超时时间',时间类型); -
} -
-
public void delete(User key) { -
redisTemplate.opsForValue().delete('key'); -
} -
-
public User get(User key) { -
return (User) redisTemplate.opsForValue().get('key'); -
} - }
- <</span>bean
id="userService" class="Service.UserService"> -
<</span>property name="redisTemplate"> -
<</span>ref bean="redisTemplate" /> -
</</span>property> - </</span>bean>
在UserService注解@Service(“userService”),也可以在Service里写名字,默认是第一字母小写。
- @Service("userService")
- public
class UserService { -
-
@Autowired -
RedisTemplate redisTemplate; -
…… -
…… - }
- <</span>context:component-scan
base-package="*" />
在main中来简单操作一下:
- public
class Main { -
public static void main( String[] args ) -
{ -
ApplicationContext applicationContext = new ClassPathXmlApplicationC ontext("classpath*:/conf/applicationContext.xml" ); -
UserService userService = (UserService) applicationContext.getBean("userService"); -
-
User user1 = new User("user1ID", "User 1"); -
User user2 = new User("user2ID", "User 2"); -
-
System.out.println("==== getting objects from redis ===="); -
System.out.println("User is not in redis yet: " + userService.get(user1)); -
System.out.println("User is not in redis yet: " + userService.get(user2)); -
-
System.out.println("==== putting objects into redis ===="); -
userService.put(user1); -
userService.put(user2); -
-
System.out.println("==== getting objects from redis ===="); -
System.out.println("User should be in redis yet: " + userService.get(user1)); -
System.out.println("User should be in redis yet: " + userService.get(user2)); -
-
System.out.println("==== deleting objects from redis ===="); -
userService.delete(user1); -
userService.delete(user2); -
-
System.out.println("==== getting objects from redis ===="); -
System.out.println("User is not in redis yet: " + userService.get(user1)); -
System.out.println("User is not in redis yet: " + userService.get(user2)); -
-
} - }
确保redis服务器是开启状态之后就可以运行程序。运行结果如下: