springboot2.x集成ureport2.2.9搭建报表引擎
1、创建springboot项目,修改pom.xml添加相关依赖
pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
</parent>
<groupId>com.wongoing</groupId>
<artifactId>wgms-report-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wgms-report-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 增加常用数据库驱动依赖 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!-- ureport -->
<dependency>
<groupId>com.bstek.ureport</groupId>
<artifactId>ureport2-console</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、增加src/main/resources/context.properties
内容如下:
ureport.fileStoreDir=F:/ureportfiles
3、修改src/main/resources/application.yml
内容如下:
server:
port: 9999
#################### 项目级数据源配置 ###################
spring:
datasource:
name: archimedes
#generate-unique-name为false时,name的值才会生效
generate-unique-name: false
url: jdbc:mysql://localhost:3306/archimedes?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
username: root
password: root@123
driver-class-name: com.mysql.cj.jdbc.Driver
4、增加com.wongoing.config.ReportConfig.java配置类
内容如下
package com.wongoing.config;
import java.io.IOException;
import javax.servlet.Servlet;
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 org.springframework.core.io.ClassPathResource;
import com.bstek.ureport.UReportPropertyPlaceholderConfigurer;
import com.bstek.ureport.console.UReportServlet;
import lombok.extern.slf4j.Slf4j;
/**
* 功能说明:springboot集成ureport的配置类
* 修改说明:
* @author zheng
* @date 2022-1-21 9:44:57
* @version 0.1
*/
//导入ureport-console-context.xml文件
@ImportResource("classpath:ureport-console-context.xml")
@Configuration
@Slf4j
public class ReportConfig {
/**
* 功能说明:注册report的servlet
* 修改说明:
* @author zheng
* @date 2022-1-21 9:43:49
* @return
*/
@Bean
public ServletRegistrationBean<Servlet> ureport2Servlet() {
return new ServletRegistrationBean<>(new UReportServlet(), "/ureport/*");
}
/**
* 功能说明:加载context.properties配置文件
* 修改说明:
* @author zheng
* @date 2022-1-21 9:44:37
* @return
* @throws IOException
*/
@Bean
public UReportPropertyPlaceholderConfigurer UReportPropertyPlaceholderConfigurer() throws IOException {
UReportPropertyPlaceholderConfigurer propertyConfigurer = new UReportPropertyPlaceholderConfigurer();
propertyConfigurer.setIgnoreUnresolvablePlaceholders(true);
org.springframework.core.io.DefaultResourceLoader df = new org.springframework.core.io.DefaultResourceLoader();
String userDir = System.getProperty("user.dir"); //获取当前jar运行的路径
log.info(userDir);
//如果jar所在目录下有config/context.properties,则加载
String configFilePath = String.format("file:///%s/%s", userDir, "config/context.properties");
log.info(configFilePath);
org.springframework.core.io.Resource configResource = df.getResource(configFilePath);
if (configResource.exists()) {
propertyConfigurer.setLocation(configResource);
return propertyConfigurer;
} else {
log.warn("configFilePath不存在!");
}
//如果jar所在目录下有context.properties,则加载
String filePath = String.format("file:///%s/%s", userDir, "context.properties");
log.info(filePath);
org.springframework.core.io.Resource resource = df.getResource(filePath);
if (resource.exists()) {
propertyConfigurer.setLocation(resource);
return propertyConfigurer;
} else {
log.warn("filePath不存在!");
}
//默认加载jar包内的context.properties或如果jar内没有则加载classpath下的context.properties
ClassPathResource pathResource = new ClassPathResource("context.properties");
propertyConfigurer.setLocation(pathResource);
return propertyConfigurer;
}
}
5、增加com.wongoing.config.ReportDataSource.java
内容如下:
package com.wongoing.config;
import java.lang.reflect.Field;
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import lombok.extern.slf4j.Slf4j;
/**
* 功能说明:ureport默认数据源配置类
* 修改说明:
* @author zheng
* @date 2022-1-21 9:46:09
* @version 0.1
*/
@Configuration
@Slf4j
public class ReportDataSource implements BuildinDatasource {
/**
* 注入application.yml中的datasource
*/
@Autowired
private DataSource dataSource;
@Override
public String name() {
// TODO Auto-generated method stub
String defaultName ="ReportDataSource";
if (this.dataSource == null) {
log.error("注入datasource失败!");
return defaultName;
} else {
if (this.dataSource instanceof DruidDataSource)
{
String name = "ReportDataSource";
DruidDataSource dds = ((DruidDataSource)this.dataSource);
try {
Field[] fields = dds.getClass().getDeclaredFields();
for(Field f : fields) {
if (f.getName().equals("basicProperties")) {
f.setAccessible(true); //设置私有属性允许访问
Object basicProperties = f.get(dds);
Field fieldName = basicProperties.getClass().getDeclaredField("name");
if (null != fieldName ) {
fieldName.setAccessible(true); //设置私有属性允许访问
Object objNameValue = fieldName.get(basicProperties);
if (null != objNameValue) {
name = objNameValue.toString();
}
}
break;
}
}
} catch(Exception ex) {
log.warn("获取name异常");
}
if (null == name || "".equals(name)) {
return defaultName;
} else {
return name;
}
} else {
return defaultName;
}
}
}
@Override
public Connection getConnection() {
try {
return this.dataSource.getConnection();
} catch(Exception ex) {
System.out.println(ex.getMessage());
return null;
}
}
}
6、启动com.wongoing.ReportServerApplication.java
内容如下:
package com.wongoing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReportServerApplication {
public static void main(String[] args) {
SpringApplication.run(ReportServerApplication.class, args);
}
}
7、启动日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.2)
2022-01-21 15:28:10.539 INFO 26048 --- [ main] com.wongoing.ReportApplication : Starting ReportApplication using Java 1.8.0_311 on zhenglibing-pc with PID 26048 (E:\sts-workspace\ureportdemo\target\classes started by zheng in E:\sts-workspace\ureportdemo)
2022-01-21 15:28:10.542 INFO 26048 --- [ main] com.wongoing.ReportApplication : No active profile set, falling back to default profiles: default
2022-01-21 15:28:11.633 INFO 26048 --- [ main] o.s.c.a.ConfigurationClassEnhancer : @Bean method ReportConfig.UReportPropertyPlaceholderConfigurer is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2022-01-21 15:28:12.103 INFO 26048 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2022-01-21 15:28:12.115 INFO 26048 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-21 15:28:12.115 INFO 26048 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-21 15:28:12.266 INFO 26048 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-01-21 15:28:12.266 INFO 26048 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1672 ms
2022-01-21 15:28:12.337 INFO 26048 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2022-01-21 15:28:12.559 INFO 26048 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
_____ __________ __________________ _______ ________ ______________
__ / / /___ __ \___ ____/___ __ \__ __ \___ __ \___ __/__|__ \
_ / / / __ /_/ /__ __/ __ /_/ /_ / / /__ /_/ /__ / ____/ /
/ /_/ / _ _, _/ _ /___ _ ____/ / /_/ / _ _, _/ _ / _ __/
\____/ /_/ |_| /_____/ /_/ \____/ /_/ |_| /_/ /____/
........................................................................................................
. uReport, is a Chinese style report engine licensed under the Apache License 2.0, .
. which is opensource, easy to use,high-performance, with browser-based-designer. .
........................................................................................................
2022-01-21 15:28:14.063 INFO 26048 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ''
2022-01-21 15:28:14.075 INFO 26048 --- [ main] com.wongoing.ReportApplication : Started ReportApplication in 4.033 seconds (JVM running for 5.63)
8、打开报表设计器
在浏览器地址栏输入http://localhost:9999/ureport/designer,显示效果如下: