唐tag 1 年之前
父節點
當前提交
7c92c9a3bf

+ 7 - 4
pom.xml

@@ -7,6 +7,9 @@
     <version>0.0.1-SNAPSHOT</version>
     <name>DataServiceStarter</name>
     <description>DataServiceStarter</description>
+
+    <packaging>jar</packaging>
+
     <properties>
         <java.version>11</java.version>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -501,10 +504,10 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>${spring-boot.version}</version>
-                <configuration>
-                    <mainClass>com.zswl.dataservicestarter.DataServiceStarterApplication</mainClass>
-                    <skip>true</skip>
-                </configuration>
+                <!--                <configuration>-->
+                <!--                    <mainClass>com.zswl.dataservicestarter.DataServiceStarterApplication</mainClass>-->
+                <!--                    <skip>true</skip>-->
+                <!--                </configuration>-->
                 <executions>
                     <execution>
                         <id>repackage</id>

+ 4 - 5
src/main/java/com/zswl/dataservicestarter/DataServiceStarterApplication.java

@@ -9,7 +9,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
-//@SpringBootApplication
+// @SpringBootApplication
 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
 @EnableAsync
 @EnableCaching
@@ -17,8 +17,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 @EnableWebMvc
 public class DataServiceStarterApplication {
 
-    public static void main(String[] args) {
-        SpringApplication.run(DataServiceStarterApplication.class, args);
-    }
-
+  public static void main(String[] args) {
+    SpringApplication.run(DataServiceStarterApplication.class, args);
+  }
 }

+ 5 - 12
src/main/java/com/zswl/dataservicestarter/config/JpaDataConfiguration.java

@@ -7,22 +7,15 @@ import org.springframework.context.annotation.Import;
 import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
-/**
- * JPA 配置
- */
-
+/** JPA 配置 */
 @Configuration
-//允许自动装载数据源
+// 允许自动装载数据源
 @Import({DataSourceAutoConfiguration.class, PaginationConfiguration.class})
 
-//允许使用jpa注解
+// 允许使用jpa注解
 @EnableJpaAuditing
 
-//允许事务管理
+// 允许事务管理
 @EnableTransactionManagement
-
 @ComponentScan("com.zswl.dataservicestarter.helper.jpa")
-public class JpaDataConfiguration {
-
-
-}
+public class JpaDataConfiguration {}

+ 74 - 61
src/main/java/com/zswl/dataservicestarter/config/MongoConfiguration.java

@@ -17,16 +17,16 @@ import org.springframework.data.mongodb.config.EnableMongoAuditing;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
 import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
+import org.springframework.data.mongodb.core.convert.MongoConverter;
 import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
 import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
+import org.springframework.data.mongodb.gridfs.GridFsTemplate;
 import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
 
 import java.util.ArrayList;
 import java.util.List;
 
-/**
- * 集成自动配置
- */
+/** 集成自动配置 */
 @Configuration
 @EnableMongoAuditing
 @ComponentScan({"com.zswl.dataservicestarter", "com.zswl.dataservicestarter.helper"})
@@ -34,69 +34,82 @@ import java.util.List;
 @Import(PaginationConfiguration.class)
 public class MongoConfiguration {
 
+  static {
+    // jdk 11 以上,强制使用 TLSv1.3 ,  mongo 驱动目前兼容 TLSv1.2
+    System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
+  }
 
-    static {
-        //jdk 11 以上,强制使用 TLSv1.3 ,  mongo 驱动目前兼容 TLSv1.2
-        System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
-    }
+  /**
+   * 自定义转换器
+   *
+   * @param mongoDbFactory
+   * @param mongoMappingContext
+   * @return
+   * @throws Exception
+   */
+  @Bean
+  @ConditionalOnMissingBean
+  public MappingMongoConverter mappingMongoConverter(
+      MongoDatabaseFactory mongoDbFactory, MongoMappingContext mongoMappingContext)
+      throws Exception {
+    DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
+    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
+    List<Object> list = new ArrayList<>();
+    list.add(new BigDecimalToDecimal128Converter()); // 自定义的类型转换器
+    list.add(new Decimal128ToBigDecimalConverter()); // 自定义的类型转换器
+    converter.setCustomConversions(new MongoCustomConversions(list));
 
-    /**
-     * 自定义转换器
-     *
-     * @param mongoDbFactory
-     * @param mongoMappingContext
-     * @return
-     * @throws Exception
-     */
-    @Bean
-    @ConditionalOnMissingBean
-    public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory mongoDbFactory, MongoMappingContext mongoMappingContext) throws Exception {
-        DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
-        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
-        List<Object> list = new ArrayList<>();
-        list.add(new BigDecimalToDecimal128Converter());//自定义的类型转换器
-        list.add(new Decimal128ToBigDecimalConverter());//自定义的类型转换器
-        converter.setCustomConversions(new MongoCustomConversions(list));
+    return converter;
+  }
 
-        return converter;
-    }
+  /**
+   * 事务
+   *
+   * @param dbFactory
+   * @return
+   */
+  @Bean
+  MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
+    return new MongoTransactionManager(dbFactory);
+  }
 
