Spring Boot 整合 UReport2

UReport2是一款基于架构在Spring之上纯Java的高性能报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。


工作中总是需要生成各种各样的报表,麻烦的很。最近发现了一个UReport2,上手比较容易。


1、Maven 添加依赖

1 pom.xml


<dependency>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-console</artifactId>
    <version>2.2.9</version>
</dependency>

二、 编写config代码类,用于配置UReport2

1.注册Bean

代码如下:

/**
 * springboot实体类配置
 * context.xml为UReport2的配置文件
 * 
 */
@ImportResource("classpath:context.xml")
@Configuration
public class BeanConfig {
    
    

    @Bean
    public ServletRegistrationBean<Servlet> ureport2Servlet() {
    
    
        return new ServletRegistrationBean<>(new UReportServlet(), "/ureport/*");
    }

}

2.新建UReport2的配置文件context.xml

在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:context.properties"/>
    </bean>

</beans>

新建context.properties

这里我主要是在这里定义UReport2中提供的默认基于文件系统的报表存储目录,需要注意的是,我们设置在ureportfiles文件夹下面存储报表,这个文件夹需要我们手动创建,否则无法保存。。。

# 用于定义UReport2中提供的默认基于文件系统的报表存储目录
ureport.fileStoreDir=src/main/resources/ureportfiles

通过实现com.bstek.ureport.definition.datasource.BuildinDatasource接口提供的内置数据源

启动项目后访问http://localhost:8888/ureport/designer

/**
 * Ureport 数据源
 *
 * @author 
 */

@Component
public class UreportDataSource implements BuildinDatasource {
    
    
    private static final String NAME = "MyDataSource";
    private final Logger log = LoggerFactory.getLogger(UreportDataSource.class);

    @Resource
    private DataSource dataSource;

    /**
     * 数据源名称
     **/
    @Override
    public String name() {
    
    
        return NAME;
    }

    /**
     * 获取连接
     **/
    @Override
    public Connection getConnection() {
    
    
        try {
    
    
            return dataSource.getConnection();
        } catch (SQLException e) {
    
    
            log.error("Ureport 数据源 获取连接失败!");
            e.printStackTrace();
        }
        return null;
    }

}

猜你喜欢

转载自blog.csdn.net/Wyangcsdb/article/details/113887889
今日推荐