From 35e93d1449b6c9cf532bb6538a6838cbed2390c4 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Mon, 24 Nov 2025 11:14:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=8D=E7=A9=B9=E5=A4=96=E5=8D=96--springCac?= =?UTF-8?q?he-@CachePut=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springcache-demo - inmind/pom.xml | 92 +++++++++++++++++++ springcache-demo - inmind/springcachedemo.sql | 7 ++ .../java/com/inmind/CacheDemoApplication.java | 16 ++++ .../inmind/config/WebMvcConfiguration.java | 53 +++++++++++ .../com/inmind/controller/UserController.java | 53 +++++++++++ .../src/main/java/com/inmind/entity/User.java | 17 ++++ .../java/com/inmind/mapper/UserMapper.java | 21 +++++ .../src/main/resources/application.yml | 21 +++++ 8 files changed, 280 insertions(+) create mode 100644 springcache-demo - inmind/pom.xml create mode 100644 springcache-demo - inmind/springcachedemo.sql create mode 100644 springcache-demo - inmind/src/main/java/com/inmind/CacheDemoApplication.java create mode 100644 springcache-demo - inmind/src/main/java/com/inmind/config/WebMvcConfiguration.java create mode 100644 springcache-demo - inmind/src/main/java/com/inmind/controller/UserController.java create mode 100644 springcache-demo - inmind/src/main/java/com/inmind/entity/User.java create mode 100644 springcache-demo - inmind/src/main/java/com/inmind/mapper/UserMapper.java create mode 100644 springcache-demo - inmind/src/main/resources/application.yml diff --git a/springcache-demo - inmind/pom.xml b/springcache-demo - inmind/pom.xml new file mode 100644 index 0000000..907d76c --- /dev/null +++ b/springcache-demo - inmind/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.3 + + + com.itheima + springcache-demo + 1.0-SNAPSHOT + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter-web + compile + + + org.projectlombok + lombok + 1.18.20 + + + + com.alibaba + fastjson + 1.2.76 + + + + commons-lang + commons-lang + 2.6 + + + org.springframework.boot + spring-boot-starter-cache + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + mysql + mysql-connector-java + runtime + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.0 + + + + com.alibaba + druid-spring-boot-starter + 1.2.1 + + + + com.github.xiaoymin + knife4j-spring-boot-starter + 3.0.2 + + + + org.springframework.boot + spring-boot-starter-test + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.7.3 + + + + diff --git a/springcache-demo - inmind/springcachedemo.sql b/springcache-demo - inmind/springcachedemo.sql new file mode 100644 index 0000000..3003d73 --- /dev/null +++ b/springcache-demo - inmind/springcachedemo.sql @@ -0,0 +1,7 @@ +DROP TABLE IF EXISTS `user`; +CREATE TABLE `user` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `name` varchar(45) DEFAULT NULL, + `age` int DEFAULT NULL, + PRIMARY KEY (`id`) +); diff --git a/springcache-demo - inmind/src/main/java/com/inmind/CacheDemoApplication.java b/springcache-demo - inmind/src/main/java/com/inmind/CacheDemoApplication.java new file mode 100644 index 0000000..aa7beab --- /dev/null +++ b/springcache-demo - inmind/src/main/java/com/inmind/CacheDemoApplication.java @@ -0,0 +1,16 @@ +package com.inmind; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@Slf4j +@SpringBootApplication +@EnableCaching +public class CacheDemoApplication { + public static void main(String[] args) { + SpringApplication.run(CacheDemoApplication.class,args); + log.info("项目启动成功..."); + } +} diff --git a/springcache-demo - inmind/src/main/java/com/inmind/config/WebMvcConfiguration.java b/springcache-demo - inmind/src/main/java/com/inmind/config/WebMvcConfiguration.java new file mode 100644 index 0000000..389dba3 --- /dev/null +++ b/springcache-demo - inmind/src/main/java/com/inmind/config/WebMvcConfiguration.java @@ -0,0 +1,53 @@ +package com.inmind.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.inmind.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/"); + } +} diff --git a/springcache-demo - inmind/src/main/java/com/inmind/controller/UserController.java b/springcache-demo - inmind/src/main/java/com/inmind/controller/UserController.java new file mode 100644 index 0000000..a2bd541 --- /dev/null +++ b/springcache-demo - inmind/src/main/java/com/inmind/controller/UserController.java @@ -0,0 +1,53 @@ +package com.inmind.controller; + +import com.inmind.entity.User; +import com.inmind.mapper.UserMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CachePut; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/user") +@Slf4j +public class UserController { + + @Autowired + private UserMapper userMapper; + + + /* + * + * 业务:在新增用户时,将用户信息缓存到redis中 + * + * Spring Expression Language(SpEL) + * + */ + @PostMapping +// @CachePut(cacheNames = "userCache",key = "#user.id")//如果使用springCache缓存数据,key的生成的规则:userCache::动态ID + @CachePut(cacheNames = "userCache",key = "#result.id")//对象导航,result就表示该方法的返回值!!! +// @CachePut(cacheNames = "userCache",key = "#p0.id")//p0:param0 参数列表中第一个参数 +// @CachePut(cacheNames = "userCache",key = "#a0.id")//a0:argment0 参数列表中第一个参数 +// @CachePut(cacheNames = "userCache",key = "#root.args[0].id")//root.args[0]:save方法的参数列表的第0个参数 + public User save(@RequestBody User user){ + userMapper.insert(user); + return user; + } + + @DeleteMapping + public void deleteById(Long id){ + userMapper.deleteById(id); + } + + @DeleteMapping("/delAll") + public void deleteAll(){ + userMapper.deleteAll(); + } + + @GetMapping + public User getById(Long id){ + User user = userMapper.getById(id); + return user; + } + +} diff --git a/springcache-demo - inmind/src/main/java/com/inmind/entity/User.java b/springcache-demo - inmind/src/main/java/com/inmind/entity/User.java new file mode 100644 index 0000000..4e433c7 --- /dev/null +++ b/springcache-demo - inmind/src/main/java/com/inmind/entity/User.java @@ -0,0 +1,17 @@ +package com.inmind.entity; + +import lombok.Data; +import java.io.Serializable; + +@Data +public class User implements Serializable { + + private static final long serialVersionUID = 1L; + + private Long id; + + private String name; + + private int age; + +} diff --git a/springcache-demo - inmind/src/main/java/com/inmind/mapper/UserMapper.java b/springcache-demo - inmind/src/main/java/com/inmind/mapper/UserMapper.java new file mode 100644 index 0000000..f3a0ac5 --- /dev/null +++ b/springcache-demo - inmind/src/main/java/com/inmind/mapper/UserMapper.java @@ -0,0 +1,21 @@ +package com.inmind.mapper; + +import com.inmind.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); +} diff --git a/springcache-demo - inmind/src/main/resources/application.yml b/springcache-demo - inmind/src/main/resources/application.yml new file mode 100644 index 0000000..c07e212 --- /dev/null +++ b/springcache-demo - inmind/src/main/resources/application.yml @@ -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: 1234 + redis: + host: localhost + port: 6379 +# password: 123456 + database: 1 +logging: + level: + com: + inmind: + mapper: debug + service: info + controller: info