1. 在pom.xml文件中配置相关依赖:
<!-- 加载MyBatis整合SpringBoot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2. 在Springboot核心配置文件application.properties中配置redis连接信息:
# 配置 Redis 连接信息
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
3. 在需要操作redis的类中注入redisTemplate:
package com.sztxtech.springbootmybatis.service.impl;
import com.sztxtech.springbootmybatis.entity.User;
import com.sztxtech.springbootmybatis.mapper.UserMapper;
import com.sztxtech.springbootmybatis.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserInfoServiceImpl implements UserInfoService {
@Autowired
private UserMapper mapper;
/** 注入SpringBoot自动配置好的 RedisTemplate */
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* 查询所有用户信息
* @return
*/
public List<User> findAllUserInfo(){
// 查询Redis缓存
List<User> userList = (List<User>) redisTemplate.opsForValue().get("allUser");