redis封装类java_SpringBoot实战:整合Redis、mybatis,封装RedisUtils工具类等(附源码)...

创建SpringBoot项目

在线创建方式

网址:https://start.spring.io/

a5e75f7e2df15a25a964bd16ae38374d.png

然后创建Controller、Mapper、Service包

2d1bd364ca6deb8d043c39018e6f7c60.png

SpringBoot整合Redis

引入Redis依赖

org.springframework.boot

spring-boot-starter-data-redis

完整pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.3.RELEASE

com.cyb

chenyb-mobile-redis

0.0.1-SNAPSHOT

chenyb-mobile-redis

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

io.projectreactor

reactor-test

test

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-maven-plugin

设置Redis的Template

7c7c4cf2c3184f9f87007744e575e5ae.png

RedisConfig.java

package com.cyb.mobile.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.RedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

* @ClassName:RedisConfig

* @Description:Redis配置类

* @Author:chenyb

* @Date:2020/8/16 11:48 下午

* @Versiion:1.0

*/

@Configuration //当前类为配置类

public class RedisConfig{

@Bean //redisTemplate注入到Spring容器

public RedisTemplate redisTemplate(RedisConnectionFactory factory){

RedisTemplate redisTemplate=new RedisTemplate<>();

RedisSerializer redisSerializer = new StringRedisSerializer();

redisTemplate.setConnectionFactory(factory);

//key序列化

redisTemplate.setKeySerializer(redisSerializer);

//value序列化

redisTemplate.setValueSerializer(redisSerializer);

//value hashmap序列化

redisTemplate.setHashKeySerializer(redisSerializer);

//key hashmap序列化

redisTemplate.setHashValueSerializer(redisSerializer);

return redisTemplate;

}

}

设置Redis连接信息

059bc8e3f9f641e00563360c2d5e2bd2.png

# 连接的那个数据库

spring.redis.database=0

# redis服务的ip地址

spring.redis.host=192.168.199.142

# redis端口号

spring.redis.port=6379

# redis的密码,没设置过密码,可为空

spring.redis.password=12345678

Redis工具类

redisTemplate API

opsForValue ==》String

opsForSet ==》Set

opsForHash ==》hash

opsForZset ==》SortSet

opsForList ==》list队列

RedisUtils.java

059bc8e3f9f641e00563360c2d5e2bd2.png

package com.cyb.mobile.utils;

import org.springframework.beans.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值