Compare commits

...

3 Commits

@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Zeppelin 忽略的文件
/ZeppelinRemoteNotebooks/

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="springcache-demo" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="springcache-demo" options="-parameters" />
</option>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 定义Maven项目对象模型POM的根元素遵循Maven 4.0.0规范 -->
<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">
<!-- 指定POM的版本为4.0.0 -->
<modelVersion>4.0.0</modelVersion>
<!-- 继承父项目 -->
<parent>
<!-- 指定父项目的groupId、artifactId和version -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<!-- 相对路径这里为空表示查找父POM时不使用相对路径 -->
<relativePath/>
</parent>
<!-- 定义项目的groupId、artifactId和version -->
<groupId>com.itheima</groupId>
<artifactId>springcache-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 定义项目属性 -->
<properties>
<!-- 设置编译的Java版本为11 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<!-- 定义项目依赖 -->
<dependencies>
<!-- Spring Boot Web启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<!-- Lombok依赖用于简化Java对象的getter、setter等方法的编写 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- Alibaba Fastjson依赖用于JSON处理 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!-- Apache Commons Lang依赖提供一些常用的工具类 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Spring Boot缓存启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Spring Boot Redis启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- MySQL JDBC驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- MyBatis Spring Boot启动器依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- Druid Spring Boot启动器依赖用于数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<!-- Knife4j Spring Boot启动器依赖用于API文档生成 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Spring Boot测试启动器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<!-- 构建配置 -->
<build>
<!-- 定义Maven插件 -->
<plugins>
<!-- Spring Boot Maven插件用于打包Spring Boot应用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.3</version>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,14 @@
-- 如果存在名为`user`的表,则删除它
DROP TABLE IF EXISTS `user`;
-- 创建一个名为`user`的新表
CREATE TABLE `user` (
-- 定义一个名为`id`的列类型为bigint不允许为空并且自增
`id` bigint NOT NULL AUTO_INCREMENT,
-- 定义一个名为`name`的列类型为varchar(45),允许为空
`name` varchar(45) DEFAULT NULL,
-- 定义一个名为`age`的列类型为int允许为空
`age` int DEFAULT NULL,
-- 将`id`列设置为表的主键
PRIMARY KEY (`id`)
);

@ -0,0 +1,27 @@
// 定义包名
package com.itheima;
// 导入lombok提供的@Slf4j注解用于日志记录
import lombok.extern.slf4j.Slf4j;
// 导入SpringApplication类用于启动Spring Boot应用
import org.springframework.boot.SpringApplication;
// 导入SpringBootApplication注解标识这是一个Spring Boot应用
import org.springframework.boot.autoconfigure.SpringBootApplication;
// 导入EnableCaching注解用于开启缓存支持
import org.springframework.cache.annotation.EnableCaching;
// 使用@Slf4j注解自动注入日志对象
@Slf4j
// 使用@SpringBootApplication注解标识这是一个Spring Boot自动配置的应用
@SpringBootApplication
// 使用@EnableCaching注解开启缓存功能
@EnableCaching
public class CacheDemoApplication {
// 定义main方法作为程序的入口点
public static void main(String[] args) {
// 调用SpringApplication的run方法启动Spring Boot应用
SpringApplication.run(CacheDemoApplication.class, args);
// 使用注入的日志对象记录项目启动成功的信息
log.info("项目启动成功...");
}
}

