本文完全参考上面教程,但实现过程中出现了若干问题,故对代码做了若干修改,纯属笔记。
1. 建Maven工程
2. 工程结构
3. 代码
1)pom.xml
<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>com.example</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>redis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
2)applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.example.redis.dao" />
<context:property-placeholder location="classpath:applicationContext.properties" />
<!-- 连接池配置 -->
<bean id="jpoolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:maxIdle="${redis.maxIdle}" p:maxWaitMillis="${redis.maxWait}"
p:testOnBorrow="${redis.testOnBorrow}" />
<!--链接配置 -->
<bean id="jconnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}" p:port="${redis.port}" p:poolConfig-ref="jpoolConfig"/>
<!-- Redis模版类,用于操作redis -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="jconnectionFactory">
</bean>
</beans>
3)applicationContext.properties
#连接配置
redis.host=127.0.0.1
redis.port=6379
#连接池配置
redis.maxIdle=10
redis.maxWait=50
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
4)User.java
package com.example.redis.entity;
import org.springframework.beans.factory.annotation.Autowired;
public class User
{
@Autowired
private long id;
@Autowired
private String name;
public long getId()
{
return id;
}
public void setId(long id2)
{
this.id = id2;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
5)UserDao.java
package com.example.redis.dao;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import com.example.redis.entity.User;
@Repository
public class UserDao
{
@Autowired
protected RedisTemplate<Serializable, Serializable> redisTemplate;
public void saveUser(final User user) {
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(redisTemplate.getStringSerializer().serialize(user.getId() + ""),
redisTemplate.getStringSerializer().serialize(user.getName()));
return null;
}
});
}
public User getUser(final long id) {
return redisTemplate.execute(new RedisCallback<User>() {
public User doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize(id + "");
if (connection.exists(key)) {
byte[] value = connection.get(key);
String name = redisTemplate.getStringSerializer().deserialize(value);
User user = new User();
user.setName(name);
user.setId(id);
return user;
}
return null;
}
});
}
}
6)App.java
package com.example.redis.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.redis.entity.User;
public class App
{
@SuppressWarnings("resource")
public static void main( String[] args )
{
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
UserDao userDAO = (UserDao)ac.getBean("userDao");
User user = new User();
user.setId(123);
user.setName("Lucky");
userDAO.saveUser(user);
User userr = userDAO.getUser(123); //与 id 对应
System.out.println(userr.getName());
}
}
7)Redis1.java
/*
* 测试连接server成功否
*/
package com.example.redis.dao;
import redis.clients.jedis.Jedis;
public class Redis1
{
public static void main(String[] args)
{
Jedis jedis = new Jedis("localhost");
System.out.println("连接: " + jedis.ping());
}
}
4. 运行
先跑Redis1.java -> 测试连接是否成功
再跑App.java -> 结果:
ok!
5. 过程中,出现的问题&解决
1)pox.xml需要导入servlet和slf4j
2)Redis连接的是本地服务器,注意地址
3)Redis不用设置密码,注意有无设置密码