spring boot 之 redis

导入项目模板

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;

@RestController
public class StringRedisController {

	protected static Logger logger=LoggerFactory.getLogger(StringRedisController.class);

	@Autowired
	StringRedisTemplate 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,默认为db0
spring.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=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
spring.redis.timeout=60000
3.启动运行 测试

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值