SpringBoot整合Ureport2实现报表详细/springboot集成ureport

本文介绍了如何在SpringBoot项目中集成UReport2,包括Maven配置、DataSource设置、报表预览与导出的方法。重点讲解了配置文件的使用和访问控制URL的结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot+ureport2

源码下载

springboot集成UReport2源码_ureport2-互联网文档类资源-CSDN下载

 

正文

//maven依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--ureport-->
        <dependency>
            <groupId>com.syyai.spring.boot</groupId>
            <artifactId>ureport-spring-boot-starter</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

 配置类

import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@ImportResource("classpath:ureport-console-context.xml")//不加项目能够启动但是会导致加载数据源报错或加载不了
@Configuration
@EnableAutoConfiguration
public class UreportConfig implements BuildinDatasource {
    @Resource
    DataSource dataSource;
    private Logger log = LoggerFactory.getLogger(getClass());

    @Bean //定义ureport的启动servlet
    public ServletRegistrationBean buildUreportServlet(){
        return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
    }

    @Override
    public String name() {
        return "System";
    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("Ureport 数据源获取连接失败!");
            e.printStackTrace();
        }
        return null;
    }
}

 配置文件 在类路径下建一个ureport.properties 配置报表保存路径

ureport.disableHttpSessionReportCache=false

#UReport2默认报表存储
ureport.disableFileProvider=false
ureport.fileStoreDir=d:/ureportfiles
ureport.debug=true

 http://localhost:8080/ureport/designer

在上面的URL中有个名为“_u”的参数,它是用来指定当前要预览的报表模版名称,如果是对正在设计器中的报表进行预览,那么它的值就是“p”,这是一个约定的值,表示要预览的是正在设计器中的报表,所以可以看到我们演示时预览的URL都是这样:http://localhost:8080/ureport2-demo/ureport/preview?_u=p 。如果预览的不是正在设计器中设计的模版,那么只需要给出具体的报表名称即可,需要注意的是,这里的报表名称要以其ReportProvider中要求的前缀开始,比如http://localhost:8080/ureport2-demo/ureport/preview?_u=classpath:test.ureport.xml等。

重要:

直接访问地址:localhost:8080/ureport/preview?_u=file:文件名

localhost:8080/ureport/preview?_u=file:xiangmu.ureport.xml

       报表预览后,通过预览页面上方工具栏中一排按钮,可以实现报表的PDF打印、导出Word、Excel、PDF等,对应的相关URL如下:

URL

说明

http://ip/host[:port]/[context-path]/ureport/pdf/show?_u=报表名称

这个URL将向浏览器写入一个PDF文件流,在Chrome、Firefox、Edge、Safari这些浏览器中将会直接在线

显示PDF文件内容,这样可以实现在线浏览PDF并打印输出

http://ip/host[:port]/[context-path]/ureport/word?_u=报表名称导出对应报表的Word文件
http://ip/host[:port]/[context-path]/ureport/excel?_u=报表名称不分页导出对应报表的Excel文件
http://ip/host[:port]/[context-path]/ureport/excel/paging?_u=报表名称分页导出对应报表的Excel文件
http://ip/host[:port]/[context-path]/ureport/excel/sheet?_u=报表名称分页分Sheet导出对应报表的Excel文件
http://ip/host[:port]/[context-path]/ureport/pdf?_u=报表名称导出对应报表的PDF文件

### SpringBoot 集成 UReport 示例教程 #### 1. 添加依赖 为了将 UReport 集成SpringBoot 项目中,首先需要在 `pom.xml` 文件中引入必要的依赖项。以下是具体的 Maven 依赖配置: ```xml <dependency> <groupId>com.bstek.ureport</groupId> <artifactId>ureport2-console</artifactId> <version>2.2.9</version> </dependency> ``` 此依赖会自动导入 UReport 所需的核心库以及控制台功能[^3]。 --- #### 2. 数据库连接设置 UReport 的运行依赖于数据库支持,因此需要确保项目的数据库连接已正确配置。通常情况下,在 `application.properties` 或 `application.yml` 中完成如下配置即可: ##### 使用 application.properties: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` ##### 使用 application.yml: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: root password: password driver-class-name: com.mysql.cj.jdbc.Driver ``` 上述配置用于指定 MySQL 数据库的相关参数,具体 URL 和凭证应根据实际环境调整。 --- #### 3. 启动类配置 为了让 UReport 控制台能够正常启动并访问,需要在 SpringBoot 应用程序的主入口类上添加特定注解以启用 Web 功能和支持静态资源加载。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 此外,还需要创建一个简单的 Java 配置类来初始化 UReport 环境: ```java import com.bstek.ureport.console.UReportConsoleConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UReportConfig { @Bean public UReportConsoleConfig ureportConsoleConfig() { return new UReportConsoleConfig(); } } ``` 该配置类的作用是注册 UReport 控制台所需的 Bean 实例,从而允许开发者通过浏览器访问报表设计器界面。 --- #### 4. 访问 UReport 控制台 成功启动应用程序后,默认可以通过以下路径访问 UReport 控制台页面: ``` http://localhost:8080/ureport/designer ``` 登录默认用户名为 `admin`,密码同样为 `admin`。首次进入时可能需要修改初始密码。 --- #### 5. 下载完整示例代码 如果希望获取完整的整合案例,可以参考两个公开仓库中的资料: - **GitCode 地址**: - [https://gitcode.com/gh_mirrors/spr/springboot-ureport](https://gitcode.com/gh_mirrors/spr/springboot-ureport)[^1] - [https://gitcode.com/open-source-toolkit/55d8f](https://gitcode.com/open-source-toolkit/55d8f)[^2] 这些资源包含了实现 SpringBoot 整合 UReport 的全部代码和配置文件,适合新手快速入门。 --- ### 注意事项 - 如果计划自定义 UReport 的行为(如更改存储方式),则需要额外扩展其核心逻辑。 - 默认情况下,UReport 将模板数据保存至数据库中;如有特殊需求可考虑其他持久化方案。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亿万富翁进化中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值