• SpringBoot整合Redis


    SpringBoot整合Redis步骤:

      1.在pom.xml添加依赖

    <!--springboot整合redis相关依赖引入-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

     

     2.在application.yml配置文件中配置redis的ip和端口

    #Redis配置
    spring: 
      redis:
        host: 192.168.175.15
        port: 6379
    

      

      3.在测试代码代码中,使用RedisTemplate对象实现redis操作

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = DemoApplication.class)
    public class TestDemo {
        //SpringBoot整合Redis
        //1.获取RedisTemplate对象
        @Autowired
        private RedisTemplate redisTemplate;
    
     //测试方法
        @Test
        public void getRedis(){
            redisTemplate.opsForValue().set("Hello","ni hao");
            String hello = (String)redisTemplate.opsForValue().get("Hello");
            System.out.println(hello);
    
            MyUser myUser = new MyUser();
            myUser.setId(3);
            myUser.setName("哈哈");
            myUser.setPassword("123456");
            myUser.setUsername("haha");
            redisTemplate.opsForValue().set("myUser",myUser);
            MyUser myUser1 = (MyUser)redisTemplate.opsForValue().get("myUser");
            System.out.println(myUser1);
    
        }
    
    }
    

       4.查看运行结果

  • 相关阅读:
    创建大顶堆
    大顶堆的实现
    二叉树的前序,中序,后序,层序实现
    链表实现多项式相加
    225. Implement Stack using Queues
    232. Implement Queue using Stacks
    JavaScript 实现队列操作
    Vue 路由守卫
    回文数 & {}[]() 符号判断

  • 原文地址:https://www.cnblogs.com/gxh494/p/11803635.html
Copyright © 2020-2023  润新知