24

Spring Boot 整合 UReport2

 3 years ago
source link: http://www.zhouzhaodong.xyz/archives/中国式报表快速开发神器
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

工作中总是需要生成各种各样的报表,麻烦的很。最近发现了一个UReport2,据说可以实现复杂的中国式报表,有点小激动。。。

1. 新建springboot项目

1.1 pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

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

1.2 application.yml

server:
  port: 8888
# 数据库链接 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://IP地址:3306/数据库名称?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&serverTimezone=GMT%2B8
    username: 数据库用户名
    password: 数据库密码

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

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

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

}

1.4 新建UReport2的配置文件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>

1.5 新建context.properties

这里我主要是在这里定义UReport2中提供的默认基于文件系统的报表存储目录:

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

这里需要注意的是,我们设置在ureportfiles文件夹下面存储报表,这个文件夹需要我们手动创建,否则无法保存。。。

1.6 这里需要定义内置数据源

这里需要注意的是数据源连接方式有三种:

  1. 直接连接数据库,就是在项目的classpath中添加好相应数据库的驱动Jar包后,在弹出的窗口中配置数据源连接信息即可,如下图所示:

    myQB732.png!mobile
  2. Spring Bean,选择Spring上下文中定义好的一个Bean来作为数据源,点击图标,在弹出的窗口中输入数据源名称及要采用的Bean的ID,如下图所示:

    neyeuif.png!mobile

    保存后,就可以在这个数据源下添加具体的数据集,添加方法就是在这个数据源下右键,在弹出的菜单中选择添加数据集,在弹出的窗口中定义数据集名称、对应的方法名以及返回对象类型,如下图所示:

    Yzuaeuz.png!mobile
  3. 通过实现com.bstek.ureport.definition.datasource.BuildinDatasource接口提供的内置数据源:

    之前已经在application.yml里面配置数据库信息了。

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

@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;
    }

}

2. 测试

2.1 启动项目

控制台打印如下信息代表成功:

veAV3yY.png!mobile

2.2 访问 http://localhost:8888/ureport/designer

QbAJzaR.png!mobile

访问地址根据配置文件得到的:

QrmI3ub.png!mobile

2.3 设置数据源

MBnARja.png!mobileJVNZZfJ.png!mobilevemM32F.png!mobileRziYZf3.png!mobilevqQFBjr.png!mobile 点击确定后数据源就搞完了,当然那些复杂的数据查询自己搞去吧!

2.4 设置报表

byam2au.png!mobileaM3yeqV.png!mobilenQJF736.png!mobileY3mu2qR.png!mobile

2.5 保存报表

eAVBNj6.png!mobilevuey2aR.png!mobileMB3u6fF.png!mobilefIZ7v2Z.png!mobile

2.6 各种类型下载链接

zi6NFbE.png!mobileQ7JV3eR.png!mobile

GitHub源码地址

https://github.com/zhouzhaodong/springboot/tree/master/spring-boot-ureport2


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK