zoukankan      html  css  js  c++  java
  • springboot与redis

    ---恢复内容开始---

    项目结构

    gradle配置文件:

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-cache')
        compile('org.springframework.boot:spring-boot-starter-data-redis')
        compile('org.springframework.boot:spring-boot-starter-web')
        compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
        compile('org.springframework.boot:spring-boot-starter-jdbc')
        runtime('mysql:mysql-connector-java')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

    application.yml

    spring:
       datasource:
        url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver
       redis:
         host: 140.143.1.xx  //ip地址
    mybatis:
      configuration:
        map-underscore-to-camel-case: true #开启驼峰命名匹配
    

     

    student实体类

    public class Student implements Serializable {
        private Integer id;
        private String name;
        private Integer age;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer 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;
        }
    }
    View Code

    dao

    @Mapper
    public interface StudentMapper {
        @Select("select * from t_student where id=#{id}")
        Student selectById(@Param("id") Integer id);
    }
    View Code

    service

    @Service
    public class StudentServiceImpl implements IStudentService {
        @Autowired
        StudentMapper studentMapper;
    
        @Cacheable(cacheNames = "student", key = "#id")
        @Override
        public Student getById(Integer id) {
            System.out.println("查询" + id + "号学生");
            return studentMapper.selectById(id);
        }
    }
    View Code

    controller

    @RestController
    public class StudentController {
    
        @Autowired
        IStudentService iStudentService;
    
        @GetMapping("/stu/{id}")
        public Student getById(@PathVariable("id") Integer id) {
            return iStudentService.getById(id);
        }
    }
    View Code
    @SpringBootApplication
    @MapperScan("com.iteng.springbootredis.mapper")
    @EnableCaching //开启基于注解的缓存
    public class SpringbootRedisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootRedisApplication.class, args);
        }
    }

    第一次访问:http://localhost:8080/stu/1

    浏览器显示:

    控制台输出:

     再次访问:http://localhost:8080/stu/1

     可以看到控制没有再次输出,证明第二次查询没有查询数据库,而是从缓存中查询

    打开redis客户端:可以看到数据已经存入redis中

    ---恢复内容结束---

  • 相关阅读:
    专业词汇-数学-运算:四则运算
    专业词汇-数学-运算:逆运算
    专业词汇-数学:运算
    DNF Package Management-CentOS 8
    Change the HostName of CentOS 8
    CentOS8 修改SSH端口,禁用root登录,修改SSH协议
    CentOS8 Disable IPV6 and Selinux
    Ubuntu 20.04 Install SSH, Change SSH Port, Enable root
    ubuntu 20.04 重启网卡服务
    Ubuntu 20.04 Install Guest Additions for VirtualBox
  • 原文地址:https://www.cnblogs.com/lysongbo/p/9507143.html
Copyright © 2011-2022 走看看