中微多查询
							parent
							
								
									7991fe68ab
								
							
						
					
					
						commit
						4f618d60d1
					
				
							
								
								
									
										16
									
								
								wms/pom.xml
								
								
								
								
							
							
						
						
									
										16
									
								
								wms/pom.xml
								
								
								
								
							| 
						 | 
					@ -70,6 +70,22 @@
 | 
				
			||||||
            <version>3.5.1</version>
 | 
					            <version>3.5.1</version>
 | 
				
			||||||
        </dependency>
 | 
					        </dependency>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.microsoft.sqlserver</groupId>
 | 
				
			||||||
 | 
					            <artifactId>mssql-jdbc</artifactId>
 | 
				
			||||||
 | 
					            <version>9.4.0.jre8</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>cn.hutool</groupId>
 | 
				
			||||||
 | 
					            <artifactId>hutool-all</artifactId>
 | 
				
			||||||
 | 
					            <version>5.8.21</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>jakarta.persistence</groupId>
 | 
				
			||||||
 | 
					            <artifactId>jakarta.persistence-api</artifactId>
 | 
				
			||||||
 | 
					            <version>2.2.3</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    </dependencies>
 | 
					    </dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <build>
 | 
					    <build>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,9 @@
 | 
				
			||||||
package com.yc.wms;
 | 
					package com.yc.wms;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.yc.wms.until.SpringContextHolder;
 | 
				
			||||||
import org.springframework.boot.SpringApplication;
 | 
					import org.springframework.boot.SpringApplication;
 | 
				
			||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
					import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@SpringBootApplication
 | 
					@SpringBootApplication
 | 
				
			||||||
public class WmsApplication {
 | 
					public class WmsApplication {
 | 
				
