Spring整合Redis入门详解及基础练习

42 篇文章 0 订阅
9 篇文章 0 订阅

简介

相信来到这里的朋友都已经对Redis有了基本的了解,基础命令都已掌握,文中就不做过多赘述,避免文章过于冗余,只上干货,干就完了bro。Let's go!!

Spring官网中整合redis地址:Spring Data Redis

Redis因其性能比较高,所以经常在项目中用于缓存数据,因此将Redis和Spring以及项目进行整合。

Spring整合Redis

| 1.创建Maven项目工程

本文所用项目结构如图所示(尚未整合web项目,入门以测试为主):

| 2.pom.xml中引入redis依赖

        <!-- 连接redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>${spring-data-redis-version}</version>
        </dependency>

依赖完成后刷新打包至本地仓库:(如图所示)

| 3.redis.conf配置

对配置文件还不了解的兄弟们请参考:

Redis中redis.conf 配置文件信息详解【一看就懂】_杨大仙爱篮球-CSDN博客

  • 1)默认情况下redis运行在保护模式(这种模式下,访问不需要密码),但是这种模式只允许本地访问
  • protected-mode yes     改为  no
  • 2)初始配置了只在127.0.0.1上绑定监听,将其注释
  • 默认 bind 127.0.0.1      改为  #bind 127.0.0.1或 0.0.0.0

| 4.防火墙配置

开放redis的默认端口号6379(或你设置的redis端口号)

这里不做展示,对防火墙不了解的兄弟们请参考(很重要):

【黑客必备】Linux系统中防火墙Firewall操作详解(附上图示及说明)【一看就懂】_杨大仙爱篮球-CSDN博客

| 5.配置spring-redis.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
    <!--1 配置工厂类 -->
    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--1.1 服务器地址 -->
        <property name="hostName" value="192.168.21.132"></property>
        <!--1.2 端口号-->
        <property name="port" value="6379"></property>
    </bean>

    <!-- 3 redisTemplate -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <!--3.1注入工厂-->
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <!--key序列化-->
        <property name="keySerializer" ref="stringRedisSerializer"></property>
        <!--value序列化-->
        <property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"></property>
        <!--hashkey-->
        <property name="hashKeySerializer" ref="stringRedisSerializer"></property>
        <!--hashvalue-->
        <property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"></property>
    </bean>

    <!--stringRedisSerializer-->
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
    <!--JdkSerializationRedisSerializer-->
    <bean id="jdkSerializationRedisSerializer"
          class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
    <!--GenericJackson2JsonRedisSerializer-->
    <bean id="genericJackson2JsonRedisSerializer"
          class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean>
    <!--Jackson2JsonRedisSerializer-->
    <bean id="jackson2JsonRedisSerializer"
          class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
        <constructor-arg value="java.lang.Object"></constructor-arg>
    </bean>

</beans>

此xml已经配好,拿去使用时只需修改ip地址,如图所示:

| 6.常见序列化反序列化处理类

对spring-redis.xml的简单解释:

StringRedisSerializer

一般key的序列化使用

JdkSerializationRedisSerializer

默认的序列化方式

GenericJackson2JsonRedisSerializer

(选择)

支持对象转json,json转对象的序列和反序列化,支持带泛型的对象,效率比Jackson2JsonRedisSerializer

Jackson2JsonRedisSerializer

支持对象转json,json转对象的序列和反序列化,不支持泛型,效率高

我们选择使用GenericJackson2JsonRedisSerializer

测试(string、list、set、zset、hash)

| 1.测试String

测试String存对象

使用命令查看数据库的对应值:

对应可视化数据库值如图所示:

| 2.测试List

测试集合的批量添加数据及获取

| 3.测试set

测试交集、并集、差集。

| 4.测试sorted_set

| 5.测试hash

关于hash的使用还有很多,比如使用hash实现购物车、计数器以及在线人数等等。

使用Redis实现购物车、计数器、好/差评等功能测试【一看就懂】_杨大仙爱篮球-CSDN博客

关于Spring和Redis的整合及入门测试练习就到此一节,勤加练习,打好基础,以熟能生巧。

转载请注明出处


如有错误,欢迎指正

Thanks

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hah杨大仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值