使用Jedis來操做Redis有兩個明顯的問題(我都能發現固然很明顯):java
不過強大的Spring確定會有對Redis的支持,因而我找到了Spring Data Redisredis
Spring Data Redis 提供了四種serializer:spring
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基礎的序列化策略,「JacksonJsonRedisSerializer」與「OxmSerializer」都是基於stirng存儲,所以它們是較爲「高級」的序列化(最終仍是使用string解析以及構建java對象)。json
RedisTemplate中須要聲明4種serializer,默認爲「JdkSerializationRedisSerializer」:設計模式
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="${redis.url}"/> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.auth}"/> <property name="timeout" value="${redis.timeout}"/> <property name="database" value="${redis.database}" /> <property name="usePool" value="true" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean>
import java.io.Serializable; /** * Created by FJH on 2017/6/18. */ public class User implements Serializable{ private Long userId; private String username; private String loginName; private String password; @Override public String toString() { return "User{" + "userId=" + userId + ", username='" + username + '\'' + ", loginName='" + loginName + '\'' + ", password='" + password + '\'' + '}'; } public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
import cn.fjhdtp.maventest.dao.UserDao; import cn.fjhdtp.maventest.model.User; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Repository; import javax.annotation.Resource; /** * Created by FJH on 2017/6/18. */ @Repository public class UserDaoImpl implements UserDao { @Resource private RedisTemplate<Long, User> redisTemplate; @Override public void save(User user) { ValueOperations<Long, User> valueOperations = redisTemplate.opsForValue(); valueOperations.set(user.getUserId(), user); } @Override public User get(Long id) { ValueOperations<Long, User> valueOperations = redisTemplate.opsForValue(); return valueOperations.get(id); } @Override public void delete(Long id) { ValueOperations<Long, User> valueOperations = redisTemplate.opsForValue(); RedisOperations<Long, User> redisOperations = valueOperations.getOperations(); redisOperations.delete(id); } }