• 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中

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

  • 相关阅读:
    三线程连续打印ABC
    Mybatis知识点总结
    【刷题】LeetCode 292 Nim Game
    Java界面编程-建立一个可以画出图形的简单框架
    第11章 持有对象
    第10章 内部类
    构造器和多态(Chapter8.3)
    对象的创建过程(chapter5.7.3)
    静态数据的初始化(Chapter5.7.2)
    final关键字
  • 原文地址:https://www.cnblogs.com/lysongbo/p/9507143.html
Copyright © 2020-2023  润新知