SpringBoot 打包成jar读取resource下的文件

resources/reportxml/BizQuitData.xml



    
        
        实际退出时间
    
    
        姓名
        身份证明
        计划名称
        已退出本金(元)
        应退出时间
        实际退出时间
    
    
    
        SELECT c.real_name,c.id_card_no,c.phone_no,CONCAT(f.name,f.plan_no,'期') as planName,b.total_principad ,DATE_FORMAT(ifnull(b.predict_advance_out_time,b.last_time),'%Y-%m-%d') AS quit_time_org,DATE_FORMAT(b.quit_time,'%Y-%m-%d') AS quit_time
        FROM ${dataSchema}.biz_plan_total_back b
        LEFT JOIN ${dataSchema}.biz_plan_financing f on b.plan_id = f.id
        LEFT JOIN ${dataSchema}.ucs_user c on b.user_id=c.id
        WHERE 1=1 ${QueryWhere}
        ORDER BY b.last_time DESC
    

读取并解析xml文件

package com.tritonsfs.cac.report.config;

import com.tritonsfs.cac.report.model.ReportXmlConfig.Column;
import com.tritonsfs.cac.report.model.ReportXmlConfig.QueryPanle;
import com.tritonsfs.cac.report.model.ReportXmlConfig.ReportXmlBean;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 2019/6/25 14:52 by 刘赵强
 **/
@Configuration
@Slf4j
public class ReportXmlConfig {
    /**
     * @return 每日退出明细数据
     */
    @Bean
    public ReportXmlBean bizQuitDataBean() {
        try {
            ReportXmlBean reportXmlBean = analysisReportXml("reportxml/BizQuitData.xml");
            reportXmlBean.setRequestUrl("/report/bizQuitData");
            return reportXmlBean;
        } catch (Exception e) {
            log.error("每日退出明细xml解析失败,errorMsg={}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @return 每日注册用户数据
     */
    @Bean
    public ReportXmlBean bizDayRegisterDataBean() {
        try {
            ReportXmlBean reportXmlBean = analysisReportXml("reportxml/BizDayRegisterData.xml");
            reportXmlBean.setRequestUrl("/report/bizDayRegisterData");
            return reportXmlBean;
        } catch (Exception e) {
            log.error("每日注册用户数据Xml解析失败,errorMsg={}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @return 每日投资数据
     */
    @Bean
    public ReportXmlBean bizDayInvestDataBean() {
        try {
            ReportXmlBean reportXmlBean = analysisReportXml("reportxml/BizDayInvestData.xml");
            reportXmlBean.setRequestUrl("/report/bizDayInvestData");
            return reportXmlBean;
        } catch (Exception e) {
            log.error("每日投资数据xml解析失败,errorMsg={}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 解析xml文件
     *
     * @param xmlPath xml文件路径
     * @return bizQuitDataBean
     * @throws Exception 异常
     */
    private ReportXmlBean analysisReportXml(String xmlPath) throws Exception {
        SAXReader reader = new SAXReader();
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xmlPath);
        Document xmlDoc = reader.read(inputStream);
        Element rootElt = xmlDoc.getRootElement();
        boolean isExport = Boolean.valueOf(rootElt.attribute("isExport").getValue());
        ReportXmlBean reportXmlBean = new ReportXmlBean();
        reportXmlBean.setIsExport(isExport);
        reportXmlBean.setName(rootElt.attribute("name").getValue());
        reportXmlBean.setId(rootElt.attribute("id") == null ? "" : rootElt.attribute("id").getValue());

        QueryPanle queryPanle = new QueryPanle();
        Element queryPanel = rootElt.element("queryPanel");
        if (queryPanel != null) {
            //递归遍历当前节点所有的子节点
            List listElement = queryPanel.elements();
            List queryPanelColumnList = new ArrayList<>();
            for (Element e : listElement) {
                Column column = new Column();
                column.setName(e.attribute("name").getText());
                column.setText(e.getText());
                column.setQueryType(e.attribute("queryType").getText());
                column.setType(e.attribute("type").getText());
                column.setTablename(e.attribute("tablename")==null?null:e.attribute("tablename").getText());
                queryPanelColumnList.add(column);
            }
            queryPanle.setColumnList(queryPanelColumnList);
            reportXmlBean.setQueryPanle(queryPanle);
        }

        Element columns = rootElt.element("columns");
        //递归遍历当前节点所有的子节点
        List columnsElementList = columns.elements();
        List columnsList = new ArrayList<>();
        for (Element e : columnsElementList) {
            Column column = new Column();
            column.setName(e.attribute("name").getText());
            column.setText(e.getText());
            columnsList.add(column);
        }
        reportXmlBean.setColumns(columnsList);

        Element listSqlElement = rootElt.element("listSql");
        reportXmlBean.setListSql(listSqlElement.getText());

        return reportXmlBean;

    }
}

XmlBean的实体对象

package com.tritonsfs.cac.report.model.ReportXmlConfig;

import lombok.Data;

import java.util.List;

/**
 * @author 2019/6/25 14:09 by 刘赵强
 * 解析report.xml
 **/
@Data
public class ReportXmlBean {
    private Boolean isExport;

    private String name;

    private String id;

    private String requestUrl;

    private String exportUrl;

    private QueryPanle queryPanle;

    private List columns;

    private String listSql;
}
package com.tritonsfs.cac.report.model.ReportXmlConfig;

import lombok.Data;

import java.util.List;

/**
 * @author 2019/6/27 17:31 by 刘赵强
 **/
@Data
public class QueryPanle {
    private List columnList;
}
package com.tritonsfs.cac.report.model.ReportXmlConfig;

import lombok.Data;

/**
 * @author 2019/6/25 14:27 by 刘赵强
 **/
@Data
public class Column {
    private String name;

    private String type;

    private String queryType;

    private String text;

    private String tablename;
}

重点代码:

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xmlPath);

 

你可能感兴趣的:(SpringBoot)