ureport2整合springboot详细教程,从代码到数据源的添加完成

一、官网文档,可以根据文档说明来完成代码部分

http://wiki.bsdn.org/pages/viewpage.action?pageId=76448360

项目效果:

二、springboot搭建就不介绍了!进入正题,一步一步操作,springboot项目建好后,先打开pom.xml即可

1、打开pom.xml,但是这个一般在搭建好springboot项目就会有的,也不排除一些人是手动操作的就可能没有,我这里也写上

<!-- 第一步 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

2、添加web依赖

<!-- 第二步 web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、添加ureport2依赖

<!-- 第三步 ureport -->
<dependency>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-console</artifactId>
    <version>2.2.9</version>
</dependency>

4、打开启动类,在启动类中添加以下方法

// 第四部,ureport2使用到servlet
@Bean
public ServletRegistrationBean buildUReportServlet(){
    return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
}

5、监听加载UReport2提供的spring配置文件ureport-console-context.xml,由于要扩展 所以引入context.xml(这里第四步也一起复制进去了,以防一些人看不懂)

@SpringBootApplication
// 第五步,监听加载UReport2提供的spring配置文件ureport-console-context.xml,由于要扩展 所以引入context.xml
@ImportResource("classpath:context.xml")
public class UreportUpbusApplication {

    public static void main(String[] args) {
        SpringApplication.run(UreportUpbusApplication.class, args);
    }

    // 第四部,ureport2使用到servlet
    @Bean
    public ServletRegistrationBean buildUReportServlet(){
        return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
    }
}

