欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
1.SpringCache的概念
 
首先我们知道jpa,jdbc这些东西都是一些规范,比如jdbc,要要连接到数据库,都是需要用到数据库连接,预处理,结果集这三个对象,无论是连接到mysql还是oracle都是需要用到这个三个对象的,这是一种规范,而SpringCache是一种作为缓存的规范,具体实现有redis,EhCahe等
 
2.SpringCache用法(redis版)
 
2.1 .SpringCache基本用法
 
1.pom.xml
 
<?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.6.3</version>
 
        <relativePath/> <!-- lookup parent from repository -->
 
    </parent>
 
    <groupId>com.yl</groupId>
 
    <artifactId>cache_redis</artifactId>
 
    <version>0.0.1-SNAPSHOT</version>
 
    <name>cache_redis</name>
 
    <description>Demo project for Spring Boot</description>
 
    <properties>
 
        <java.version>11</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-data-redis</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>
 
    </dependencies>
 
    <build>
 
        <plugins>
 
            <plugin>
 
                <groupId>org.springframework.boot</groupId>
 
                <artifactId>spring-boot-maven-plugin</artifactId>
 
            </plugin>
 
        </plugins>
 
    </build>
 
</project>
 
2.application.properties
 
# redis的配置
 
spring.redis.host=192.168.244.135
 
spring.redis.port=6379
 
spring.redis.password=root123
 
3.实体类
 
package com.yl.cache_redis.domain;
 
import java.io.Serializable;
 
public class User implements Serializable {
 
    private Integer id;
 
    private String username;
 
    private String password;
 
    public Integer getId() {
 
        return id;
 
    }
 
    public void setId(Integer id) {
 
        this.id = id;
 
    }
 
    public String getUsername() {
 
        return username;
 
    }
 
    public void setUsername(String username) {
 
        this.username = username;
 
    }
 
    public String getPassword() {
 
        return password;
 
    }
 
    public void setPassword(String password) {
 
        this.password = password;
 
    }
 
    @Override
 
    public String toString() {
 
        return "User{" +
 
                "id=" + id +
 
                ", username='" + username + '\'' +
 
                ", password='" + password + '\'' +
 
                '}';
 
    }
 
}
 
4.service
 
package com.yl.cache_redis;
 
import com.yl.cache_redis.domain.User;
 
import org.springframework.cache.annotation.Cacheable;
 
import org.springframework.stereotype.Service;
 
@Service
 
public class UserService {
 
    @Cacheable(cacheNames = "u1") //这个注解作用就是将方法的返回值存到缓存中
 
    public User getUserById(Integer id) {
 
        System.out.println("getUserById:" + id);
 
        User user = new User();
 
        user.setId(id);
 
        user.setUsername("root");
 
        user.setPassword("root");
 
        return user;
 
    }
 
}
 
5.主程序,加上开启缓存的注解
 
package com.yl.cache_redis;
 
import org.springframework.boot.SpringApplication;
 
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import org.springframework.cache.annotation.EnableCaching;
 
@SpringBootApplication
 
@EnableCaching //开启缓存功能
 
public class CacheRedisApplication {
 
    public static void main(String[] args) {
 
        SpringApplication.run(CacheRedisApplication.class, args);
 
    }
 
}

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h65209.shtml