导入项目模板
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;
@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