SpringData进阶(一)SpringData Redis之整合初步

前言

      本章讲解SpringData Redis之整合

方法

1.概念

我们之前学习过Redis,这部分的内容我相信大家并不陌生,当时我们依靠一个Jar包对Redis进行操作。

说句实话,其实还是蛮繁琐的!SpringData Redis的出现一定程度上解决了这个问题。

2.整合Spring+SpringData Redis

1)创建相应的项目

2)编写pom文件,引入相关的jar包坐标

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.edu.ccut</groupId>
	<artifactId>spring-springdata-redis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	<properties>
		<!-- 设定Java的版本 -->
		<java.version>1.8</java.version>
		<!-- 设定Spring的版本 -->
		<spring.version>5.1.9.RELEASE</spring.version>
	</properties>
	
	<dependencies>
		<!-- 引入Spring相关jar -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- springData Redis 相关jar -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>2.1.10.RELEASE</version>
		</dependency>
		<!-- Redis 相关jar -->
		<dependency>
		    <groupId>redis.clients</groupId>
		    <artifactId>jedis</artifactId>
		    <version>3.0.1</version>
		</dependency>
		<!-- 引入spring整合JUnit测试包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- 引入JUnit测试包 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

3)修改applicationContext.xml文件和redis.properties文件

本次引入的是redis单机版,所以SpringData Redis 2.0之后的配置如下所示:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 配置组件扫描,以便于告诉Spring哪里有注解 -->
    <context:component-scan base-package="cn.edu.ccut"></context:component-scan>
    <!-- 引入redis配置文件 -->
    <context:property-placeholder location="classpath:redis.properties"></context:property-placeholder>
   
    <!-- 配置RedisStandaloneConfiguration(单机版) -->
    <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
    </bean>
    <!-- 配置Jedis连接工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    	<constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
    </bean>
    <!-- 配置Redis模板类 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    	<property name="connectionFactory" ref="jedisConnectionFactory"/>
    	<!-- redis序列化器 -->
    	<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>

redis.properties配置文件详情如下:

#Redis
redis.host=192.168.1.103
redis.port=6379

#Redis Pool
#redis.pool.maxTotal=20
#redis.pool.maxIdle=10
#redis.pool.minIdle=5

4)启动我们事先准备好的Redis

如果还没有学过Redis的同学请移步Redis学习

本次Redis安装在Linux系统中!

5)编写测试代码初步测试

package cn.edu.ccut;

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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class RedisTest {
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Test
	public void testRedis(){
		redisTemplate.opsForValue().set("username", "jwang");
	}
}

测试结果为:

注意:

SpringData Redis2.0以下的版本与本次配置有所不同,使用SpringData Redis2.0以下的版本的同学请自行查阅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值