From b7f0283f5b7506013e890a55f3adf849457f7e9d Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 07 十月 2023 09:43:28 +0800
Subject: [PATCH] 支持跨域访问

---
 src/main/java/com/smartearth/poiexcel/controller/EntController.java |   19 +++++
 src/main/java/com/smartearth/poiexcel/config/WebConfig.java         |   92 ++++++++++++++++++++++++++++++
 src/main/java/com/smartearth/poiexcel/mapper/QiYeMapper.java        |    8 ++
 src/main/resources/mapper/QiYeMapper.xml                            |   12 ++++
 4 files changed, 129 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/smartearth/poiexcel/config/WebConfig.java b/src/main/java/com/smartearth/poiexcel/config/WebConfig.java
new file mode 100644
index 0000000..24b4294
--- /dev/null
+++ b/src/main/java/com/smartearth/poiexcel/config/WebConfig.java
@@ -0,0 +1,92 @@
+package com.smartearth.poiexcel.config;
+
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.alibaba.fastjson.support.config.FastJsonConfig;
+import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.Ordered;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.util.AntPathMatcher;
+import org.springframework.web.servlet.config.annotation.*;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+/**
+ * Web閰嶇疆绫�
+ * @author WWW
+ */
+@Configuration
+public class WebConfig extends WebMvcConfigurationSupport {
+    @Override
+    public void addViewControllers(ViewControllerRegistry registry) {
+        // 璁剧疆璁块棶璺緞涓� 鈥�/鈥� 璺宠浆鍒版寚瀹氶〉闈�
+        registry.addViewController("/").setViewName("redirect:/swagger-ui.html");
+        // 璁剧疆涓烘渶楂樹紭鍏堢骇
+        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
+    }
+
+    /**
+     * swagger鎺у埗
+     */
+    @Override
+    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
+        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
+        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
+        super.addResourceHandlers(registry);
+    }
+
+    /**
+     * 蹇界暐url鍦板潃澶у皬鍐�
+     */
+    @Override
+    protected void configurePathMatch(PathMatchConfigurer configurer) {
+        AntPathMatcher matcher = new AntPathMatcher();
+        matcher.setCaseSensitive(false);
+
+        configurer.setPathMatcher(matcher);
+    }
+
+    /**
+     * 璺ㄥ煙璇锋眰
+     */
+    @Override
+    protected void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+                .allowCredentials(true)
+                .allowedOrigins("*")
+                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
+                .allowedHeaders("*")
+                .maxAge(3600);
+    }
+
+    /**
+     * 娣诲姞缁熶竴鐨勬嫤鎴櫒
+     */
+    @Override
+    protected void addInterceptors(InterceptorRegistry registry) {
+        super.addInterceptors(registry);
+    }
+
+    /**
+     * 澶勭悊json鏍煎紡锛屽�糿ull鐨勮浆鎹负""
+     */
+    @Override
+    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
+        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
+        FastJsonConfig config = new FastJsonConfig();
+        config.setSerializerFeatures(
+                SerializerFeature.WriteNullListAsEmpty,
+                SerializerFeature.WriteMapNullValue,
+                //SerializerFeature.WriteNullStringAsEmpty,
+                SerializerFeature.WriteNullNumberAsZero,
+                SerializerFeature.WriteNullBooleanAsFalse);
+        // 缁撴灉鏄惁鏍煎紡鍖�,榛樿涓篺alse
+        //SerializerFeature.PrettyFormat);
+
+        converter.setFastJsonConfig(config);
+        converter.setDefaultCharset(StandardCharsets.UTF_8);
+        converters.add(converter);
+    }
+}
diff --git a/src/main/java/com/smartearth/poiexcel/controller/EntController.java b/src/main/java/com/smartearth/poiexcel/controller/EntController.java
index 6cc888a..cd1bd57 100644
--- a/src/main/java/com/smartearth/poiexcel/controller/EntController.java
+++ b/src/main/java/com/smartearth/poiexcel/controller/EntController.java
@@ -47,6 +47,21 @@
     @Value("${address.code.url}")
     private String addressCodeUrl;
 
+    @ApiOperation(value = "鏍规嵁鍚嶇О妯$硦鏌ヨ浼佷笟")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "name", value = "浼佷笟鍚嶇О鎴栧湴鍧�", dataType = "String", paramType = "query", example = "")
+    })
+    @GetMapping({"/selectByName"})
+    public ResponseMsg<Object> selectByName(String name) {
+        try {
+            List<EntEntity> list = qiYeMapper.selectByName(StringHelper.getLikeStr(name));
+
+            return success(null == list ? 0 : list.size(), list);
+        } catch (Exception ex) {
+            return fail(ex, -1);
+        }
+    }
+
     @ApiOperation(value = "鏌ヨ浠ょ墝")
     @GetMapping({"/selectToken"})
     public ResponseMsg<Object> selectToken() {
@@ -59,7 +74,7 @@
         }
     }
 
-    @ApiOperation(value = "鏌ヨ浼佷笟")
+    @ApiOperation(value = "鏌ヨ浼佷笟淇℃伅")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "token", value = "浠ょ墝", dataType = "String", paramType = "query", example = ""),
             @ApiImplicitParam(name = "startDate", value = "寮�濮嬫棩鏈�", dataType = "String", paramType = "query", example = "2023-06-29"),
@@ -130,7 +145,7 @@
         }
     }
 
-    @ApiOperation(value = "鍒嗛〉鏌ヨ")
+    @ApiOperation(value = "鍒嗛〉鏌ヨ浼佷笟")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "pageSize", value = "姣忛〉鏉℃暟", dataType = "Integer", paramType = "query", example = "10"),
             @ApiImplicitParam(name = "pageIndex", value = "鍒嗛〉鏁帮紙浠�1寮�濮嬶級", dataType = "Integer", paramType = "query", example = "1")
diff --git a/src/main/java/com/smartearth/poiexcel/mapper/QiYeMapper.java b/src/main/java/com/smartearth/poiexcel/mapper/QiYeMapper.java
index 6b6e141..c22b1c1 100644
--- a/src/main/java/com/smartearth/poiexcel/mapper/QiYeMapper.java
+++ b/src/main/java/com/smartearth/poiexcel/mapper/QiYeMapper.java
@@ -15,6 +15,14 @@
 @Repository
 public interface QiYeMapper {
     /**
+     * 鏍规嵁鍚嶇О妯$硦鏌ヨ
+     *
+     * @param name
+     * @return
+     */
+    public List<EntEntity> selectByName(@Param("name") String name);
+
+    /**
      * 鏌ヨ琛屾暟
      *
      * @return
diff --git a/src/main/resources/mapper/QiYeMapper.xml b/src/main/resources/mapper/QiYeMapper.xml
index 8fbc98c..b7c5388 100644
--- a/src/main/resources/mapper/QiYeMapper.xml
+++ b/src/main/resources/mapper/QiYeMapper.xml
@@ -1,6 +1,18 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.smartearth.poiexcel.mapper.QiYeMapper">
+    <select id="selectByName" resultType="com.smartearth.poiexcel.entity.EntEntity">
+        select *
+        from smart_earth.yz_qiyexinxi
+        <where>
+            <if test="name != null">
+                upper(entName) like #{name} or upper(address) like #{name}
+            </if>
+        </where>
+        order by entName
+        limit 10;
+    </select>
+
     <select id="selectCount" resultType="java.lang.Integer">
         select count(*)
         from smart_earth.yz_qiyexinxi

--
Gitblit v1.9.3