spring-boot-starter-data-redis

來源:互聯網
上載者:User

GitHub:https://github.com/asd821300801/Spring-Boot/tree/spring-boot-redis 前期準備


建立Spring Boot 工程

......


Maven 加入必要的依賴


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


application.properties


spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=123456spring.redis.database=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.pool.max-idle=500spring.redis.pool.min-idle=0spring.redis.timeout=0


啟動redis



串連redis做相應的資料操作


查看源碼可知 redisTemplate 和 StringRedisTemplate已被自動設定,所以我們直接用就可以

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration.class

RedisDao.java

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Repository;@Repositorypublic class RedisDao {    @Autowired    private StringRedisTemplate template;    public  void setKey(String key,String value){        ValueOperations<String, String> ops = template.opsForValue();        ops.set(key,value);    }    public String getValue(String key){        ValueOperations<String, String> ops = this.template.opsForValue();        return ops.get(key);    }}


RedisController.java

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.dao.RedisDao;@RestControllerpublic class RedisController {    @Autowired    private RedisDao redisDao;    @RequestMapping("/set")    public String set(String key,String value){        redisDao.setKey(key, value);        return "success";    }    @RequestMapping("/get")    public String get(String key){        return redisDao.getValue(key);    }}


訪問測試


設定資料:http://localhost:8080/set?key=lingdu&value=123456

擷取資料:http://localhost:8080/get?key=lingdu

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.