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中

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

  • 相关阅读:
    使用SpringAOP获取一次请求流经方法的调用次数和调用耗时
    疫苗之殇与理性应对之道
    【做更好的职场人】理性、弹性、开放的沟通
    使用IntelljIDEA生成接口的类继承图及装饰器模式
    订单导出应对大流量订单导出时的设计问题
    预发和线上的自动化对比工具微框架
    从实战角度看如何构建高质量的软件:一线工程师的一份质量手记
    代码问题及对策
    若干设计经验教训小记
    输入输出无依赖型函数的GroovySpock单测模板的自动生成工具(上)
  • 原文地址:https://www.cnblogs.com/lysongbo/p/9507143.html
Copyright © 2011-2022 走看看