Personal learning series - Spring Boot integration UReport2

It is always necessary to generate various reports in work, which is very troublesome. Recently, I found a UReport2, which is said to be able to realize complex Chinese style reports. I'm a little excited...

1. Create a new springboot project

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
# Database link data source configuration
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://IP address: 3306 / database name? useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&serverTimezone=GMT%2B8
    username: Database user name
    password: Database password

1.3 write a config code class to configure UReport2

/**
 * springboot Entity class configuration
 * context.xml Configuration file for UReport2
 * @author zhouzhaodong
 */
@ImportResource("classpath:context.xml")
@Configuration
public class BeanConfig {

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

}

1.4 create a new configuration file context for UReport2 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"/>
    <!-- Import profile -->
    <bean id="propertyConfigurer" parent="ureport.props">
        <property name="location" value="classpath:context.properties"/>
    </bean>

</beans>

1.5 create a new context properties

Here I mainly define the default file system based report storage directory provided in UReport2:

# Used to define the default file system based report storage directory provided in UReport2
ureport.fileStoreDir=src/main/resources/ureportfiles

It should be noted here that we set the report to be stored under the ureportfiles folder. This folder needs to be created manually, otherwise it cannot be saved...

1.6 the built-in data source needs to be defined here

It should be noted that there are three ways to connect data sources:

  1. To directly connect to the database is to add the driver Jar package of the corresponding database in the classpath of the project, and then configure the data source connection information in the pop-up window, as shown in the following figure:

  1. Spring Bean: select a Bean defined in the spring context as the data source, click the icon, and enter the name of the data source and the ID of the Bean to be used in the pop-up window, as shown in the following figure:


After saving, you can add a specific dataset under this data source by right clicking the data source, selecting Add dataset in the pop-up menu, and defining the dataset name, corresponding method name and return object type in the pop-up window, as shown in the following figure:

  1. By implementing com bstek. ureport. definition. datasource. Built in data source provided by buildindatasource interface:

Previously in application The database information is configured in YML.

/**
 * Ureport data source
 *
 * @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;

    /**
     * Data source name
     **/
    @Override
    public String name() {
        return NAME;
    }

    /**
     * Get connection
     **/
    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("Ureport Failed to get connection from data source!");
            e.printStackTrace();
        }
        return null;
    }

}

2. Test

2.1 start up project

Successful printing of console information means:

2.2 access http://localhost:8888/ureport/designer


The access address is obtained according to the configuration file:

2.3 setting data source






After clicking OK, the data source will be finished. Of course, those complex data queries can be done by yourself!

2.4 setting report




2.5 saving reports




2.6 various types of download links


Personal blog address

http://www.zhouzhaodong.xyz

GitHub source address

https://github.com/zhouzhaodo...

Tags: Spring Boot

Posted by skoobi on Sun, 15 May 2022 22:28:19 +0300