写在最前面redis
1.Spring必须是4.2.6及以上版本才支持redisspring
2.jar包版本建议统一数据结构
须要准备jar包测试
1.aopalliance-1.0.jarthis
2.spring-data-commons-1.8.4.RELEASE.jarspa
3.spring-data-redis-1.8.4.RELEASE.jar3d
正文code
1.在spring配置文件中添加配置 xml
<!-- 链接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大链接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲链接数 --> <property name="maxIdle" value="10" /> <!-- 每次释放链接的最大数目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 释放链接的扫描间隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 链接最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 链接空闲多久后释放, 当空闲时间>该值 且 空闲链接>最大空闲链接数 时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 获取链接时的最大等待毫秒数,小于零:阻塞不肯定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取链接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="false" /> <!-- 在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="true" /> <!-- 链接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- redis单机 经过链接池 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="hostName" value="127.0.0.1" /> <property name="port" value="6379" /> </bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean>
3.测试一下对象
首先建立一个User的model类(必须继承Serializable类才可序列化)
public class User implements Serializable{ private String name; private String age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void print(){ System.out.println("姓名:"+name); System.out.println("年龄:"+age); System.out.println("性别:"+sex); }
}
写一个测试类
public class TestOrder { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); RedisTemplate r = context.getBean(RedisTemplate.class);; User user = new User(); user.setName("冯吉荣"); user.setAge("22"); user.setSex("男"); r.opsForValue().set("user_1", user); User user1 = (User)r.opsForValue().get("user_1"); user1.print(); } }
结果
此次真有图
写在最后
对象的存储是hash,保证jar包的版本统一,同时保证redis服务在运行,便可成功