码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot配置redis

时间:2019-04-06 18:44:00      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:lan   16px   ast   height   desc   auto   auth   string   system   

  1. 配置pom.xml文件,引入如下依赖
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

     

  2. 配置application文件

            2.1 如果是application.yml文件,则配置如下

server:
  port: 8080

spring:
    datasource:
        name: test
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: lch123
        driver-class-name: com.mysql.jdbc.Driver
        
    redis:
     host: 127.0.0.1
     port: 6379
     database: 1
     timeout: 20000
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.ocean.pojo
  

         2.2 如果是application.properties文件,则配置如下

# redis相关配置
spring.redis.database=0  
spring.redis.host=localhost
spring.redis.port=6379  
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
#项目服务端口号配置
server.port=8810

3.添加配置类,配置类代码如下

package com.ocean.redis;

import java.lang.reflect.Method;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
	
	@Bean
	public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }
}

  4.写一个方法进行测试

/**
 * <p>Title: TestRedis.java</p>
 * <p>Description: TODO</p>
 * <p>Copyright: Copyright (c) 2018</p>
 * @author lch
 * @date 2019年4月6日
 */
package com.ocean.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title: TestRedis.java</p>
 * <p>Description: TODO</p>
 * @author lch
 * @time 2019年4月6日 下午5:08:20
 * @version 1.0
 */
@RestController
@RequestMapping("/redis")
public class TestRedis {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = {
            "/test"
    }, produces = {
            "application/json;charset=UTF-8"
    }, method = RequestMethod.GET)
    public boolean getAllUsers() {
        stringRedisTemplate.opsForValue().set("aaa", "111");
        System.out.println("11");
        return true;
    }

}

  5.调接口测试

 localhost:8080/redis/test

6.结果

技术图片

  

 

we1.       配置pom.xml文件,引入依赖

 

Springboot配置redis

标签:lan   16px   ast   height   desc   auto   auth   string   system   

原文地址:https://www.cnblogs.com/lchzlp/p/10662281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!