使用手册
一、Maven 项目中引入 URepost2 的方式如下:
1. pom.xml 文件的配置
4.0.0
com.bstek.ureport
ureport2-demo
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
Beans 包-->
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.5.4
com.bstek.ureport
ureport2-console
2.2.9
mysql
mysql-connector-java
5.1.46
org.springframework.boot
spring-boot-starter-web
sonatype
https://oss.sonatype.org/content/groups/public/
2. resources 下 context.xml 文件的配置
3. resources/config 下 application.yml 文件的配置
spring:
datasource:
url:
jdbc:mysql://localhost:3306/notified?useUnicode=true&character
Encoding=UTF-8&zeroDateTimeBehavior=convertToNull
username: root
password: Admin123@
driver-class-name: com.mysql.jdbc.Driver
4. Application.java 文件的配置
package com.bstek.ureport.test;
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;
/**
* 加载对应的自定义的配置文件
*/
@ImportResource("classpath:context.xml")
/** 指定类为应用启动类 */
@SpringBootApplication
public class Application {
/**
* main 函数
*/
public static void main(String[] args) {
/** main 方法中通过 SpringApplication 的 run 方法启动应用。 */
SpringApplication.run(Application.class, args);
}
/**
* 进行注册 Servlet
* 配置 UReport2 需要使用到的 servlet
*/
@Bean
public ServletRegistrationBean buildUReportServlet() {
/**
* @param servlet
* @param urlMappings 值为“/ureport/*”的 urlMappings 是一定不能
变的,否则系统将无法运行。
*/
return new ServletRegistrationBean(new UReportServlet(), "/ureport/*");
}
}
二、内置数据源的配置方式
package com.bstek.ureport.test;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
/**
* Ureport 内置数据源
*
*/
@Component
public class UreportDataSource implements BuildinDatasource {
private static final String NAME = "MyDataSource";
private Logger log = LoggerFactory.getLogger(UreportDataSource.class);
@Autowired
private DataSource dataSource;
/**
* 数据源名称
**/
public String name() {
return NAME;
}
/**
* 获取连接
**/
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
log.error("Ureport 数据源 获取连接失败!");
e.printStackTrace();
}
return null;
}
}
三、SpringBean 数据源的配置方式
1. TestBean 的配置
package com.bstek.ureport.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;
/**
* 声明一个类,相当于一个配置文件 bean → 注解@Component
* 把普通 pojo 实例化到 spring 容器中,相当于配置文件中的
*/
@Component("testBean")
public class TestBean {
/**
* 方法必须包含三个参数:String,String,Map
*
* @return 集合类型 ,包含字段:id,name,salary
*/
public
List
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
四、报表存储的方式
1. 默认存储方式
1.1、默认存储位置:
Tomcat>WBE-INF>urepostfiles
1.2、注意
此方式不适用与微服务的 java -jar 启动方式,一旦微服务重启,保存的报表将丢失。
1.3、默认存储禁用方式
ureport.disableFileProvider=true,即可禁用默认提供的文件报错机制。需在
properties 文件中配置,在 yml 中配置是读取不到的。
2. 自定义本地存储方式
2.1、自定义存储的配置
需在 properties 文件中配置 ureport.fileStoreDir=D://ftp1
2.2、自定义本地存储的实现类
package com.bstek.ureport.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
/**
*/
@Component
public class FileReportProvider implements ReportProvider,ApplicationContextAware{
private String prefix="file:";
@Value("${ureport.fileStoreDir}")
private String fileStoreDir;
private boolean disabled;
@Override
public InputStream loadReport(String file) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
}
String fullPath=fileStoreDir+"/"+file;
try {