-    /**
-     * 事务
-     *
-     * @param dbFactory
-     * @return
-     */
-    @Bean
-    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
-        return new MongoTransactionManager(dbFactory);
-    }
+  /**
+   * 配置读写分离
+   *
+   * @return
+   */
+  @Bean
+  public ReadPreference readPreference(MongoTemplate mongoTemplate) {
+    //        首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。
+    ReadPreference readPreference = ReadPreference.primaryPreferred();
+    mongoTemplate.setReadPreference(readPreference);
+    return readPreference;
+  }
 
-    /**
-     * 配置读写分离
-     *
-     * @return
-     */
-    @Bean
-    public ReadPreference readPreference(MongoTemplate mongoTemplate) {
-//        首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。
-        ReadPreference readPreference = ReadPreference.primaryPreferred();
-        mongoTemplate.setReadPreference(readPreference);
-        return readPreference;
-    }
+  @Bean
+  public DocumentCodec documentCodec(MongoDatabaseFactory dbFactory) {
+    return new DocumentCodec(dbFactory.getCodecRegistry());
+  }
 
-    @Bean
-    public DocumentCodec documentCodec(MongoDatabaseFactory dbFactory) {
-        return new DocumentCodec(dbFactory.getCodecRegistry());
-    }
+  /**
+   * GridFs
+   *
+   * @param mongoDbFactory
+   * @param mongoConverter
+   * @return
+   */
+  @Bean
+  public GridFsTemplate gridFsTemplate(
+      MongoDatabaseFactory mongoDbFactory, MongoConverter mongoConverter) {
+    return new GridFsTemplate(mongoDbFactory, mongoConverter);
+  }
 
-
-// 	 @EventListener(ApplicationReadyEvent.class)
-//	 public void initIndicesAfterStartup() {
-//
-//	    IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
-//
-//	     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
-//	     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
-//	 }
+  // 	 @EventListener(ApplicationReadyEvent.class)
+  //	 public void initIndicesAfterStartup() {
+  //
+  //	    IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
+  //
+  //	     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
+  //	     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
+  //	 }
 }

+ 24 - 13
src/main/java/com/zswl/dataservicestarter/controller/TestController.java

@@ -2,33 +2,44 @@ package com.zswl.dataservicestarter.controller;
 
 import com.zswl.dataservicestarter.service.UserService;
 import com.zswl.dataservicestarter.utils.result.ResultContent;
+import lombok.Cleanup;
+import lombok.SneakyThrows;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 
-/**
- *
- */
+/** */
 @RequestMapping("/test")
 @RestController
 @Validated
 public class TestController {
 
-    @Autowired
-    UserService userService;
+  @Autowired UserService userService;
 
-    @RequestMapping(value = "test", method = {RequestMethod.GET, RequestMethod.POST})
-    public ResultContent test(String name) {
-        Map map = new HashMap();
-        map.put("obj", name);
-        map.put("time", System.currentTimeMillis());
+  @RequestMapping(
+      value = "test",
+      method = {RequestMethod.GET, RequestMethod.POST})
+  public ResultContent test(String name) {
+    Map map = new HashMap();
+    map.put("obj", name);
+    map.put("time", System.currentTimeMillis());
 
-        return userService.addUser(name);
-    }
-}
+    return userService.addUser(name);
+  }
 
+  @RequestMapping(value = "uploadFileDelegate", method = RequestMethod.POST)
+  @SneakyThrows
+  public ResultContent uploadFileDelegate(
+      @RequestParam(value = "file") MultipartFile multipartFile, String path, String epId) {
+    @Cleanup InputStream inputStream = multipartFile.getInputStream();
+    return ResultContent.buildSuccess();
+  }
+}

+ 19 - 18
src/main/java/com/zswl/dataservicestarter/service/UserService.java

@@ -7,6 +7,7 @@ import com.zswl.dataservicestarter.utils.result.ResultContent;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.gridfs.GridFsTemplate;
 import org.springframework.stereotype.Service;
 
 /**
@@ -16,25 +17,25 @@ import org.springframework.stereotype.Service;
 @Slf4j
 @Service
 public class UserService {
-    @Autowired
-    UserDao userDao;
 
-    @Autowired
-    RedisService redisService;
+  @Autowired UserDao userDao;
 
-    public ResultContent addUser(String name) {
-        User user = new User();
-        if (StringUtils.isEmpty(name)) {
-            name = "名称";
-        }
-//        name = name + Math.random();
-        user.setName(name);
-        user.setAge(1);
-        user.setAddress("重庆市渝北区");
-        userDao.save(user);
-        int code = user.hashCode();
-        log.info("名称: {} code {} redis: {}", name, code, AppInfoUtil.generateRandomString());
-        return ResultContent.buildSuccess(name + ": " + code);
-    }
+  @Autowired RedisService redisService;
+
+  @Autowired GridFsTemplate gridFsTemplate;
 
+  public ResultContent addUser(String name) {
+    User user = new User();
+    if (StringUtils.isEmpty(name)) {
+      name = "名称";
+    }
+    //        name = name + Math.random();
+    user.setName(name);
+    user.setAge(1);
+    user.setAddress("重庆市渝北区");
+    userDao.save(user);
+    int code = user.hashCode();
+    log.info("名称: {} code {} redis: {}", name, code, AppInfoUtil.generateRandomString());
+    return ResultContent.buildSuccess(name + ": " + code);
+  }
 }