Ureport2使用和springBoot集成
- 第一步:我们在使用Ureport2我们要导入他的maven依赖
<dependency>
<groupId>com.bstek.ureport</groupId>
<artifactId>ureport2-console</artifactId>
<version>2.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bstek.ureport</groupId>
<artifactId>ureport2-core</artifactId>
<version>2.3.0-SNAPSHOT</version>
</dependency>
- 第二步:我们需要在启动类中将ServletRegistrationBean交给Spring来管理
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new UReportServlet(), "/ureport/*");
}
- 第三步:创建一个WebMvcConfig实现WebMvcConfigurer来加载``` classpath:ureport-console-context.xml文件
@Configuration
@ImportResource(locations = "classpath:ureport-console-context.xml")
public class WebMvcConfig implements WebMvcConfigurer {}
- 第四步:配置ureport访问拦截配置
public class UReportSecurityConfig {
@Autowired
private ReportProperties reportProperties;
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new UReportFilter(reportProperties.isDebug()));
registration.addUrlPatterns("/ureport/*");
registration.setName("UReportFilter");
return registration;
}
}
class UReportFilter implements Filter {
private boolean isDebug;
Logger logger = LoggerFactory.getLogger(UReportFilter.class);
UReportFilter() {
}
UReportFilter(boolean isDebug) {
this.isDebug = isDebug;
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain
filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
logger.info("this is MyFilter,url :" + request.getRequestURI());
if(request.getRequestURI().startsWith("/ureport/designer")) {
PrintWriter writer = response.getWriter();
if (writer != null) {
writer.print("Not allowed in prod env!");
}
return;
}
if(request.getRequestURI().startsWith("/ureport/datasource")) {
PrintWriter writer = response.getWriter();
if (writer != null) {
writer.print("Not allowed in prod env!");
}
return;
}
-
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
5.第五步配置Ureport的内置数据源
@Component
public class ReportDatesource implements BuildinDatasource {
@Autowired
SqlSession sqlSession;
@Override
public String name() {
return "内置数据源";
}
/**
* @return 返回当前采用数据源的一个连接
*/
@Override
public Connection getConnection() {
Connection conn = null;
try {
conn = sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
- 第6步重写Ureport2中的保存方式使的保存xml文件的时候保存在我们创建好的数据库中
@Component
public class DatabaseReportProvider implements ReportProvider {
private String prefix="database:";
private boolean disabled;
@Autowired
IReportService reportService;
/**
* 根据报表名加载报表文件
* @param file 报表名称
* @return 返回的InputStream
*/
@Override
public InputStream loadReport(String file) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
}
Report reportStore = reportService.getByName(file);
try {
if (reportStore != null) {
InputStream in = new ByteArrayInputStream(reportStore.getContent().getBytes());
return in;
}
throw new ReportException("Not found!");
}
catch (Exception e){
throw new ReportException(e);
}
}
/**
* 根据报表名,删除指定的报表文件
* @param file 报表名称
*/
@Override
public void deleteReport(String file) {
if(file.startsWith(prefix)){
file=file.substring(prefix.length(),file.length());
Report reportStore = reportService.getByName(file);
if (reportStore != null) {
reportStore.deleteById();
}
}
}
/**
* 获取所有的报表文件
* @return 返回报表文件列表
*/
@Override
public List<ReportFile> getReportFiles() {
List<ReportFile> reportFileList = new ArrayList<>();
List<Report> reportStoreList = reportService.list();
if(reportStoreList != null) {
for(Report reportStore:reportStoreList) {
ReportFile reportFile = new ReportFile(reportStore.getName(),reportStore.getCreateDate());
reportFileList.add(reportFile);
}
}
return reportFileList;
}
/**
* 保存报表文件
* @param file 报表名称
* @param content 报表的XML内容
*/
@Override
public void saveReport(String file,String content) {
if(file.startsWith(prefix)) {
file = file.substring(prefix.length(), file.length());
}
Report reportStore = reportService.getByName(file);
if(reportStore == null) {
reportStore = new Report();
}
reportStore.setName(file);
reportStore.setContent(content);
reportService.saveOrUpdate(reportStore);
}
@Override
public String getName() {
return "Mysql-数据存储";
}
/**
* @return 返回是否禁用
*/
@Override
public boolean disabled() {
return false;
}
/**
* @return 返回报表文件名前缀
*/
@Override
public String getPrefix() {
return prefix;
}
}
7.现在我们就可以访问Ureport2地址 (http://localhost:项目端口/ureport/designer)
-
使用Ureport总体流程:我们在创建好业务所需要的XML文件。
-
我们要把他的文件名,文件内容保存在我们的数据库中。因为我们自定义了保存过程会自动保存在我们配置好的数据库中,服务器文件保存是基于tomcat我们在下次访问的时候我们保存的文件就是消失
10.我们只需要把http://localhost:项目端口/ureport/preview?_u=(设置的ureport前缀):(文件名)就可以访问到我们的预览模型; 数据字段名: