spring boot 之 redis
来源:互联网 发布:学校网络下载不了 编辑:程序博客网 时间:2024/06/16 10:35
导入项目模板
2.dao
3.Controller
1.导入spring boot redis项目
http://start.spring.io/
选择 web redis
2.导入到eclipse
3.编辑HelloController
@RequestMapping("/") public String helloworld(){ logger.debug("访问hello"); return "Hello world!"; } @RequestMapping("/hello/{name}") public String helloName(@PathVariable String name){ logger.debug("访问helloName,Name={}",name); return "Hello "+name; }4.测试验证
http://localhost:8080/
http://localhost:8080/hello/测试用户StringRedisTemplate
1.StringRedisController
import javax.annotation.Resource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class StringRedisController {protected static Logger logger=LoggerFactory.getLogger(StringRedisController.class);@AutowiredStringRedisTemplate stringRedisTemplate;@Resource(name="stringRedisTemplate")ValueOperations<String,String> valOpsStr;@RequestMapping("set")public String setKeyAndValue(String key,String value){logger.debug("访问set:key={},value={}",key,value);valOpsStr.set(key, value);return "set ok";}@RequestMapping("get")public String getKey(String key){logger.debug("访问get:key={}",key);return valOpsStr.get(key);}}
2.配置文件 application.properties
#redis数据库名称 从0到15,默认为db0spring.redis.database=1#redis服务器名称spring.redis.host=127.0.0.1#redis服务器密码spring.redis.password=123456#redis服务器连接端口号spring.redis.port=6379#redis连接池设置spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1#spring.redis.sentinel.master=#spring.redis.sentinel.nodes=spring.redis.timeout=600003.启动运行 测试
http://localhost:8080/set?key=lxh2&&value=1001
http://localhost:8080/get?key=lxh2
RedisTemplate
1.domain
import java.io.Serializable;public class Person implements Serializable { private static final long serialVersionUID = 1L; private String id; private String name; private Integer age; public Person() { super(); } public Person(String id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
2.dao
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Repository;import com.example.springbootsampleredis.domain.Person; @Repository public class PersonDao { @Autowired RedisTemplate<Object,Object> redisTemplate; @Resource(name="redisTemplate") ValueOperations<Object,Object> valOps; public void save(Person person){ valOps.set(person.getId(), person); } public Person getPerson(String id){ return (Person) valOps.get(id); } }
3.Controller
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.example.springbootsampleredis.dao.PersonDao;import com.example.springbootsampleredis.domain.Person; @RestController public class ObjectRedisController { protected static Logger logger=LoggerFactory.getLogger(ObjectRedisController.class); @Autowired PersonDao personDao; @RequestMapping("/setPerson") public void set(String id,String name,Integer age){ logger.debug("访问setPerson:id={},name={},age={}",id,name,age); Person person=new Person(id,name,age); personDao.save(person); } @RequestMapping("/getPerson") public Person getPerson(String id){ return personDao.getPerson(id); } }
4.测试
http://localhost:8080/setPerson?id=2&&name=lxh2&&age=2
http://localhost:8080/getPerson?id=2
项目目录结构
项目
链接: https://pan.baidu.com/s/1dFALRDv 密码: krmu
参考 http://blog.csdn.net/lxhjh/article/details/51753852
阅读全文
0 0
- spring boot之redis
- spring boot 之 redis
- spring boot之缓存Redis
- Spring boot系列--redis使用之1
- spring boot之Session实现集群-redis
- Spring boot 之 Cache With Redis
- spring boot使用redis
- spring boot session redis
- spring boot使用redis
- spring-boot redis 配置
- Spring boot + redis 项目
- Spring Boot Redis 集成
- redis-10-spring-boot
- spring boot+redis
- [spring-boot] 使用redis
- spring boot整合redis
- Spring Boot集成Redis
- spring-boot 集成 redis
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1) ABCDF
- EasyDSS高性能流媒体服务器前端重构(六)- webpack-dev-server 支持手机端访问
- 数据处理收集
- leet code 003:Longest Substring Without Repeating Characters
- TCP/IP的开肠破肚(2)
- spring boot 之 redis
- Linux防火墙(Iptables)的开启与关闭
- 网站建设时需要避免这些心理
- 【总结】】maven错误及解决,持续更新~
- jQuery UI 实例
- 利用HorizontalScrollView自己写一个viewPager指示器
- 技术文章 | Python和Ruby两大语言全方位对比
- LeetCode算法题——Roman to Integer
- 文件系统---代码层次深入分析文件系统