SpringBoot项目中使用Ureport2报表工具
1.在spring boot项目中使用Ureport2报表工具,首先需要在maven的pom.xml中添加Ureport2的依赖。
<!-- https://mvnrepository.com/artifact/com.bstek.ureport/ureport2-console -->
<dependency>
<groupId>com.bstek.ureport</groupId>
<artifactId>ureport2-console</artifactId>
<version>2.2.9</version>
</dependency>
2.链接数据库的相关依赖(MySQL)。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3.在src/main/resources目录下的appliction.propertion中添加mysql数据库链接。
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?useSSL=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
4.在src/main/resources目录下的appliction.propertion中添加Ureport2报表的存储路径配置。
## 更改 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 下
5.在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>
6.在Spring boot主类Application.java 添加注解和注册servlet。
package com.example.dppcpreportureport;
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 DppcpReportUreportApplication {
public static void main(String[] args) {
SpringApplication.run(DppcpReportUreportApplication.class, args);
}
/**
* 进行注册Servlet
* 配置 UReport2 需要使用到的servlet
*/
@Bean
public ServletRegistrationBean buildUReportServlet() {
/**
* @param servlet
* @param urlMappings 值为“/ureport/*”的 urlMappings 是一定不能变的,否则系统将无法运行。
*/
return new ServletRegistrationBean(new UReportServlet(), "/ureport/*");
}
}
7.运行启动Spring boot项目在浏览器输入:
http://localhost:8080/ureport/designer |