6、在resources下创建context.xml(classpath属性对应的配置文件在resources目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    
<-- 这里空白添加下面的第七步 -->

</beans>

7、添加以下内容,这里在第五步的时候本应该是引入classpath:ureport-console-context.xml,变成classpath:context.xml后,在本文件中导入以让启动类找到此配置文件

<!-- 创建context.xml是第六步,引入ureport2报表xml配置文件 -->
<import resource="classpath:ureport-console-context.xml" />
<!-- 第七步,修改目录文件位置。这里多说几句,官方文档默认是在/WEB-INF/config.properties下面,
WEB-INF每次运行的时候都会动态生成一个临时目录,我们是找不到的,所以我们将位置修改成绝对路径,自己可以找到。
由于config.properties是在resouces配置下,所以使用classpath: -->

<bean id="propertyConfigurer" parent="ureport.props">
    <property name="location">
        <value>classpath:config.properties</value>
    </property>
</bean>

8、根据第七步classpath:config.properties,在resources下添加config.properties配置文件,并添加以下内容。这个自己去指定哪个盘符下的哪个目录。前提是文件夹手动建好

ureport.fileStoreDir=D:/ureportfiles

9、打开pom添加连接池和驱动

<!-- 第九步,添加数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
</dependency>

<!-- 加载Driver驱动程序 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
    <scope>runtime</scope>
</dependency>

10、创建application.yml文件,添加以下内容。(和第九步两个依赖是对应的)

server:
  port: 8080    # HTTP(Tomcat) Port,端口自定义

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/upbus?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai #数据库连接池(druid)
    driver-class-name: com.mysql.cj.jdbc.Driver # 加载Driver驱动

11、启动项目,查看效果,输入localhost:8080/ureport/designer(根据上面第五步中的/ureport/*来的)

12、这就成功了。下面写了一些解释,有些人会对application.yml中的配置有点疑惑

13、添加数据源,后续查看此链接即可。这里要讲一下,配置中的数据库

https://www.cnblogs.com/Seven-cjy/p/9542740.html

注意点:我目前才用的是第一种方式

14、按照第13步操作完成后,点击测试连接,看是否能连接数据库成功,则会显示连接测试成功!然后点击保存

15、添加字段操作

 

16、上述添加完成后,保存一下,不然当刷新页面时候又会恢复原样

17、预览效果,如果预览不通过,在使用.xml文件在页面也不会有效果,因为预览时候可能已经报错

18、保存,建议每做一步保存一步,保存后预览效果

19、打开保存的xml文件,声明:每次保存后的xml文件都是不一样的,文件已经发生了改变

20、合并单元格

21、字段是否有框,默认无框,需要手动添加

22、单元格背景色和字体色,选中单元格自行设置吧。

23、字段设置,根据业务需求来

24、过滤条件

最终效果:

25、父格,表示以哪个为父格

效果:

26、最后将此xml文件保存后,并找到相应文件,复制到代码中进行逻辑处理。再次声明,这两天一直踩坑,如果预览报错,代码中解析xml文件也一样报错,无法通过,所以测试好后在将xml文件复制到项目中

前端:

function loadDatagrid() {
   $.ajax({
      type: "POST",
      url: "${ctx}/report/upbusreport",
      data: $("#searchform").serialize(),
      dataType: "json",
      beforeSend: function (XMLHttpRequest) {
         $("#tabledata").html("正在努力加载,汇总数据较多需要占用较长时间,请稍后......");
      },
      success: function (data) {
         document.getElementById("fz").innerHTML=data.style
         $("#tabledata").html(data.content);
      }
   });
}

后端接口:

/**
 * 车船报表
 * @param sceid
 * @param request
 * @param starttime
 * @param endtime
 * @return
 * @throws ParseException
 */
@RequestMapping("upbusreport")
@ResponseBody
public Object upbusreport(@CookieValue("s8c6e1_sceid") Integer sceid,HttpServletRequest request,String starttime,String endtime,String channelId)  throws ParseException{
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   Map<String, Object> map = new HashMap<String, Object>();
   Date st = null;
   Date et = null;
   if (StringUtils.isNotEmpty(starttime)) {
      st = sdf.parse(starttime);
      map.put("starttime", st);
   }
   if (StringUtils.isNotEmpty(endtime)) {
      et = sdf.parse(endtime);
      map.put("endtime", et);
   }
   HtmlReport htmlReport= exportManager.exportHtml("file:upaybus.ureport.xml",request.getContextPath(),map);
   return htmlReport;
}

大致就这样吧,应该够详细了整个流程!从开始到部署到项目,复制代码只是为了让大家看的大致能懂。这个文件生成后,保存后复制到项目中,保证可以读到此文件即可

27、在测试环境中,若打开文件并预览文件,出现错误,查看是否是驱动或者URL连接问题

修改完驱动后,点击保存,并预览。声明:前提是测试数据库有对应的表,不然会报sql找不到错误。如果能出来数据,再进行报表文件的保存,保存到测试环境中

28、测试环境ok没问题了,提交到svn的代码,同样需要修改

启动ureport生成xml文件的项目,也浏览器中修改本地配置文件,并保存。此时本地也连接了测试数据库

29、刚刚是测试环境保存,同样的操作,本地点击预览是否能查到数据,出现的话,再次保存

30、同时将本地保存xml文件(每次更改,xml配置文件也会更改),复制到项目中,并提交到svn

31、提交这个文件

32、若svn仓库没配置的话,修改仓库,将仓库地址复制进去即可

  • 10
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
UReport2 是一个基于 JasperReport 的开源报表工具,它的特点是支持在线设计报表、动态数据源、可视化配置等功能。Spring Boot 是一个快速开发框架,它可以帮助我们快速搭建 Web 应用程序。下面是 Spring Boot 整合 UReport2 的步骤。 1. 添加 UReport2 的依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.bstek.ureport</groupId> <artifactId>ureport2-console</artifactId> <version>2.8.10</version> </dependency> ``` 2. 配置 UReport2 在 application.properties 文件中添加以下配置: ```properties # UReport2 配置文件 ureport.config.location=classpath:ureport.xml ureport.temp.path=/tmp/ureport/ ureport.export.path=/tmp/ureport/export/ ureport.debug=true ``` 其中,ureport.config.location 指定 UReport2 的配置文件路径,ureport.temp.path 指定 UReport2 的临时文件路径,ureport.export.path 指定 UReport2 的导出文件路径,ureport.debug=true 表示启用 UReport2 的调试模式。 3. 配置数据源 在 application.properties 文件中添加以下配置: ```properties # 数据源配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 ``` 其中,spring.datasource.driver-class-name 指定数据库驱动程序,spring.datasource.url 指定数据库连接地址,spring.datasource.username 和 spring.datasource.password 分别指定数据库用户名和密码。 4. 编写 UReport2 配置文件 在 resources 目录下创建 ureport.xml 文件,配置数据源和报表存储方式: ```xml <?xml version="1.0" encoding="UTF-8"?> <ureport> <!-- 配置数据源 --> <data-sources> <data-source name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName">#{jdbc.driverClassName}</property> <property name="url">#{jdbc.url}</property> <property name="username">#{jdbc.username}</property> <property name="password">#{jdbc.password}</property> </data-source> </data-sources> <!-- 配置报表存储方式 --> <report-storage> <report-storage-in-file path="/tmp/ureport/files" /> </report-storage> </ureport> ``` 其中,data-source 标签用于配置数据源,report-storage 标签用于配置报表存储方式。 5. 创建报表模板 在 resources 目录下创建 report.ureport.xml 文件,用于定义报表模板。 6. 编写 Spring Boot Controller 在 Spring Boot 应用程序中创建一个 Controller,用于处理报表请求: ```java @RestController public class ReportController { @Autowired private ReportManager reportManager; @RequestMapping("/report") public void report(HttpServletRequest request, HttpServletResponse response) throws Exception { String id = request.getParameter("id"); ReportClient reportClient = reportManager.createReportClient("file:/tmp/ureport/files/" + id + ".ureport.xml", request, response); reportClient.execute(); } } ``` 其中,reportManager 是 UReport2 的报表管理器,用于创建报表客户端,report 方法用于处理报表请求,根据请求参数 id 加载对应的报表模板,并生成报表。 7. 运行应用程序 启动 Spring Boot 应用程序,并访问 http://localhost:8080/report?id=report,即可生成报表。其中,report 是报表模板的名称,可以根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值