第十一篇:springboot集成spring cache

版权声明:如需转载,请备注出处 https://blog.csdn.net/u013783065/article/details/81200212

简介:

当我们调用一个缓存方法是会根据相信信息和返回结果作为一个键值对的形式存在缓存之中吗,等待下次利用同样的参数来调用改方法时将不会再次执行该方法,而是直接从缓存中获取结果进行返回。

声明式缓存

spring中定义CacheManager和Cache接口用来同意不通的缓存技术,比如JCache、EhCache、HastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration等。

springboot在我们不使用其他第三方缓存的时候,会自动采用ConcurrenMapCacheMamager作为缓存管理器。

此次我们也将会进行一些前面知识的整合,

创建项目:

引入的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wujie</groupId>
    <artifactId>springboot-cache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-cache</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/dao/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>

利用之前的你想生成工具生成我们的dao以及mapper还有pojo,这里提供下我自己的,我这里是修改了一下源码,对生成的代码进行了一下简单的修改:逆向生成工具传送门

service:

public interface UserService {
    public UserBean findById(Integer id);
}

这里为了测试,只是简单的写了一个查询的方法。

service实现层:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    @Cacheable("user")
    public UserBean findById(Integer id) {
        System.out.println("走数据库");
        return userDao.selectByPrimaryKey(id);
    }
}

这里添加@Cacheable注解,当我们调用此方法时,会先去缓存中查找,如果找得到就会直接进行返回,如果找不到,那就会去数据库中进行查询。

controller层:

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("findById")
    public UserBean findById(Integer id){
        return userService.findById(id);
    }
}

最后我们只需要在启动类上添加@EnableCaching注解,启动程序即可。这次的比较简单,希望大家都能够学会。

有相关问题可以关注我的公众号:咱们一起学习讨论:

springboot-cache源码

请找到先相对应的名称下的源码拷贝。

猜你喜欢

转载自blog.csdn.net/u013783065/article/details/81200212
今日推荐