SpringBoot實(shí)戰(zhàn):整合Redis、mybatis,封裝RedisUtils工具類等(附源碼)
作者:陳彥斌
cnblogs.com/chenyanbin/p/13515268.html
創(chuàng)建SpringBoot項(xiàng)目
在線創(chuàng)建方式
網(wǎng)址:https://start.spring.io/
然后創(chuàng)建Controller、Mapper、Service包
SpringBoot整合Redis
引入Redis依賴
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
完整pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.3.RELEASEversion>
<relativePath/>
parent>
<groupId>com.cybgroupId>
<artifactId>chenyb-mobile-redisartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>chenyb-mobile-redisname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.projectreactorgroupId>
<artifactId>reactor-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
設(shè)置Redis的Template
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 //當(dāng)前類為配置類
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;
}
}
設(shè)置Redis連接信息
# 連接的那個(gè)數(shù)據(jù)庫
spring.redis.database=0
# redis服務(wù)的ip地址
spring.redis.host=192.168.199.142
# redis端口號(hào)
spring.redis.port=6379
# redis的密碼,沒設(shè)置過密碼,可為空
spring.redis.password=12345678
Redis工具類
redisTemplate API
-
opsForValue ==》String
-
opsForSet ==》Set
-
opsForHash ==》hash
-
opsForZset ==》SortSet
-
opsForList ==》list隊(duì)列
RedisUtils.java
package com.cyb.mobile.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @ClassName:RedisUtils
* @Description:Redis工具類
* @Author:chenyb
* @Date:2020/8/17 12:05 上午
* @Versiion:1.0
*/
@Service
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
private static double size = Math.pow(2, 32);
/**
* 寫入緩存
*
* @param key
* @param offset 位 8Bit=1Byte
* @return
*/
public boolean setBit(String key, long offset, boolean isShow) {
boolean result = false;
try {
ValueOperations
operations = redisTemplate.opsForValue();
operations.setBit(key, offset, isShow);
result =
true;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 寫入緩存
*
* @param key
* @param offset
* @return
*/
public boolean getBit(String key, long offset) {
boolean result =
false;
try {
ValueOperations
operations = redisTemplate.opsForValue();
result = operations.getBit(key, offset);
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 寫入緩存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result =
false;
try {
ValueOperations
operations = redisTemplate.opsForValue();
operations.set(key, value);
result =
true;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 寫入緩存設(shè)置時(shí)效時(shí)間
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result =
false;
try {
ValueOperations
operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result =
true;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量刪除對(duì)應(yīng)的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 刪除對(duì)應(yīng)的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判斷緩存中是否有對(duì)應(yīng)的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 讀取緩存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result =
null;
ValueOperations
operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 哈希 添加
*
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value) {
HashOperations
hash = redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
/**
* 哈希獲取數(shù)據(jù)
*
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey) {
HashOperations
hash = redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
/**
* 列表添加
*
* @param k
* @param v
*/
public void lPush(String k, Object v) {
ListOperations
list = redisTemplate.opsForList();
list.rightPush(k, v);
}
/**
* 列表獲取
*
* @param k
* @param l
* @param l1
* @return
*/
public List
評(píng)論
圖片
表情
