Java-SpringDataRedis使用入门
2023-09-14 09:01:58 时间
<–start–>
spring设计spring data的初衷就是为了统一持久层。spring data redis设计的目的就是为了简化redis的操作,要使用spring data redis,分为以下几个要点:
① 在pom文件中配置spring data redis的坐标。
② 在spring的applicationContext.xml文件中配置jedis的连接工厂。
③ 基于jedis的工厂构造redis的模板RedisTemplate。
④ 将redis的模板RedisTemplate注入到程序代码中。
⑤ 在程序代码中就可以通过操作RedisTemplate来操作redis。
在pom文件中引入spring data redis的坐标:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
在spring的配置文件applicationContext.xml中配置RedisTemplate。
JedisPoolConfig(连接池)JedisConnectionFactory(连接工厂)RedisTemplate(redis模板)StringRedisSerializer(将key和value存储为string类型)
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- jedis 连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxWaitMillis" value="3000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- jedis 连接工厂 -->
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="localhost" p:port="6379" p:pool-config-ref="poolConfig"
p:database="0" />
<!-- spring data 提供 redis模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
</property>
</bean>
</beans>
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class RedisTemplateTest {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void testRedis() {
// 保存key value
// 设置30秒失效
redisTemplate.opsForValue().set("city", "武汉", 30, TimeUnit.SECONDS);
System.out.println(redisTemplate.opsForValue().get("city"));
}
}
<–end–>
相关文章
- Java 异常错误 (Ljava/lang/String;)L java/lang/String;「建议收藏」
- 怎么用命令提示符运行JAVA代码_java命令提示符如何进入
- Java 入门练习(26 - 30)
- java 实现 按位异或_Java 按位异或的性质及其妙用
- java使用md5_Java_MD5的使用「建议收藏」
- java怎么用_如何使用Java编写程序
- java标识符是什么_Java 标识符「建议收藏」
- md5 java 实现_MD5加密的Java实现
- eclipse创建一个java项目目录_Eclipse创建JAVA项目
- java 缓存工具类初始化_Java缓存框架
- Java方法重载_java入门方法的使用
- JAVA读取csv文件_java读取csv文件某一列
- 使用Java在Linux系统上开发:一步步安装全部组件(java环境linux)
- 实现使用Java实现Redis消息队列(redis消息队列java)
- 服务器使用Java进行Linux服务器监控(java监控linux)
- Java远程登录Linux服务器入门指南(java远程linux)
- 使用Java连接Linux服务器的简单方法(java连接linux)
- 学习Redis:Java实现快速入门(redisjava实例)
- 如何使用Java备份Oracle数据库?(java备份oracle)
- 使用Java实现MySQL数据恢复操作(java恢复mysql)
- java使用Weka、Linux和Java构建强大的数据分析系统(weka linux)
- Java更新提升Oracle软件性能(java更新oracle)
- Java导入Oracle 实现快速数据传输(java导入oracle)
- 使用Java实现Redis锁定的实现(redis锁定 java)
- 本使用Oracle Java 进行升级新版本带来新体验(oracle java版)