javascript
012_Spring Data Redis
1. Spring Data Redis簡介
2. Spring?Data Redis相關jar包
2.1. Spring相關jar包
?
2.2. Spring?Data Redis相關jar包?
2.3. Json相關jar包?
3. Spring?Data Redis案例
3.1. 新建一個名為spring-data-redis的Java項目, 同時添加相關jar包。
?
3.2. 添加Junit
3.2.1.?項目上右鍵——>Build Path——>Configure Build Path...
?
3.2.2.?點擊Add Library...?
3.2.3.?選擇JUnit——>Next——>Apply and Close?
3.2.4.?JUnit能力?
3.3. 在src下新建application.properties?
spring.redis.pool.max-idle=10 spring.redis.pool.min-idle=5 spring.redis.pool.max-total=20spring.redis.hostName=192.168.25.138 spring.redis.port=6379 spring.redis.password=lyw1234563.4. 在src下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置讀取properties文件的工具類 --><context:property-placeholder location="classpath:application.properties"/><!-- Jedis連接池 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${spring.redis.pool.max-total}"/><property name="maxIdle" value="${spring.redis.pool.max-idle}"/><property name="minIdle" value="${spring.redis.pool.min-idle}"/></bean><!-- Jedis連接工廠: 創建Jedis對象的工廠 --><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><!-- IP地址 --><property name="hostName" value="${spring.redis.hostName}"/><!-- 端口 --><property name="port" value="${spring.redis.port}"/><!-- 端口 --><property name="password" value="${spring.redis.password}"/><!-- 連接池 --><property name="poolConfig" ref="poolConfig"/></bean><!-- Redis模板對象:是SpringDataRedis提供的用戶操作Redis的對象 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory"/><!-- 默認的序列化器: 序列化器就是根據規則將存儲的數據中的key與value做字符串的序列化處理 --><!-- keySerializer、valueSerializer: 對應的是Redis中的String類型 --><!-- hashKeySerializer、hashValueSerializer: 對應的是Redis中的Hash類型 --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property></bean> </beans>3.5. 新建User.java
package com.bjbs.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";} }3.6. 新建RedisTest.java
package com.bjbs.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.bjbs.pojo.User;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class RedisTest {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;/*** 添加一個字符串*/@Testpublic void setStr(){redisTemplate.opsForValue().set("spring-data-redis", "SpringData Redis存儲字符串");}/*** 獲取一個字符串*/@Testpublic void getStr(){String str = (String)redisTemplate.opsForValue().get("spring-data-redis");System.out.println(str);}/*** 刪除一個值*/@Testpublic void delValue(){redisTemplate.delete("user_json");}/*** 添加User對象, 使用JDK的序列化器*/@Testpublic void setUesr() {User user = new User();user.setAge(20);user.setName("張三豐");user.setId(1);// 重新設置序列化器redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());redisTemplate.opsForValue().set("user", user);}/*** 取User對象, 使用JDK的序列化器*/@Testpublic void getUser() {// 重新設置序列化器redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());User users = (User) redisTemplate.opsForValue().get("user");System.out.println(users);}/*** 基于JSON格式存User對象*/@Testpublic void setUseJSON() {User user = new User();user.setAge(20);user.setName("李四豐");user.setId(1);redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));redisTemplate.opsForValue().set("user_json", user);}/*** 基于JSON格式取User對象*/@Testpublic void getUseJSON() {redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));User user = (User) this.redisTemplate.opsForValue().get("user_json");System.out.println(user);} }3.7. 啟動服務器上的Redis, 選中setStr方法, 右鍵——>Run As——>JUnit Test
3.8. 選中getStr方法, 右鍵——>Run As——>JUnit Test?
3.9. 選中setUser方法, 右鍵——>Run As——>JUnit Test?
3.10. 選中getUser方法, 右鍵——>Run As——>JUnit Test
?3.11. 選中setUserJSON方法, 右鍵——>Run As——>JUnit Test?
3.12. 選中getUserJSON方法, 右鍵——>Run As——>JUnit Test
總結
以上是生活随笔為你收集整理的012_Spring Data Redis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 011_Spring Data JPA多
- 下一篇: 008_Input输入框