1. 程式人生 > >spring boot + ureport2整合

spring boot + ureport2整合

UReport2是一款基於架構在Spring之上純Java的高效能報表引擎,通過迭代單元格可以實現任意複雜的中國式報表;在UReport2中,提供了全新的基於網頁的報表設計器,可以在Chrome、Firefox、Edge等各種主流瀏覽器執行(IE瀏覽器除外)。使用UReport2,開啟瀏覽器即可完成各種複雜報表的設計製作。

一、搭建ureport2

  1. 搭建maven專案(目前用idea搭建,spring boot框架)
  2. 在pom.xml中新增先關依賴在pom.xml中新增先關依賴

(1) Ureport2的相關依賴程式碼

<dependencies>標籤下新增ureport2依賴

<dependency>

<groupId>com.bstek.ureport</groupId>

<artifactId>ureport2-console</artifactId>

<version>[version]</version>

</dependency>

(2)連線資料庫的相關依賴(MySQL)(只搭建Ureport2可以不新增資料庫)

<dependencies>標籤下新增 MySQL依賴

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>[version]</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>[version]</version>

</dependency>

(3)Springboot相關依賴

<dependencies>標籤下新增springboot依賴

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<project>標籤下新增springboot依賴

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>[version]</version>

</parent>

1、在src/main/resources下建立application.properties檔案
配置資料庫連線

## 更改 ureport2 的儲存目錄(需真實存在的目錄)

# ureport.fileStoreDir=G:/tool/ureportfiles

##

## 如若不需要更改:

## 1.web專案:預設在專案web\web-inf\ureportfiles下

## 2.spring boot專案:

##   C:\Users\使用者名稱\AppData\Local\Temp\tomcat-docbase.??.8080\WEB-INF\ureportfiles 下

 

# 資料庫連結 資料來源配置

spring.datasource.url=jdbc:mysql://localhost:3306/ag_auth

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driverClassName=com.mysql.jdbc.Driver

在src/main/resources 下建立 context.xml 檔案

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="classpath:ureport-console-context.xml"/>

 

    <!-- 引入配置檔案 -->

    <bean id="propertyConfigurer" parent="ureport.props">

        <property name="location">

            <!-- 讀取配置檔案 -->

            <value>classpath:application.properties</value>

        </property>

    </bean>

</beans>
  1. 在src/main/java下建立 com.bstek.ureport.test 包 package
  2. 在com.bstek.ureport.test包下建立Application.java 檔案在
package com.bstek.ureport.test;
import com.bstek.ureport.console.UReportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;

/**
 * 載入對應的xml配置檔案
 */
@ImportResource("classpath:context.xml")
/** 指定類為應用啟動類 */
@SpringBootApplication
public class Application {
    /**
     * main 函式
     */
    public static void main(String[] args) {
        /** main方法中通過SpringApplication的run方法啟動應用。 */
        SpringApplication.run(Application.class, args);
    }
    /**
     * 進行註冊Servlet
     * 配置 UReport2 需要使用到的servlet
     */

    @Bean
    public ServletRegistrationBean buildUReportServlet() {
        /**
         * @param  servlet
         * @param  urlMappings 值為“/ureport/*”的 urlMappings 是一定不能變的,否則系統將無法執行。
         */
        return new ServletRegistrationBean(new UReportServlet(), "/ureport/*");
    }
}

二、執行main()

  1. 點選Application.java 檔案 → 右鍵 → 點選(run’Application.main()’)
    在這裡插入圖片描述

三、驗收結果

  1. 在瀏覽器輸入地址
    在這裡插入圖片描述