			||||||
| 
						 | 
					@ -9,5 +11,10 @@ public class WmsApplication {
 | 
				
			||||||
    public static void main(String[] args) {
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
        SpringApplication.run(WmsApplication.class, args);
 | 
					        SpringApplication.run(WmsApplication.class, args);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SpringContextHolder springContextHolder() {
 | 
				
			||||||
 | 
					        return new SpringContextHolder();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,12 +5,20 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
				
			||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
					import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
				
			||||||
import com.yc.wms.model.Menu;
 | 
					import com.yc.wms.model.Menu;
 | 
				
			||||||
import com.yc.wms.model.User;
 | 
					import com.yc.wms.model.User;
 | 
				
			||||||
 | 
					import com.yc.wms.service.DcxService;
 | 
				
			||||||
import com.yc.wms.service.MenuService;
 | 
					import com.yc.wms.service.MenuService;
 | 
				
			||||||
import com.yc.wms.service.UserService;
 | 
					import com.yc.wms.service.UserService;
 | 
				
			||||||
import com.yc.wms.until.QueryPageUtil;
 | 
					import com.yc.wms.until.QueryPageUtil;
 | 
				
			||||||
import com.yc.wms.until.Result;
 | 
					import com.yc.wms.until.Result;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.http.HttpStatus;
 | 
				
			||||||
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
 | 
					import springfox.documentation.spring.web.json.Json;
 | 
				
			||||||
 | 
					import cn.hutool.json.JSONArray;
 | 
				
			||||||
 | 
					import cn.hutool.json.JSONObject;
 | 
				
			||||||
 | 
					import cn.hutool.json.JSONUtil;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Arrays;
 | 
					import java.util.Arrays;
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
| 
						 | 
					@ -25,6 +33,7 @@ import java.util.List;
 | 
				
			||||||
 * @since 2023-03-04
 | 
					 * @since 2023-03-04
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
@RequestMapping("/user")
 | 
					@RequestMapping("/user")
 | 
				
			||||||
public class UserController {
 | 
					public class UserController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -34,26 +43,65 @@ public class UserController {
 | 
				
			||||||
    @Autowired
 | 
					    @Autowired
 | 
				
			||||||
    private MenuService menuService;
 | 
					    private MenuService menuService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private DcxService dcxService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @return
 | 
					     * @return
 | 
				
			||||||
     * 登录
 | 
					     * 登录
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @PostMapping ("/login")
 | 
					    @PostMapping ("/login")
 | 
				
			||||||
    public Result login(@RequestBody User user) {
 | 
					    public Result login(@RequestBody User user) {
 | 
				
			||||||
        Result result = null;
 | 
					//        Result result = null;
 | 
				
			||||||
       List<User> list= userService.lambdaQuery()
 | 
					//       List<User> list= userService.lambdaQuery()
 | 
				
			||||||
                  .eq(User::getUserNo,user.getUserNo())
 | 
					//                  .eq(User::getUserNo,user.getUserNo())
 | 
				
			||||||
                  .eq(User::getPassWord,user.getPassWord()).list();
 | 
					//                  .eq(User::getPassWord,user.getPassWord()).list();
 | 
				
			||||||
       if(list.size()>0){
 | 
					//       if(list.size()>0){
 | 
				
			||||||
           List menulist=menuService.lambdaQuery().like(Menu::getMenuRight,list.get(0).getRoleId().toString()).list();
 | 
					//           List menulist=menuService.lambdaQuery().like(Menu::getMenuRight,list.get(0).getRoleId().toString()).list();
 | 
				
			||||||
           HashMap map =new HashMap();
 | 
					//           HashMap map =new HashMap();
 | 
				
			||||||
           map.put("user",list.get(0));
 | 
					//           map.put("user",list.get(0));
 | 
				
			||||||
           map.put("menu",menulist);
 | 
					//           map.put("menu",menulist);
 | 
				
			||||||
           result=Result.success(map);
 | 
					//           result=Result.success(map);
 | 
				
			||||||
       }else{
 | 
					//       }else{
 | 
				
			||||||
           result=Result.fail("账号或密码输入有误!");
 | 
					//           result=Result.fail("账号或密码输入有误!");
 | 
				
			||||||
       }
 | 
					//       }
 | 
				
			||||||
        return result;
 | 
					//        return result;
 | 
				
			||||||
 | 
					        JSONArray array=new JSONArray();
 | 
				
			||||||
 | 
					        JSONObject data=new JSONObject();
 | 
				
			||||||
 | 
					        int menuCount=3;
 | 
				
			||||||
 | 
					        for (int i=1;i<=menuCount;i++){
 | 
				
			||||||
 | 
					            JSONObject menu=new JSONObject();
 | 
				
			||||||
 | 
					            menu.put("id",i);
 | 
				
			||||||
 | 
					            menu.put("menuCode",i);
 | 
				
			||||||
 | 
					            if(i==1) {
 | 
				
			||||||
 | 
					                menu.put("menuName", "库存汇总查询");
 | 
				
			||||||
 | 
					            }else if(i==2) {
 | 
				
			||||||
 | 
					                menu.put("menuName", "入库明细查询");
 | 
				
			||||||
 | 
					            }else if(i==3) {
 | 
				
			||||||
 | 
					                menu.put("menuName", "出库明细查询");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            menu.put("menuLevel",i);
 | 
				
			||||||
 | 
					            menu.put("menuParentCode",i);
 | 
				
			||||||
 | 
					            menu.put("menuClick","admin"+i);
 | 
				
			||||||
 | 
					            menu.put("menuRight", "0,1");
 | 
				
			||||||
 | 
					            menu.put("menuComponent","/admin"+i);
 | 
				
			||||||
 | 
					            menu.put("menuIcon","el-icon-s-home");
 | 
				
			||||||
 | 
					            array.add(menu);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        data.put("menu",array);
 | 
				
			||||||
 | 
					        JSONObject u=new JSONObject();
 | 
				
			||||||
 | 
					        u.put("id", 2);
 | 
				
			||||||
 | 
					        u.put("userNo", "1");
 | 
				
			||||||
 | 
					        u.put("userName", "1");
 | 
				
			||||||
 | 
					        u.put("passWord", "123456");
 | 
				
			||||||
 | 
					        u.put("age", 1);
 | 
				
			||||||
 | 
					        u.put("sex", 1);
 | 
				
			||||||
 | 
					        u.put("phone", "1");
 | 
				
			||||||
 | 
					        u.put("roleId", 1);
 | 
				
			||||||
 | 
					        u.put("isValid", false);
 | 
				
			||||||
 | 
					        data.put("user",u);
 | 
				
			||||||
 | 
					        log.info("lll:"+new ResponseEntity(data, HttpStatus.OK));
 | 
				
			||||||
 | 
					        return  Result.success(menuCount,data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
| 
						 | 
					@ -62,26 +110,27 @@ public class UserController {
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @PostMapping("/queryUser")
 | 
					    @PostMapping("/queryUser")
 | 
				
			||||||
    public Result queryUser(@RequestBody QueryPageUtil query) {
 | 
					    public Result queryUser(@RequestBody QueryPageUtil query) {
 | 
				
			||||||
        HashMap map = query.getParam();
 | 
					//        HashMap map = query.getParam();
 | 
				
			||||||
        String name = (String) map.get("name");
 | 
					//        String name = (String) map.get("name");
 | 
				
			||||||
        String sex = (String) map.get("xb");
 | 
					//        String sex = (String) map.get("xb");
 | 
				
			||||||
        Page<User> page = new Page<User>();
 | 
					//        Page<User> page = new Page<User>();
 | 
				
			||||||
        page.setCurrent(query.getPageNum());
 | 
					//        page.setCurrent(query.getPageNum());
 | 
				
			||||||
        page.setSize(query.getPageSize());
 | 
					//        page.setSize(query.getPageSize());
 | 
				
			||||||
        System.out.println("当前页:" + query.getPageNum());
 | 
					//        System.out.println("当前页:" + query.getPageNum());
 | 
				
			||||||
        System.out.println("条数:" + query.getPageSize());
 | 
					//        System.out.println("条数:" + query.getPageSize());
 | 
				
			||||||
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
 | 
					//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
 | 
				
			||||||
        if (name != null && name.length() > 0) {
 | 
					//        if (name != null && name.length() > 0) {
 | 
				
			||||||
            lambdaQueryWrapper.like(User::getUserName, name);
 | 
					//            lambdaQueryWrapper.like(User::getUserName, name);
 | 
				
			||||||
        }
 | 
					//        }
 | 
				
			||||||
        if (sex != null && sex.length() > 0) {
 | 
					//        if (sex != null && sex.length() > 0) {
 | 
				
			||||||
            lambdaQueryWrapper.like(User::getSex, sex);
 | 
					//            lambdaQueryWrapper.like(User::getSex, sex);
 | 
				
			||||||
        }
 | 
					//        }
 | 
				
			||||||
        IPage iPage = userService.QueryUser(page, lambdaQueryWrapper);
 | 
					//        IPage iPage = userService.QueryUser(page, lambdaQueryWrapper);
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
        System.out.println(iPage.getTotal());
 | 
					//        System.out.println(iPage.getTotal());
 | 
				
			||||||
        System.out.println(iPage.getRecords());
 | 
					//        System.out.println(iPage.getRecords());
 | 
				
			||||||
        return Result.success(iPage.getTotal(), iPage.getRecords());
 | 
					//        return Result.success(iPage.getTotal(), iPage.getRecords());
 | 
				
			||||||
 | 
					        return dcxService.readJson(query);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					package com.yc.wms.service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.baomidou.mybatisplus.core.conditions.Wrapper;
 | 
				
			||||||
 | 
					import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
				
			||||||
 | 
					import com.baomidou.mybatisplus.extension.service.IService;
 | 
				
			||||||
 | 
					import com.yc.wms.model.User;
 | 
				
			||||||
 | 
					import com.yc.wms.until.QueryPageUtil;
 | 
				
			||||||
 | 
					import com.yc.wms.until.Result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * <p>
 | 
				
			||||||
 | 
					 *  服务类
 | 
				
			||||||
 | 
					 * </p>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author wms
 | 
				
			||||||
 | 
					 * @since 2023-03-04
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public interface DcxService  {
 | 
				
			||||||
 | 
					    Result readJson(QueryPageUtil query);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,73 @@
 | 
				
			||||||
 | 
					package com.yc.wms.service.impl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cn.hutool.json.JSONArray;
 | 
				
			||||||
 | 
					import cn.hutool.json.JSONObject;
 | 
				
			||||||
 | 
					import com.yc.wms.service.DcxService;
 | 
				
			||||||
 | 
					import com.yc.wms.until.QueryPageUtil;
 | 
				
			||||||
 | 
					import com.yc.wms.until.Result;
 | 
				
			||||||
 | 
					import com.yc.wms.until.SpringContextHolder;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.jdbc.object.SqlQuery;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import javax.persistence.EntityManager;
 | 
				
			||||||
 | 
					import javax.persistence.Query;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Service
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
 | 
					public class DcxServiceImpl implements DcxService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private  EntityManager entityManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public Result readJson(QueryPageUtil query) {
 | 
				
			||||||
 | 
					        HashMap map = query.getParam();
 | 
				
			||||||
 | 
					//        String methodType = (String) map.get("methodType");
 | 
				
			||||||
 | 
					//        if(methodType.equals("kchzcx")){//库存汇总查询
 | 
				
			||||||
 | 
					//            return queryKchz(query);
 | 
				
			||||||
 | 
					//        }
 | 
				
			||||||
 | 
					        return queryKchz(query);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Result queryKchz(QueryPageUtil query) {
 | 
				
			||||||
 | 
					        EntityManager entity = SpringContextHolder.getBean(EntityManager.class);
 | 
				
			||||||
 | 
					        HashMap map = query.getParam();
 | 
				
			||||||
 | 
					//        String itemCode = (String) map.get("itemCode");
 | 
				
			||||||
 | 
					        int pageNum = query.getPageNum();//当前页
 | 
				
			||||||
 | 
					        int pageSize = query.getPageSize();//条数
 | 
				
			||||||
 | 
					        System.out.println("当前页:" + pageNum);
 | 
				
			||||||
 | 
					        System.out.println("条数:" + pageSize);
 | 
				
			||||||
 | 
					        String sql = "from (SELECT ROW_NUMBER() OVER(ORDER BY it.id) AS RowNum, it.id ,"
 | 
				
			||||||
 | 
					                + "it.code,"
 | 
				
			||||||
 | 
					                + "it.name"
 | 
				
			||||||
 | 
					                + " from Item it ";
 | 
				
			||||||
 | 
					        sql += ")  AS subquery ";
 | 
				
			||||||
 | 
					        String sqlCount = "select count(id) " + sql;
 | 
				
			||||||
 | 
					        sql = "select * " + sql + " WHERE RowNum BETWEEN " + (pageNum-1) * pageSize + " AND " + (pageNum) * pageSize;
 | 
				
			||||||
 | 
					        log.info("sqlCount:"+sqlCount);
 | 
				
			||||||
 | 
					        log.info("sql:"+sql);
 | 
				
			||||||
 | 
					        log.info("entityManager:"+entity);
 | 
				
			||||||
 | 
					        List<Object[]> list=entity.createNativeQuery(sql).getResultList();
 | 
				
			||||||
 | 
					        List listCount=entity.createNativeQuery(sqlCount).getResultList();
 | 
				
			||||||
 | 
					        JSONArray data = new JSONArray();
 | 
				
			||||||
 | 
					        for(Object[] obj:list){
 | 
				
			||||||
 | 
					            int i=0;
 | 
				
			||||||
 | 
					            JSONObject d = new JSONObject();
 | 
				
			||||||
 | 
					            d.put("id",obj[i++]==null?"":obj[i-1]);
 | 
				
			||||||
 | 
					            d.put("userNo",obj[i++]==null?"":obj[i-1]);
 | 
				
			||||||
 | 
					            d.put("userName", obj[i++]==null?"":obj[i-1]);
 | 
				
			||||||
 | 
					            d.put("passWord","123456");
 | 
				
			||||||
 | 
					            d.put("age",19);
 | 
				
			||||||
 | 
					            d.put("sex",1);
 | 
				
			||||||
 | 
					            d.put("phone", "123");
 | 
				
			||||||
 | 
					            d.put("roleId", 2);
 | 
				
			||||||
 | 
					            d.put("isValid", false);
 | 
				
			||||||
 | 
					            data.add(d);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					       return  Result.success(listCount.size(),data);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,43 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright 2019-2020  the original author or authors.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					 * you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					 * You may obtain a copy of the License at
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					 * See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					 * limitations under the License.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package com.yc.wms.until;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author: liaojinlong
 | 
				
			||||||
 | 
					 * @date: 2020/6/9 17:02
 | 
				
			||||||
 | 
					 * @since: 1.0
 | 
				
			||||||
 | 
					 * @see {@link SpringContextHolder}
 | 
				
			||||||
 | 
					 * 针对某些初始化方法,在SpringContextHolder 初始化前时,<br>
 | 
				
			||||||
 | 
					 * 可提交一个 提交回调任务。<br>
 | 
				
			||||||
 | 
					 * 在SpringContextHolder 初始化后,进行回调使用
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public interface CallBack {
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 回调执行方法
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    void executor();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 本回调任务名称
 | 
				
			||||||
 | 
					     * @return /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    default String getCallBackName() {
 | 
				
			||||||
 | 
					        return Thread.currentThread().getId() + ":" + this.getClass().getName();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,157 @@
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *  Copyright 2019-2020 Zheng Jie
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					 *  you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					 *  You may obtain a copy of the License at
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					 *  distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					 *  See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					 *  limitations under the License.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					package com.yc.wms.until;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
 | 
					import org.springframework.beans.BeansException;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.DisposableBean;
 | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContext;
 | 
				
			||||||
 | 
					import org.springframework.context.ApplicationContextAware;
 | 
				
			||||||
 | 
					import org.springframework.core.env.Environment;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author Jie
 | 
				
			||||||
 | 
					 * @date 2019-01-07
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
 | 
					public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static ApplicationContext applicationContext = null;
 | 
				
			||||||
 | 
					    private static final List<CallBack> CALL_BACKS = new ArrayList<>();
 | 
				
			||||||
 | 
					    private static boolean addCallback = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 针对 某些初始化方法,在SpringContextHolder 未初始化时 提交回调方法。
 | 
				
			||||||
 | 
					     * 在SpringContextHolder 初始化后,进行回调使用
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param callBack 回调函数
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public synchronized static void addCallBacks(CallBack callBack) {
 | 
				
			||||||
 | 
					        if (addCallback) {
 | 
				
			||||||
 | 
					            SpringContextHolder.CALL_BACKS.add(callBack);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName());
 | 
				
			||||||
 | 
					            callBack.executor();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
					    public static <T> T getBean(String name) {
 | 
				
			||||||
 | 
					        assertContextInjected();
 | 
				
			||||||
 | 
					        return (T) applicationContext.getBean(name);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T> T getBean(Class<T> requiredType) {
 | 
				
			||||||
 | 
					        assertContextInjected();
 | 
				
			||||||
 | 
					        return applicationContext.getBean(requiredType);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取SpringBoot 配置信息
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param property     属性key
 | 
				
			||||||
 | 
					     * @param defaultValue 默认值
 | 
				
			||||||
 | 
					     * @param requiredType 返回类型
 | 
				
			||||||
 | 
					     * @return /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) {
 | 
				
			||||||
 | 
					        T result = defaultValue;
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            result = getBean(Environment.class).getProperty(property, requiredType);
 | 
				
			||||||
 | 
					        } catch (Exception ignored) {}
 | 
				
			||||||
 | 
					        return result;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取SpringBoot 配置信息
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param property 属性key
 | 
				
			||||||
 | 
					     * @return /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static String getProperties(String property) {
 | 
				
			||||||
 | 
					        return getProperties(property, null, String.class);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取SpringBoot 配置信息
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param property     属性key
 | 
				
			||||||
 | 
					     * @param requiredType 返回类型
 | 
				
			||||||
 | 
					     * @return /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static <T> T getProperties(String property, Class<T> requiredType) {
 | 
				
			||||||
 | 
					        return getProperties(property, null, requiredType);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 检查ApplicationContext不为空.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private static void assertContextInjected() {
 | 
				
			||||||
 | 
					        if (applicationContext == null) {
 | 
				
			||||||
 | 
					            throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" +
 | 
				
			||||||
 | 
					                    ".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder.");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 清除SpringContextHolder中的ApplicationContext为Null.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private static void clearHolder() {
 | 
				
			||||||
 | 
					        log.debug("清除SpringContextHolder中的ApplicationContext:"
 | 
				
			||||||
 | 
					                + applicationContext);
 | 
				
			||||||
 | 
					        applicationContext = null;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void destroy() {
 | 
				
			||||||
 | 
					        SpringContextHolder.clearHolder();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 | 
				
			||||||
 | 
					        if (SpringContextHolder.applicationContext != null) {
 | 
				
			||||||
 | 
					            log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        SpringContextHolder.applicationContext = applicationContext;
 | 
				
			||||||
 | 
					        if (addCallback) {
 | 
				
			||||||
 | 
					            for (CallBack callBack : SpringContextHolder.CALL_BACKS) {
 | 
				
			||||||
 | 
					                callBack.executor();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            CALL_BACKS.clear();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        SpringContextHolder.addCallback = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 获取 @Service 的所有 bean 名称
 | 
				
			||||||
 | 
					     * @return /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static List<String> getAllServiceBeanName() {
 | 
				
			||||||
 | 
					        return new ArrayList<>(Arrays.asList(applicationContext
 | 
				
			||||||
 | 
					                .getBeanNamesForAnnotation(Service.class)));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3,10 +3,10 @@ server:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
spring:
 | 
					spring:
 | 
				
			||||||
  datasource:
 | 
					  datasource:
 | 
				
			||||||
    url: jdbc:mysql://47.100.54.81:53306/wms?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false
 | 
					    url: jdbc:sqlserver://47.100.54.81:1433;DatabaseName=zwtest
 | 
				
			||||||
    driver-class-name: com.mysql.cj.jdbc.Driver
 | 
					    driver-class-name:  com.microsoft.sqlserver.jdbc.SQLServerDriver
 | 
				
			||||||
    username: root
 | 
					    username: super
 | 
				
			||||||
    password: Youchain@56
 | 
					    password: 1K4QfD%ESd1fe@VJ
 | 
				
			||||||
    hikari:
 | 
					    hikari:
 | 
				
			||||||
      connection-timeout: 60000
 | 
					      connection-timeout: 60000
 | 
				
			||||||
      validation-timeout: 3000
 | 
					      validation-timeout: 3000
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue