igl 1 anno fa
parent
commit
adc7b0c4c8

+ 9 - 0
framework-common/pom.xml

@@ -95,5 +95,14 @@
             <groupId>com.googlecode.libphonenumber</groupId>
             <artifactId>geocoder</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-core</artifactId>
+            <version>3.5.3.1</version>
+        </dependency>
     </dependencies>
 </project>

+ 27 - 0
framework-common/src/main/java/com/chelvc/framework/common/model/PagedDTO.java

@@ -0,0 +1,27 @@
+package com.chelvc.framework.common.model;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * 分页传输模型
+ *
+ * @author igl
+ * @date 2023/05/26 10:46
+ */
+@Data
+public class PagedDTO implements Serializable {
+
+  private static final long serialVersionUID = -6094379285329869450L;
+
+  /**
+   * 页码
+   */
+  private Long pageCode;
+
+  /**
+   * 页大小
+   */
+  private Long pageSize;
+}

+ 55 - 0
framework-common/src/main/java/com/chelvc/framework/common/model/PagedVO.java

@@ -0,0 +1,55 @@
+package com.chelvc.framework.common.model;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.chelvc.framework.common.util.BeanCopyUtil;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 分页视图
+ *
+ * @author 武尊
+ * @date 2023/04/16 23:09
+ */
+@Data
+public class PagedVO<T> implements Serializable {
+
+  private static final long serialVersionUID = 3889341994334964174L;
+
+  /**
+   * 总数
+   */
+  private Long totalNumber;
+
+  /**
+   * 页码
+   */
+  private Long pageNumber;
+
+  /**
+   * 页面大小
+   */
+  private Long pageSize;
+
+  /**
+   * 数据记录
+   */
+  private List<T> records;
+
+  public static <T, S> PagedVO<S> build(IPage<T> dataSource, Class<S> targetClazz) {
+    PagedVO<S> result = new PagedVO<>();
+    result.setTotalNumber(dataSource.getTotal());
+    result.setPageNumber(dataSource.getCurrent());
+    result.setPageSize(dataSource.getSize());
+    List<S> records = new ArrayList<>();
+    for (T t : dataSource.getRecords()) {
+      S s = BeanCopyUtil.copy(t, targetClazz);
+      records.add(s);
+    }
+    result.setRecords(records);
+    return result;
+  }
+}

+ 93 - 0
framework-common/src/main/java/com/chelvc/framework/common/util/BeanCopyUtil.java

@@ -0,0 +1,93 @@
+package com.chelvc.framework.common.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import org.springframework.cglib.beans.BeanCopier;
+
+/**
+ * Bean对象拷贝工具
+ *
+ * @author igl
+ * @date 2023/12/13 23:48
+ */
+public class BeanCopyUtil {
+
+  private final static Map<String, BeanCopier> BEAN_COPIER_WAREHOUSE = new ConcurrentHashMap<>();
+
+  /**
+   * Bean对象拷贝
+   *
+   * @param dataSource  数据源
+   * @param targetClazz 目标数据类型
+   * @return {@link S}
+   * @author igl
+   * @date 2023/12/13 23:48
+   */
+  public static <T, S> S copy(T dataSource, Class<S> targetClazz) {
+    BeanCopier beanCopier = getBeanCopier(dataSource.getClass(), targetClazz);
+    S result;
+    try {
+      result = targetClazz.newInstance();
+      beanCopier.copy(dataSource, result, null);
+    } catch (InstantiationException | IllegalAccessException e) {
+      throw new RuntimeException("数据转换异常");
+    }
+    return result;
+  }
+
+  /**
+   * Bean对象拷贝
+   *
+   * @param dataSource  数据源
+   * @param result      结果
+   * @param targetClazz 目标类型
+   * @return {@link S}
+   * @author igl
+   * @date 2023/12/13 23:48
+   */
+  public static <T, S> S copy(T dataSource, S result, Class<S> targetClazz) {
+    BeanCopier beanCopier = getBeanCopier(dataSource.getClass(), targetClazz);
+    beanCopier.copy(dataSource, result, null);
+    return result;
+  }
+
+  /**
+   * List对象拷贝
+   *
+   * @param source
+   * @param targetClazz
+   * @return {@link List<S>}
+   * @author igl
+   * @date 2023/12/13 23:48
+   */
+  public static <T, S> List<S> copy(Collection<T> source, Class<S> targetClazz) {
+    if (Optional.ofNullable(source).orElse(Collections.emptyList()).isEmpty()) {
+      return new ArrayList<>();
+    }
+    List<S> result = new ArrayList<>();
+    for (T t : source) {
+      S s = copy(t, targetClazz);
+      result.add(s);
+    }
+    return result;
+  }
+
+  public static <T, S> BeanCopier getBeanCopier(Class<T> sourceClazz, Class<S> targetClazz) {
+    String key = getKey(sourceClazz, targetClazz);
+    if (BEAN_COPIER_WAREHOUSE.containsKey(key)) {
+      return BEAN_COPIER_WAREHOUSE.get(key);
+    }
+    BeanCopier beanCopier = BeanCopier.create(sourceClazz, targetClazz, false);
+    BEAN_COPIER_WAREHOUSE.put(key, beanCopier);
+    return beanCopier;
+  }
+
+  private static <T, S> String getKey(Class<T> sourceClazz, Class<S> targetClazz) {
+    return sourceClazz.getSimpleName() + "To" + targetClazz.getSimpleName();
+  }
+}

+ 9 - 0
framework-redis/pom.xml

@@ -32,5 +32,14 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>lock4j-redisson-spring-boot-starter</artifactId>
+            <version>2.2.4</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 58 - 0
framework-redis/src/main/java/com/chelvc/framework/redis/config/RedissonConfig.java

@@ -0,0 +1,58 @@
+package com.chelvc.framework.redis.config;
+
+import lombok.RequiredArgsConstructor;
+import org.redisson.Redisson;
+import org.redisson.api.RedissonClient;
+import org.redisson.config.Config;
+import org.redisson.spring.data.connection.RedissonConnectionFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
+
+/**
+ * Redisson配置
+ *
+ * @author igl
+ * @date 2023/05/28 18:06
+ */
+@Configurable
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
+public class RedissonConfig {
+
+  private static final String REDIS_CONNECTION_TEMPLATE = "redis://%s:%d";
+
+  @Value("${spring.redis.host:127.0.0.1}")
+  private String host;
+
+  @Value("${spring.redis.port:60379}")
+  private Integer port;
+
+  @Value("${spring.redis.password:igl611}")
+  private String password;
+
+  @Value("${spring.redis.database:0}")
+  private Integer database;
+
+  @Bean
+  @Primary
+  public RedissonConnectionFactory redisConnectionFactory() {
+    return new RedissonConnectionFactory(createConfig());
+  }
+
+  @Bean
+  public RedissonClient redissonClient() {
+    return Redisson.create(createConfig());
+  }
+
+  private Config createConfig() {
+    Config config = new Config();
+    String address = String.format(REDIS_CONNECTION_TEMPLATE, host, port);
+    config.useSingleServer()
+        .setAddress(address)
+        .setPassword(password)
+        .setDatabase(database);
+    return config;
+  }
+}

+ 26 - 0
framework-redis/src/main/java/com/chelvc/framework/redis/handler/LockExceptionHandler.java

@@ -0,0 +1,26 @@
+package com.chelvc.framework.redis.handler;
+
+import com.baomidou.lock.exception.LockFailureException;
+import com.chelvc.framework.base.exception.ResourceUnavailableException;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 锁异常处理
+ *
+ * @author 武尊
+ * @date 2023/05/28 20:36
+ */
+@Slf4j
+@Configurable
+@RestControllerAdvice
+public class LockExceptionHandler {
+
+  @ExceptionHandler(LockFailureException.class)
+  public void exceptionHandler(LockFailureException e) {
+    log.warn("锁异常:{}", e.getMessage(), e);
+    throw new ResourceUnavailableException("服务器繁忙,请稍后再试");
+  }
+}