springboot与redis

本文介绍了一个使用Spring Boot结合MyBatis和Redis实现数据缓存的示例项目。项目通过Gradle进行依赖管理,并配置了MySQL作为数据源。具体实现了学生信息的数据缓存功能,展示了如何在控制器中调用服务层获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

项目结构

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中

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

转载于:https://www.cnblogs.com/lysongbo/p/9507143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值