From 8668cae0615f4c493502252b94dd8fcbb2cac980 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 20 六月 2025 14:22:45 +0800 Subject: [PATCH] 修改common模块 --- se-common/src/main/java/com/terra/common/service/RedisService.java | 265 ++++++++++++++++++++++++++ se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java | 135 +++++++++++++ se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java | 64 ++++++ se-common/pom.xml | 17 + se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java | 53 +++++ se-common/src/main/java/com/terra/common/configure/RedisConfig.java | 43 ++++ se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports | 3 7 files changed, 580 insertions(+), 0 deletions(-) diff --git a/se-common/pom.xml b/se-common/pom.xml index 9327875..a47dd61 100644 --- a/se-common/pom.xml +++ b/se-common/pom.xml @@ -107,6 +107,23 @@ <artifactId>javax.servlet-api</artifactId> </dependency> + <!-- SpringBoot Boot Redis --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-redis</artifactId> + </dependency> + + <!-- SpringBoot Web --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + + <!-- SpringDoc webmvc --> + <dependency> + <groupId>org.springdoc</groupId> + <artifactId>springdoc-openapi-ui</artifactId> + </dependency> </dependencies> </project> diff --git a/se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java b/se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java new file mode 100644 index 0000000..ea766ee --- /dev/null +++ b/se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java @@ -0,0 +1,53 @@ +package com.terra.common.configure; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.filter.Filter; +import com.terra.common.constant.Constants; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; + +/** + * Redis浣跨敤FastJson搴忓垪鍖� + * + * @author ruoyi + */ +public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> +{ + public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + + static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR); + + private Class<T> clazz; + + public FastJson2JsonRedisSerializer(Class<T> clazz) + { + super(); + this.clazz = clazz; + } + + @Override + public byte[] serialize(T t) throws SerializationException + { + if (t == null) + { + return new byte[0]; + } + return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException + { + if (bytes == null || bytes.length <= 0) + { + return null; + } + String str = new String(bytes, DEFAULT_CHARSET); + + return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); + } +} diff --git a/se-common/src/main/java/com/terra/common/configure/RedisConfig.java b/se-common/src/main/java/com/terra/common/configure/RedisConfig.java new file mode 100644 index 0000000..b548dd3 --- /dev/null +++ b/se-common/src/main/java/com/terra/common/configure/RedisConfig.java @@ -0,0 +1,43 @@ +package com.terra.common.configure; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * redis閰嶇疆 + * + * @author ruoyi + */ +@Configuration +@EnableCaching +@AutoConfigureBefore(RedisAutoConfiguration.class) +public class RedisConfig extends CachingConfigurerSupport +{ + @Bean + @SuppressWarnings(value = { "unchecked", "rawtypes" }) + public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) + { + RedisTemplate<Object, Object> template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + + FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); + + // 浣跨敤StringRedisSerializer鏉ュ簭鍒楀寲鍜屽弽搴忓垪鍖杛edis鐨刱ey鍊� + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(serializer); + + // Hash鐨刱ey涔熼噰鐢⊿tringRedisSerializer鐨勫簭鍒楀寲鏂瑰紡 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(serializer); + + template.afterPropertiesSet(); + return template; + } +} diff --git a/se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java b/se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java new file mode 100644 index 0000000..55eb8d7 --- /dev/null +++ b/se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java @@ -0,0 +1,64 @@ +package com.terra.common.configure; + +import com.terra.common.configure.properties.SpringDocProperties; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.oas.models.servers.Server; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +import java.util.ArrayList; +import java.util.List; + +/** + * Swagger 鏂囨。閰嶇疆 + * + * @author ruoyi + */ +@EnableConfigurationProperties(SpringDocProperties.class) +@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true) +public class SpringDocAutoConfiguration +{ + @Bean + @ConditionalOnMissingBean(OpenAPI.class) + public OpenAPI openApi(SpringDocProperties properties) + { + return new OpenAPI().components(new Components() + // 璁剧疆璁よ瘉鐨勮姹傚ご + .addSecuritySchemes("apikey", securityScheme())) + .addSecurityItem(new SecurityRequirement().addList("apikey")) + .info(convertInfo(properties.getInfo())) + .servers(servers(properties.getGatewayUrl())); + } + + public SecurityScheme securityScheme() + { + return new SecurityScheme().type(SecurityScheme.Type.APIKEY) + .name("Authorization") + .in(SecurityScheme.In.HEADER) + .scheme("Bearer"); + } + + private Info convertInfo(SpringDocProperties.InfoProperties infoProperties) + { + Info info = new Info(); + info.setTitle(infoProperties.getTitle()); + info.setDescription(infoProperties.getDescription()); + info.setContact(infoProperties.getContact()); + info.setLicense(infoProperties.getLicense()); + info.setVersion(infoProperties.getVersion()); + return info; + } + + public List<Server> servers(String gatewayUrl) + { + List<Server> serverList = new ArrayList<>(); + serverList.add(new Server().url(gatewayUrl)); + return serverList; + } +} diff --git a/se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java b/se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java new file mode 100644 index 0000000..a14dd88 --- /dev/null +++ b/se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java @@ -0,0 +1,135 @@ +package com.terra.common.configure.properties; + +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.License; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +/** + * Swagger 閰嶇疆灞炴�� + * + * @author ruoyi + */ +@ConfigurationProperties(prefix = "springdoc") +public class SpringDocProperties +{ + /** + * 缃戝叧 + */ + private String gatewayUrl; + + /** + * 鏂囨。鍩烘湰淇℃伅 + */ + @NestedConfigurationProperty + private InfoProperties info = new InfoProperties(); + + /** + * <p> + * 鏂囨。鐨勫熀纭�灞炴�т俊鎭� + * </p> + * + * @see io.swagger.v3.oas.models.info.Info + * + * 涓轰簡 springboot 鑷姩鐢熶骇閰嶇疆鎻愮ず淇℃伅锛屾墍浠ヨ繖閲屽鍒朵竴涓被鍑烘潵 + */ + public static class InfoProperties + { + /** + * 鏍囬 + */ + private String title = null; + + /** + * 鎻忚堪 + */ + private String description = null; + + /** + * 鑱旂郴浜轰俊鎭� + */ + @NestedConfigurationProperty + private Contact contact = null; + + /** + * 璁稿彲璇� + */ + @NestedConfigurationProperty + private License license = null; + + /** + * 鐗堟湰 + */ + private String version = null; + + public String getTitle() + { + return title; + } + + public void setTitle(String title) + { + this.title = title; + } + + public String getDescription() + { + return description; + } + + public void setDescription(String description) + { + this.description = description; + } + + public Contact getContact() + { + return contact; + } + + public void setContact(Contact contact) + { + this.contact = contact; + } + + public License getLicense() + { + return license; + } + + public void setLicense(License license) + { + this.license = license; + } + + public String getVersion() + { + return version; + } + + public void setVersion(String version) + { + this.version = version; + } + } + + public String getGatewayUrl() + { + return gatewayUrl; + } + + public void setGatewayUrl(String gatewayUrl) + { + this.gatewayUrl = gatewayUrl; + } + + public InfoProperties getInfo() + { + return info; + } + + public void setInfo(InfoProperties info) + { + this.info = info; + } +} diff --git a/se-common/src/main/java/com/terra/common/service/RedisService.java b/se-common/src/main/java/com/terra/common/service/RedisService.java new file mode 100644 index 0000000..2e17787 --- /dev/null +++ b/se-common/src/main/java/com/terra/common/service/RedisService.java @@ -0,0 +1,265 @@ +package com.terra.common.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.BoundSetOperations; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * spring redis 宸ュ叿绫� + * + * @author ruoyi + **/ +@SuppressWarnings(value = { "unchecked", "rawtypes" }) +@Component +public class RedisService +{ + @Autowired + public RedisTemplate redisTemplate; + + /** + * 缂撳瓨鍩烘湰鐨勫璞★紝Integer銆丼tring銆佸疄浣撶被绛� + * + * @param key 缂撳瓨鐨勯敭鍊� + * @param value 缂撳瓨鐨勫�� + */ + public <T> void setCacheObject(final String key, final T value) + { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缂撳瓨鍩烘湰鐨勫璞★紝Integer銆丼tring銆佸疄浣撶被绛� + * + * @param key 缂撳瓨鐨勯敭鍊� + * @param value 缂撳瓨鐨勫�� + * @param timeout 鏃堕棿 + * @param timeUnit 鏃堕棿棰楃矑搴� + */ + public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) + { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 璁剧疆鏈夋晥鏃堕棿 + * + * @param key Redis閿� + * @param timeout 瓒呮椂鏃堕棿 + * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触 + */ + public boolean expire(final String key, final long timeout) + { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 璁剧疆鏈夋晥鏃堕棿 + * + * @param key Redis閿� + * @param timeout 瓒呮椂鏃堕棿 + * @param unit 鏃堕棿鍗曚綅 + * @return true=璁剧疆鎴愬姛锛沠alse=璁剧疆澶辫触 + */ + public boolean expire(final String key, final long timeout, final TimeUnit unit) + { + return redisTemplate.expire(key, timeout, unit); + } + + /** + * 鑾峰彇鏈夋晥鏃堕棿 + * + * @param key Redis閿� + * @return 鏈夋晥鏃堕棿 + */ + public long getExpire(final String key) + { + return redisTemplate.getExpire(key); + } + + /** + * 鍒ゆ柇 key鏄惁瀛樺湪 + * + * @param key 閿� + * @return true 瀛樺湪 false涓嶅瓨鍦� + */ + public Boolean hasKey(String key) + { + return redisTemplate.hasKey(key); + } + + /** + * 鑾峰緱缂撳瓨鐨勫熀鏈璞°�� + * + * @param key 缂撳瓨閿�� + * @return 缂撳瓨閿�煎搴旂殑鏁版嵁 + */ + public <T> T getCacheObject(final String key) + { + ValueOperations<String, T> operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 鍒犻櫎鍗曚釜瀵硅薄 + * + * @param key + */ + public boolean deleteObject(final String key) + { + return redisTemplate.delete(key); + } + + /** + * 鍒犻櫎闆嗗悎瀵硅薄 + * + * @param collection 澶氫釜瀵硅薄 + * @return + */ + public boolean deleteObject(final Collection collection) + { + return redisTemplate.delete(collection) > 0; + } + + /** + * 缂撳瓨List鏁版嵁 + * + * @param key 缂撳瓨鐨勯敭鍊� + * @param dataList 寰呯紦瀛樼殑List鏁版嵁 + * @return 缂撳瓨鐨勫璞� + */ + public <T> long setCacheList(final String key, final List<T> dataList) + { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + + /** + * 鑾峰緱缂撳瓨鐨刲ist瀵硅薄 + * + * @param key 缂撳瓨鐨勯敭鍊� + * @return 缂撳瓨閿�煎搴旂殑鏁版嵁 + */ + public <T> List<T> getCacheList(final String key) + { + return redisTemplate.opsForList().range(key, 0, -1); + } + + /** + * 缂撳瓨Set + * + * @param key 缂撳瓨閿�� + * @param dataSet 缂撳瓨鐨勬暟鎹� + * @return 缂撳瓨鏁版嵁鐨勫璞� + */ + public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) + { + BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); + Iterator<T> it = dataSet.iterator(); + while (it.hasNext()) + { + setOperation.add(it.next()); + } + return setOperation; + } + + /** + * 鑾峰緱缂撳瓨鐨剆et + * + * @param key + * @return + */ + public <T> Set<T> getCacheSet(final String key) + { + return redisTemplate.opsForSet().members(key); + } + + /** + * 缂撳瓨Map + * + * @param key + * @param dataMap + */ + public <T> void setCacheMap(final String key, final Map<String, T> dataMap) + { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 鑾峰緱缂撳瓨鐨凪ap + * + * @param key + * @return + */ + public <T> Map<String, T> getCacheMap(final String key) + { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 寰�Hash涓瓨鍏ユ暟鎹� + * + * @param key Redis閿� + * @param hKey Hash閿� + * @param value 鍊� + */ + public <T> void setCacheMapValue(final String key, final String hKey, final T value) + { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 鑾峰彇Hash涓殑鏁版嵁 + * + * @param key Redis閿� + * @param hKey Hash閿� + * @return Hash涓殑瀵硅薄 + */ + public <T> T getCacheMapValue(final String key, final String hKey) + { + HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + /** + * 鑾峰彇澶氫釜Hash涓殑鏁版嵁 + * + * @param key Redis閿� + * @param hKeys Hash閿泦鍚� + * @return Hash瀵硅薄闆嗗悎 + */ + public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) + { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 鍒犻櫎Hash涓殑鏌愭潯鏁版嵁 + * + * @param key Redis閿� + * @param hKey Hash閿� + * @return 鏄惁鎴愬姛 + */ + public boolean deleteCacheMapValue(final String key, final String hKey) + { + return redisTemplate.opsForHash().delete(key, hKey) > 0; + } + + /** + * 鑾峰緱缂撳瓨鐨勫熀鏈璞″垪琛� + * + * @param pattern 瀛楃涓插墠缂� + * @return 瀵硅薄鍒楄〃 + */ + public Collection<String> keys(final String pattern) + { + return redisTemplate.keys(pattern); + } +} diff --git a/se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index d374be8..a9cbde5 100644 --- a/se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,4 @@ com.terra.common.utils.SpringUtils +com.terra.common.service.RedisService +com.terra.common.configure.RedisConfig +com.ruoyi.common.configure.SpringDocAutoConfiguration \ No newline at end of file -- Gitblit v1.9.3