1、创建springboot项目,修改pom.xml添加相关依赖2、增加src/main/resources/context.properties3、修改src/main/resources/application.yml4、增加com.wongoing.config.ReportConfig.java配置类5、增加com.wongoing.config.ReportDataSource.java6、启动com.wongoing.ReportServerApplication.java7、启动日志8、打开报表设计器
1、创建springboot项目,修改pom.xml添加相关依赖pom.xml内容如下:
2、增加src/main/resources/context.properties4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 com.wongoing wgms-report-server 0.0.1-SNAPSHOT wgms-report-server Demo project for Spring Boot 1.8 org.projectlombok lombok provided org.springframework.boot spring-boot-starter-web com.microsoft.sqlserver mssql-jdbc runtime com.oracle.database.jdbc ojdbc8 runtime mysql mysql-connector-java runtime org.postgresql postgresql runtime com.alibaba druid-spring-boot-starter 1.2.8 com.bstek.ureport ureport2-console 2.2.9 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
内容如下:
ureport.fileStoreDir=F:/ureportfiles
内容如下:
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;
//导入ureport-console-context.xml文件
@importResource("classpath:ureport-console-context.xml")
@Configuration
@Slf4j
public class ReportConfig {
@Bean
public ServletRegistrationBean ureport2Servlet() {
return new ServletRegistrationBean<>(new UReportServlet(), "/ureport
@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;
@Configuration
@Slf4j
public class ReportDataSource implements BuildinDatasource {
@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-workspaceureportdemotargetclasses started by zheng in E:sts-workspaceureportdemo)
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,显示效果如下:



