spring boot 2.x访问redis数据库

1 win7安装redis数据库

去网址https://github.com/MSOpenTech/redis/releases下载最新的redis版本文件Redis-x64-3.2.100.zip,解压缩,然后再cmd命令行中进入到该目录,执行命令 redis-server.exe redis.windows.conf 来启动redis数据库:

D:\Software\Redis-x64-3.2.100>redis-server.exe redis.windows.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.100 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7536
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[7536] 06 Sep 17:27:59.194 # Server started, Redis version 3.2.100
[7536] 06 Sep 17:27:59.195 * The server is now ready to accept connections on po
rt 6379

2 pom.xml中添加redis的依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3 application.properties中添加配置

spring.redis.database=0 
spring.redis.host=127.0.0.1 
spring.redis.port=6379 
spring.redis.password=
spring.redis.jedis.pool.max-idle=60 
spring.redis.jedis.pool.min-idle=30 
spring.redis.jedis.pool.max-active=200 
spring.redis.jedis.pool.max-wait=60000ms
spring.redis.timeout=5000ms

4 编写配置类

package com.example.redis;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@EnableCaching
public class RedisConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        CacheManager cacheManager = RedisCacheManager.create(factory);
        return cacheManager;
    }
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

}

5 编写实体类

package com.example.redis;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 3221700752972709820L;
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public User(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

}

6 编写服务类

package com.example.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public void set(String key, User user) {
        redisTemplate.opsForValue().set(key, user);
    }
    public User get(String key) {
        return (User) redisTemplate.boundValueOps(key).get();
    }
    public void setCode(String key, String code) {
        stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS);
    }
    public String getCode(String key) {
        return stringRedisTemplate.boundValueOps(key).get();
    }

}

7 编写控制类

package com.example.redis;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("redis")
public class RedisController {
    @Resource
    private UserService userService;
    @GetMapping("set")
    public void set() {
        userService.set("key1", new User(1, "meepoguan", 26));
    }
    @GetMapping("get")
    public String get() {
        return userService.get("key1").getName();
    }
    @GetMapping("stringset")
    public void stringset() {
        userService.setCode("stringkey", "meepoguan_coke");
    }
    @GetMapping("stringget")
    public String stringget() {
        return userService.getCode("stringkey");
    }

}

8 测试

运行项目,在浏览器中输入http://localhost:8080/redis/set回车,然后再访问http://localhost:8080/redis/get,得到结果

在浏览器中输入http://localhost:8080/redis/stringset并回车,然后再访问http://localhost:8080/redis/stringget得到:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值