@ -0,0 +1,53 @@
package com.itheima.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
/**
*
* @return
*/
@Bean
public Docket docket(){
log.info("准备生成接口文档...");
ApiInfo apiInfo = new ApiInfoBuilder()
.title("接口文档")
.version("2.0")
.description("接口文档")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
//指定生成接口需要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.itheima.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
/**
*
* @param registry
*/
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始设置静态资源映射...");
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

@ -0,0 +1,83 @@
// 定义包名
package com.itheima.controller;
// 导入User实体类
import com.itheima.entity.User;
// 导入UserMapper接口
import com.itheima.mapper.UserMapper;
// 导入Lombok提供的@Slf4j注解用于日志记录
import lombok.extern.slf4j.Slf4j;
// 导入Spring框架的Autowired注解用于自动注入依赖
import org.springframework.beans.factory.annotation.Autowired;
// 导入Spring框架的缓存注解
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
// 导入Spring框架的Web注解
import org.springframework.web.bind.annotation.*;
// 使用RestController注解标识这是一个控制器并且返回的数据直接作为响应体
@RestController
// 使用RequestMapping注解设置这个控制器的基础路径
@RequestMapping("/user")
// 使用@Slf4j注解自动注入日志对象
@Slf4j
public class UserController {
// 使用Autowired注解自动注入UserMapper
@Autowired
private UserMapper userMapper;
/**
*
* @param user
* @return
*/
@PostMapping
// 使用CachePut注解指定在保存用户信息后更新缓存key为用户ID
@CachePut(cacheNames = "userCache",key = "#user.id")
public User save(@RequestBody User user){
// 调用UserMapper的insert方法保存用户信息
userMapper.insert(user);
// 返回保存后的用户对象
return user;
}
/**
* ID
*/
@DeleteMapping
// 使用CacheEvict注解指定在删除用户信息后清除缓存中对应的数据key为用户ID
@CacheEvict(cacheNames = "userCache",key = "#id")
public void deleteById(Long id){
// 调用UserMapper的deleteById方法删除用户信息
userMapper.deleteById(id);
}
/**
*
*/
@DeleteMapping("/delAll")
// 使用CacheEvict注解指定在删除所有用户信息后清除缓存中的所有数据
@CacheEvict(cacheNames = "userCache",allEntries = true)
public void deleteAll(){
// 调用UserMapper的deleteAll方法删除所有用户信息
userMapper.deleteAll();
}
/**
* ID
* @param id ID
* @return
*/
@GetMapping
// 使用Cacheable注解指定在获取用户信息时如果缓存中有则直接返回缓存中的数据key为用户ID
@Cacheable(cacheNames = "userCache",key = "#id")
public User getById(Long id){
// 调用UserMapper的getById方法获取用户信息
User user = userMapper.getById(id);
// 返回用户对象
return user;
}
}

@ -0,0 +1,26 @@
// 定义包名
package com.itheima.entity;
// 导入lombok提供的@Data注解用于简化Java对象的getter、setter、toString等方法的编写
import lombok.Data;
// 导入Serializable接口用于实现对象的序列化
import java.io.Serializable;
// 使用@Data注解自动为类生成getter、setter、toString等方法
@Data
// 声明User类实现Serializable接口以便可以被序列化
public class User implements Serializable {
// 声明serialVersionUID用于在序列化和反序列化过程中保持版本的一致性
private static final long serialVersionUID = 1L;
// 声明id属性用于存储用户的唯一标识
private Long id;
// 声明name属性用于存储用户的姓名
private String name;
// 声明age属性用于存储用户的年龄
private int age;
}

@ -0,0 +1,21 @@
package com.itheima.mapper;
import com.itheima.entity.User;
import org.apache.ibatis.annotations.*;
@Mapper
public interface UserMapper{
@Insert("insert into user(name,age) values (#{name},#{age})")
@Options(useGeneratedKeys = true,keyProperty = "id")
void insert(User user);
@Delete("delete from user where id = #{id}")
void deleteById(Long id);
@Delete("delete from user")
void deleteAll();
@Select("select * from user where id = #{id}")
User getById(Long id);
}

@ -0,0 +1,39 @@
# 定义服务的配置项
server:
# 设置服务的端口号为8888
port: 8888
spring:
# 数据源配置
datasource:
# 配置Druid连接池
druid:
# 指定JDBC驱动类名
driver-class-name: com.mysql.cj.jdbc.Driver
# 指定数据库连接URL
url: jdbc:mysql://localhost:3306/spring_cache_demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
# 指定数据库用户名
username: root
# 指定数据库密码
password: root
# Redis配置
redis:
# 指定Redis服务器地址
host: localhost
# 指定Redis服务器端口
port: 6379
# 指定Redis密码
password: 123456
# 指定Redis数据库索引
database: 1
# 日志配置
logging:
# 设置日志级别
level:
# 设置com.itheima包下的mapper日志级别为debug
com:
itheima:
mapper: debug
# 设置com.itheima包下的service日志级别为info
service: info
# 设置com.itheima包下的controller日志级别为info
controller: info

@ -0,0 +1,21 @@
server:
port: 8888
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_cache_demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
redis:
host: localhost
port: 6379
password: 123456
database: 1
logging:
level:
com:
itheima:
mapper: debug
service: info
controller: info
Loading…
Cancel
Save