diff --git a/docs/.vuepress/public/images/springboot/0b94988b3cde463ed16ca1edec244c1e.png b/docs/.vuepress/public/images/springboot/0b94988b3cde463ed16ca1edec244c1e.png new file mode 100644 index 00000000000..ae4f87849ca Binary files /dev/null and b/docs/.vuepress/public/images/springboot/0b94988b3cde463ed16ca1edec244c1e.png differ diff --git a/docs/.vuepress/public/images/springboot/image-20231222191923432.png b/docs/.vuepress/public/images/springboot/image-20231222191923432.png new file mode 100644 index 00000000000..3fd8469d55e Binary files /dev/null and b/docs/.vuepress/public/images/springboot/image-20231222191923432.png differ diff --git a/docs/.vuepress/public/images/springboot/image-20231222193138072.png b/docs/.vuepress/public/images/springboot/image-20231222193138072.png new file mode 100644 index 00000000000..5244502eb24 Binary files /dev/null and b/docs/.vuepress/public/images/springboot/image-20231222193138072.png differ diff --git a/docs/.vuepress/public/images/springboot/image-20231222194901593.png b/docs/.vuepress/public/images/springboot/image-20231222194901593.png new file mode 100644 index 00000000000..84c11e1a8b2 Binary files /dev/null and b/docs/.vuepress/public/images/springboot/image-20231222194901593.png differ diff --git a/docs/.vuepress/public/images/springboot/image-20231222201249988.png b/docs/.vuepress/public/images/springboot/image-20231222201249988.png new file mode 100644 index 00000000000..6b5cbdfa434 Binary files /dev/null and b/docs/.vuepress/public/images/springboot/image-20231222201249988.png differ diff --git "a/docs/Redis/01 \345\256\211\350\243\205Redis.md" "b/docs/Redis/01 \345\256\211\350\243\205Redis.md" index d1b27dd4ad8..24e18aba290 100644 --- "a/docs/Redis/01 \345\256\211\350\243\205Redis.md" +++ "b/docs/Redis/01 \345\256\211\350\243\205Redis.md" @@ -98,7 +98,7 @@ firewall-cmd --reload > redis-server 指定不同的配置文件,可以在一台机器上启动多个实例 ```sh -cd /usr/redis/redis-6.2.6 +cd /usr/local/redis/redis-6.2.6 # 启动服务 src/redis-server redis.conf ``` diff --git "a/docs/java/58 \347\251\272\345\255\227\347\254\246\344\270\262.md" "b/docs/java/58 \347\251\272\345\255\227\347\254\246\344\270\262.md" new file mode 100644 index 00000000000..ee7327bfde3 --- /dev/null +++ "b/docs/java/58 \347\251\272\345\255\227\347\254\246\344\270\262.md" @@ -0,0 +1,72 @@ +--- +sidebarDepth: 3 +sidebar: auto +prev: + text: Back To 目录 + link: /java/ +typora-root-url: ..\.vuepress\public +--- + + + +unicode为12288字符为全角空格,trim()无法去除,去除方法如下: + +```java +str = str.replace((char) 12288, ' '); +str=str.trim(); +``` + + + +```java +public class StringUtils { + + public static void main(String[] args) { + String str = " "; + printStrAscii(str); + char space = 12288; + String strTwo = "" + space; + printStrAscii(strTwo); + + str = str.trim(); + strTwo = strTwo.trim(); + System.out.println(str.length()); + System.out.println(strTwo.length()); + } + + private static void printStrAscii(String str) { + System.out.print(str + " : "); + for(int i=0; i 判断为是否空字符串 + +```java +/** + * 判断是否为null或空字符 + */ + public static boolean isNullOrEmpty(Object o) { + if (o == null) { + return true; + } + if (String.valueOf(o).replace((char) 12288, ' ').trim().length() == 0) { + return true; + } + if ("null".equals(o)) { + return true; + } + return false; + } +``` + diff --git "a/docs/springboot/15 openapi\346\226\207\346\241\243\345\267\245\345\205\267.md" "b/docs/springboot/15 openapi\346\226\207\346\241\243\345\267\245\345\205\267.md" index 7f82de427e2..c3b67f3b829 100644 --- "a/docs/springboot/15 openapi\346\226\207\346\241\243\345\267\245\345\205\267.md" +++ "b/docs/springboot/15 openapi\346\226\207\346\241\243\345\267\245\345\205\267.md" @@ -57,9 +57,25 @@ typora-root-url: ..\.vuepress\public ![image-20230603115651236](/images/springboot/image-20230603115651236.png) +引入依赖在Controller上配置相关信息就可以访问了`http://localhost:8082/swagger-ui/index.html`,没有配置context-path,默认就是`/` + +```java +@RestController +@RequestMapping("/account") +@Tag(name = "管理界面用户管理",description = "用户管理登录等等") +@Slf4j +public class AccountController extends BaseController{ + @Operation(summary = "保存用户信息", description = "保存用户信息到数据库") + @PostMapping("/save") + public void save() { + throw new NotImplementedException("接口未实现"); + } +} +``` + -## 额外信息配置 +## 额外信息配置(非强制) ![image-20230522135222296](/images/springboot/image-20230522135222296.png) diff --git a/docs/springboot/24 springboot cache.md b/docs/springboot/24 springboot cache.md new file mode 100644 index 00000000000..7f16bb16b01 --- /dev/null +++ b/docs/springboot/24 springboot cache.md @@ -0,0 +1,170 @@ +--- +sidebarDepth: 3 +sidebar: auto +prev: + text: Back To 目录 + link: /springboot/ +typora-root-url: ..\.vuepress\public +--- + +## spring + +在spring中就已经引入了本地缓存,spring-context中就有了`CacheEvict`等注解 + +在springboot中,自动配置类中就配置了ConcurrentMapCacheManager,默认的。 + +下面这个依赖,方便引入其他缓存,如RedisCache,Ehcache等 + +``` + + org.springframework.boot + spring-boot-starter-cache + +``` + + + +## 简单使用 + +Spring 从 3.1 开始就引入了对 Cache 的支持。定义了 `org.springframework.cache.Cache` 和 `org.springframework.cache.CacheManager` 接口来统一不同的缓存技术。并支持使用 `JCache(JSR-107)`注解简化我们的开发。 + +其使用方法和原理都类似于 Spring 对事务管理的支持。Spring Cache 是作用在方法上的,其核心思想是,当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存在缓存中。 + +```xml + + org.springframework.boot + spring-boot-starter + +``` + +引入了springboot,就可以使用缓存了 + +1. 开启基于注解的缓存,使用 `@EnableCaching` 标识在 SpringBoot 的主启动类上。 + +2. 标注缓存注解即可 + +```java +@SpringBootApplication +@EnableCaching // 开启基于注解的缓存 +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class); + } +} +``` + +```java +@Component +@Log +public class SimpleRunner implements CommandLineRunner, ApplicationContextAware { + private ApplicationContext applicationContext; + @Override + public void run(String... args) throws Exception { + log.info("-----------msg-------------------"); + /** + * 因为spring的缓存是通过代理实现的,类似aop.被Spring包装了。 + * 在该类方法直接执行,不会走代理,所以我们需要拿到代理类 + */ + SimpleRunner simpleRunner = applicationContext.getBean(SimpleRunner.class); + + log.info(simpleRunner.getData(1)); + log.info(simpleRunner.getData(1)); + log.info(simpleRunner.getData(1)); + } + + private AtomicInteger count = new AtomicInteger(0); + @Cacheable(cacheNames = "emps") + public String getData(Integer id){ + return "msg"+count.incrementAndGet(); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} +``` + +![image-20231222191923432](/images/springboot/image-20231222191923432.png) + +### 常用属性说明 + + + +#### 常用属性说明 + +下面介绍一下 `@Cacheable `这个注解常用的几个属性: + +> - `cacheNames/value` :用来指定缓存组件的名字 +> - `key` :缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写) +> - `keyGenerator` :key 的生成器。 key 和 keyGenerator 二选一使用 +> - `cacheManager` :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。 +> - `condition` :可以用来指定符合条件的情况下才缓存 +> - `unless` :否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过 `#result` 获取方法结果) +> - `sync` :是否使用异步模式。 + +#### spEL 编写 key + +前面说过,缓存的 key 支持使用 spEL 表达式去编写,下面总结一下使用 spEL 去编写 key 可以用的一些元数据: + +![img](/images/springboot/0b94988b3cde463ed16ca1edec244c1e.png) + +## 引入第三方缓存Redis + +springboot自动配置类配置了`RedisCacheConfiguration`,引入下面的依赖,就会注册`RedisCacheManager`。 + +![image-20231222193138072](/images/springboot/image-20231222193138072.png) + +```xml + + org.springframework.boot + spring-boot-starter-data-redis + +``` + +![image-20231222194901593](/images/springboot/image-20231222194901593.png) + +由于出现乱码,我们需要配置一下,首先需要引入jackson + +```xml + + com.fasterxml.jackson.core + jackson-databind + 2.16.0 + +``` + +```java +@Configuration +public class RedisConfig { + @Bean + @Primary + public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){ + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); + return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build(); + } +} +``` + +![image-20231222201249988](/images/springboot/image-20231222201249988.png) + +```java +@Cacheable(cacheNames = "msg",key = "#root.targetClass.simpleName+':'+#id") +public String getData(Integer id){ + return "msg"+count.incrementAndGet(); +} +``` + + + +## 参考 + +[SpringBoot 缓存之 @Cacheable 详细介绍](https://xie.infoq.cn/article/001e0f5ab65fa7dd1484c51e5) + +[Caching Data with Spring](https://spring.io/guides/gs/caching/) + +[“8个步骤”手把手带你用SpringBoot操作Redis,实现数据缓存](https://cloud.tencent.com/developer/article/1824707) \ No newline at end of file