乐趣区

关于springboot:个人学习系列-Spring-Boot-整合-UReport2

工作中总是须要生成各种各样的报表,麻烦的很。最近发现了一个 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 包后,在弹出的窗口中配置数据源连贯信息即可,如下图所示:

  1. Spring Bean,抉择 Spring 上下文中定义好的一个 Bean 来作为数据源,点击图标,在弹出的窗口中输出数据源名称及要采纳的 Bean 的 ID,如下图所示:


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

  1. 通过实现 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 启动我的项目

控制台打印如下信息代表胜利:

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


拜访地址依据配置文件失去的:

2.3 设置数据源






点击确定后数据源就搞完了,当然那些简单的数据查问本人搞去吧!

2.4 设置报表




2.5 保留报表




2.6 各种类型下载链接


集体博客地址

http://www.zhouzhaodong.xyz

GitHub 源码地址

https://github.com/zhouzhaodo…

退出移动版