尚学堂102天总结+springdata-redis

102天
行百里者半九十,想要在一个行业里成为顶尖人才,一定满足一万小时定律,要想学好JAVA,需要持之以恒不断地努力,每天都要勤思考+善于询问+解决问题!

知识温故而知新>>>>>>

Linux下安装solr的教程

555.png

SpringBoot2.2以上版本添加junit进行测试的方法

h111.png

Maven依赖中标签的作用

image.png

./的作用和用法

image.png

启动和关闭redis的命令

image.png

今天学到的知识>>>>>>

com.sxt.springdataredis.config.RedisConfig

package com.sxt.springdataredis.config;

import com.sxt.springdataredis.pojo.Users;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate getRedisTemplate(RedisConnectionFactory factory){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        //更改值序列化器,默认使用JDK的序列化器
        Jackson2JsonRedisSerializer usersSerializer =  new Jackson2JsonRedisSerializer(Users.class);
        redisTemplate.setValueSerializer(usersSerializer);//设置value的序列化器
        redisTemplate.setHashValueSerializer(usersSerializer); //设置hash类型的value序列化器
        //更改键序列化器,默认使用JDK的序列化器
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);//设置value的序列化器
        redisTemplate.setHashKeySerializer(stringSerializer); //设置hash类型的value序列化器
        return redisTemplate;
    }
}

com.sxt.springdataredis.pojo.Users

package com.sxt.springdataredis.pojo;

import java.io.Serializable;

public class Users implements Serializable {
    private int userid;
    private String username;
    private int userage;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getUserage() {
        return userage;
    }

    public void setUserage(int userage) {
        this.userage = userage;
    }

    @Override
    public String toString() {
        return "Users{" +
                "userid=" + userid +
                ", username='" + username + '\'' +
                ", userage=" + userage +
                '}';
    }
}

com.sxt.springdataredis.SpringdataRedisApplicationTests

package com.sxt.springdataredis;

import com.sxt.springdataredis.pojo.Users;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
class SpringdataRedisApplicationTests {

    @Autowired
    StringRedisTemplate stringRedisTemplate;//操作字符串类型的模板
    //@Autowired
    //RedisTemplate redisTemplate;//操作对象类型的模板
    @Autowired
    RedisTemplate redisTemplate;
    //测试字符串类型的存取
    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("hello","spring data redis!");//存值
        String hello = stringRedisTemplate.opsForValue().get("hello");//取值
        System.out.println(hello);
    }
    //测试对象类型的存取
//    @Test
//    public void testObject(){
//        Users users = new Users();
//        users.setUserid(1);
//        users.setUsername("zhangsan");
//        users.setUserage(20);
//        redisTemplate.opsForValue().set("users",users);//存值
//        Object object = redisTemplate.opsForValue().get("users");//取值
//        System.out.println(object);
//    }

    @Test
    public void testObject2(){
        Users users = new Users();
        users.setUserid(1);
        users.setUsername("zhangsan");
        users.setUserage(20);
        redisTemplate.opsForValue().set("users",users);//存值
        Users u = redisTemplate.opsForValue().get("users");//取值
        System.out.println(u);
    }
}

习题的情况总结>>>>>>

你可能感兴趣的:(尚学堂102天总结+springdata-redis)