基于注解整合缓存

1.0 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hy</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 支持jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- MySql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.appliaction.properties

spring.datasource.url=jdbc:mysql://localhost:3306/java117?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=hy123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapperLocations=classpath:mapper/*.xml


logging.level.com.hy.test.dao=DEBUG



3.启动类

package com.hy.test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan(basePackages = {"com.hy.test.dao"})
//开启缓存
@EnableCaching
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

4.

package com.hy.test.controller;

import com.hy.test.model.Course;
import com.hy.test.service.UserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/t")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/u")
    @ResponseBody
    @Cacheable(cacheNames = {"user"}) // 表示这个方法使用缓存  缓存的名字叫user
    public  List<Course> getUser(){
        List<Course> courseById = userService.getCourseById();
                System.out.println("访问信息");
        return courseById;
    }

}

5.

package com.hy.test.dao;

import com.hy.test.model.Course;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserDao {

    @Select("Select * from course ")
    List<Course> getCourseById(  );
}

第一次访问 有sql打印说明连接数据库

在这里插入图片描述

,清空控制台再次访问,发现连方法都没进来说明访问的是缓存
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是对这三个实验的总结: 1. Spring Boot 整合 Redis 的基本步骤: Spring Boot 提供了对 Redis 的自动配置支持,可以通过配置文件来配置 Redis 连接参数,然后通过@Autowired注解注入 RedisTemplate 来使用 Redis。Spring Boot 整合 Redis 的基本步骤如下: 1)在pom.xml文件中添加 redis 依赖:spring-boot-starter-data-redis。 2)在 application.properties 文件中添加 Redis 连接参数配置,例如: spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= 3)在 Java 代码中使用 RedisTemplate 操作 Redis,例如: @Autowired private RedisTemplate<String, Object> redisTemplate; ValueOperations<String, Object> operations = redisTemplate.opsForValue(); operations.set("key", "value"); Object value = operations.get("key"); 2. Redis 第三方技术的应用: 除了 Redis 本身提供的数据结构和操作命令,还有许多第三方技术可以应用在 Redis 中,例如: 1)Redisson:基于 Redis 的分布式 Java 对象和服务框架,提供了分布式锁、分布式集合等功能。 2)Lettuce:高性能的 Redis 客户端,提供了异步、响应式等多种操作方式,支持 Redis Sentinel 和 Redis Cluster。 3)Jedis:Redis 的 Java 客户端库,提供了 Redis 的基本操作命令。 4)Spring Data Redis:Spring 提供的 Redis 访问框架,提供了对 Redis 的基本操作和缓存支持。 3. 基于注解的 Redis 缓存实现: Spring Boot 提供了基于注解的 Redis 缓存实现,可以将方法的返回值缓存到 Redis 中,下次调用该方法时,如果缓存中存在对应的数据,则直接返回缓存数据,不再执行方法体逻辑。基于注解的 Redis 缓存实现的基本步骤如下: 1)在 pom.xml 文件中添加 Redis 和 Cache 依赖:spring-boot-starter-data-redis、spring-boot-starter-cache。 2)在 Java 代码中添加 @EnableCaching 注解启用缓存功能。 3)在方法上添加 @Cacheable 注解指定方法返回值需要缓存到 Redis 中,例如: @Cacheable(value = "userCache", key = "#id") public User getUserById(String id) { return userDao.getUserById(id); } 4)在 application.properties 文件中配置 Redis 连接参数和缓存配置,例如: spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.cache.type=redis 通过使用基于注解的 Redis 缓存实现,可以提高应用程序的性能和响应速度,减少对数据库的访问,提高系统的并发能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值