springboot(二)集成redis

相隔好久,我又回來了,獻上一篇整合redis 的教程給新手看一下吧,技術點不怎麼有,最簡單的配置,入手即用,那下面開始java

本章在個人上一篇文章爲基礎繼續整合的,不知道的能夠見個人整合tkmapper
springboot整合tkmapper
一、下面開始引入pom依賴web

<!--  springboot整合 redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

二、yml文件根據本身配置添加redis信息redis

spring: 
    redis:
        host: 127.0.0.1
        database: 0
        password: redis
        port: 6379
        timeout: 60000

三、HelloController代碼,用戶訪問接口測試redisspring

package com.cxt.demo.controller;

import com.cxt.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liBai
 * @Classname HelloController
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
@RestController
@RequestMapping("/test")
public class HelloController {
    @Autowired
    private HelloService helloService;
    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello();
    }
    @RequestMapping("/get")
    public String getRedisInfo(){
        return helloService.getRedisInfo();
    }
}

四、HelloService代碼瀏覽器

package com.cxt.demo.service;

/**
 * @author liBai
 * @Classname HelloService
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
public interface HelloService {
    String sayHello();

    String getRedisInfo();

}

implspringboot

package com.cxt.demo.service.impl;

import com.cxt.demo.bean.TestSys;
import com.cxt.demo.dao.TestSysMapper;
import com.cxt.demo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liBai
 * @Classname HelloServiceImpl
 * @Description TODO
 * @Date 2019-06-02 10:50
 */
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
    @Autowired
    private TestSysMapper testSysMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String sayHello() {
        TestSys testSys = testSysMapper.selectByPrimaryKey("1");
        redisTemplate.opsForValue().set("testSys",testSys.toString(),10,TimeUnit.MINUTES);
        log.info("redis set value =" +testSys.toString());
        return "redis set value success ,userName = "+testSys.getName();
    }

    @Override
    public String getRedisInfo() {
        log.debug("redis get info {}",redisTemplate.opsForValue().get("testSys"));
        return redisTemplate.opsForValue().get("testSys");
    }
}

五、到這初級版本整合就能夠使用了,正常項目中不可能這麼簡單使用redis,畢竟redis的用處仍是很強大的,例如單機,集羣,哨兵等,具體的配置就本身挖掘吧,或者繼續關注我後續的文章,下面就來演示一下這節的成果吧
瀏覽器訪問
http://localhost:8081/test/hello
在這裏插入圖片描述
上面已經看到redis放入字符串已經放進去了,下面就拿出來試試吧
訪問
http://localhost:8081/test/get
在這裏插入圖片描述app

到了這基本也就結束了, 有問題歡迎留言討論,若有錯誤請指出一塊兒交流,謝謝!ide

相關文章
相關標籤/搜索