缓存Redis Demo

Redis 简单案例
1.在pox.xml中添加对Redis的依赖
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
2.添加一个实体对象Person
//必须序列化对象
public class Person implements Serializable{

    private String name;
    private String password;
    private int age;

    public Person() {
        super();
    }

    public Person(String name, String password, int age) {
        super();
        this.name = name;
        this.password = password;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
3.添加一个PersonDao

@Repository
public class PersonDao {
//    @Autowired
//    StringRedisTemplate stringRedisTemplate; //1
    private StringRedisTemplate stringRedisTemplate;

    public StringRedisTemplate getStringRedisTemplate() {
        return stringRedisTemplate;
    }

    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }


    @Resource(name = "stringRedisTemplate")
    ValueOperations<String, String> valOpsStr; //3




    @Autowired
    RedisTemplate<Object, Object> redisTemplate; //2

    @Resource(name = "redisTemplate")
    ValueOperations<Object, Object> valOps; //4

    public void stringRedisTemplateDemo() { //5
        valOpsStr.set("xx", "dairui");
    }


    public void save(Person person) { //6
        valOps.set(person.getName(), person);
    }

    public String getString() {//7
        return valOpsStr.get("xx");
    }

    public Person getPerson() {//8
        return (Person) valOps.get("1");
    }
}
4.创建DataController类
@RestController
public class DateController {
    @Autowired
    PersonDao personDao;

    @RequestMapping("/set") //1
    public void set(){
        Person person = new Person("1","wyf", 32);
        personDao.save(person);
        personDao.stringRedisTemplateDemo();
    }

    @RequestMapping("/getStr") //2
    public String getStr(){
        return personDao.getString();
    }

    @RequestMapping("/getPerson") //3
    public Person getPerson(){
        return personDao.getPerson();
    }

5.Application类
@SpringBootApplication
public class RedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }

//    @Bean
//    @SuppressWarnings({"rawtypes", "unchecked"})
//    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
//            throws UnknownHostException {
//        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
//        template.setConnectionFactory(redisConnectionFactory);
//
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//
//        template.setValueSerializer(jackson2JsonRedisSerializer); //1
//        template.setKeySerializer(new StringRedisSerializer()); //2
//
//        template.afterPropertiesSet();
//        return template;
//    }

}

猜你喜欢

转载自blog.csdn.net/sinat_32856935/article/details/81984253