日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

012_Spring Data Redis

發(fā)布時(shí)間:2025/5/22 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 012_Spring Data Redis 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. Spring Data Redis簡介

2. Spring?Data Redis相關(guān)jar包

2.1. Spring相關(guān)jar包

?

2.2. Spring?Data Redis相關(guān)jar包?

2.3. Json相關(guān)jar包?

3. Spring?Data Redis案例

3.1. 新建一個(gè)名為spring-data-redis的Java項(xiàng)目, 同時(shí)添加相關(guān)jar包。

?

3.2. 添加Junit

3.2.1.?項(xiàng)目上右鍵——>Build Path——>Configure Build Path...

?

3.2.2.?點(diǎn)擊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=lyw123456

3.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連接工廠: 創(chuàng)建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"/><!-- 默認(rèn)的序列化器: 序列化器就是根據(jù)規(guī)則將存儲的數(shù)據(jù)中的key與value做字符串的序列化處理 --><!-- keySerializer、valueSerializer: 對應(yīng)的是Redis中的String類型 --><!-- hashKeySerializer、hashValueSerializer: 對應(yīng)的是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;/*** 添加一個(gè)字符串*/@Testpublic void setStr(){redisTemplate.opsForValue().set("spring-data-redis", "SpringData Redis存儲字符串");}/*** 獲取一個(gè)字符串*/@Testpublic void getStr(){String str = (String)redisTemplate.opsForValue().get("spring-data-redis");System.out.println(str);}/*** 刪除一個(gè)值*/@Testpublic void delValue(){redisTemplate.delete("user_json");}/*** 添加User對象, 使用JDK的序列化器*/@Testpublic void setUesr() {User user = new User();user.setAge(20);user.setName("張三豐");user.setId(1);// 重新設(shè)置序列化器redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());redisTemplate.opsForValue().set("user", user);}/*** 取User對象, 使用JDK的序列化器*/@Testpublic void getUser() {// 重新設(shè)置序列化器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. 啟動(dòng)服務(wù)器上的Redis, 選中setStr方法, 右鍵——>Run As——>JUnit Test

3.8. 選中g(shù)etStr方法, 右鍵——>Run As——>JUnit Test?

3.9. 選中setUser方法, 右鍵——>Run As——>JUnit Test?

3.10. 選中g(shù)etUser方法, 右鍵——>Run As——>JUnit Test

?3.11. 選中setUserJSON方法, 右鍵——>Run As——>JUnit Test?

3.12. 選中g(shù)etUserJSON方法, 右鍵——>Run As——>JUnit Test

總結(jié)

以上是生活随笔為你收集整理的012_Spring Data Redis的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。