Springboot整合Redis

5 篇文章 0 订阅
3 篇文章 0 订阅

1: Springboot整合Redis是在上一篇Springboot整合Mybatis的基础上进行的,链接

https://blog.csdn.net/tangmingxin0529/article/details/81106359

2:新增依赖

spring-boot-starter-data-redis  为redis依赖

com.alibaba.fastjson  是json依赖

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        <dependency>
		<groupId>com.alibaba</groupId>
	        <artifactId>fastjson</artifactId>
	        <version>1.2.47</version>
	</dependency>

3:application.yml的配置

server:
  port: 8080
  context-path: /springbootMybatis 
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8
    username: root
    password: mysql
  redis:
    database: 1
    host: 127.0.0.1
    port: 6379
    password: 
    pool:
      #连接池最大连接数[使用负数表示没有限制]
      max-active: 1000
      #连接池最大阻塞等待时间[使用负数表示没有限制]
      max-wait: -1
      #连接池最大空闲连接
      max-idle: 10
      #连接池最小空闲连接
      min-idle: 2
    #连接超时时间
    timeout: 0
mybatis:
  mapperLocations: classpath:/mapper/UserMapper.xml
  typeAliasesPackage: com.springbmybatis.entity

4:Controller

import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.springbmybatis.entity.User;
import com.springbmybatis.service.UserService;

@RestController
@RequestMapping(value="/redisController")
public class RedisController {
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	
	@Resource
	private UserService userService;
	
	@GetMapping(value="/redisTest")
	public String redisTest() {
		stringRedisTemplate.opsForValue().set("name", "我是redis");
		String string = stringRedisTemplate.opsForValue().get("name");
		return string;
	}
	//将数据库参数存入缓存再取出
	@GetMapping(value="/redisGetUser/{id}")
	public String redisGetUser(@PathVariable("id") Integer userId) {
		User user = userService.findUserById(userId);
		stringRedisTemplate.opsForValue().set("user",JSON.toJSONString(user));
		String userInfo = stringRedisTemplate.opsForValue().get("user");
		return userInfo;
	}
	//直接从缓存中获取值
	@GetMapping(value="/redisGetUser1/{id}")
	public String redisGetUser1() {
		String userInfo = stringRedisTemplate.opsForValue().get("user");
		return userInfo;
	}
	//放入一个集合
	@GetMapping(value="/listUser")
	public String listUser(){
		List<User> findAllUser = userService.findAllUser();
		stringRedisTemplate.opsForValue().set("userList", JSON.toJSONString(findAllUser));
		String listUserInfo = stringRedisTemplate.opsForValue().get("userList");
		return listUserInfo;
	}
	@GetMapping(value="/listUser1")
	public String listUser1(){
		String listUserInfo = stringRedisTemplate.opsForValue().get("userList");
		return listUserInfo;
	}
}

需要注意的是采用StringRedisTemplate只能存放String数据类型,所以这里采用了将对象[或者对象集合]转换为JSON字符串

5:启动demo

访问1:  http://127.0.0.1:8080/springbootMybatis/redisController/redisTest

访问2:http://127.0.0.1:8080/springbootMybatis/redisController/redisGetUser/1

访问3:  http://127.0.0.1:8080/springbootMybatis/redisController/listUser

 以上这3个示例,都是将值先存放入缓存中,再取出.下面我们直接从缓存中取值

访问4:  http://127.0.0.1:8080/springbootMybatis/redisController/listUser1

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值