跳至主要內容

Springboot整合Ureport

holic-x...大约 2 分钟框架组件SpringbootUreport可视化

Springboot整合Ureport

项目构建过程

开发构建说明

【1】创建Springboot项目

【2】引入ureport相关依赖

【3】配置application.yml配置文件

【4】编写config,引入ureport

步骤参考

​ 通过idea快速构建创建springboot项目,引入ureport相关依赖,构建pom.xml参考如下所示

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rsp</groupId>
    <artifactId>springboot-ureport</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-ureport</name>
    <description>springboot ureport</description>
    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.2.6.RELEASE</spring.boot.version>
        <druid.version>1.1.22</druid.version>
        <mysql.version>5.1.47</mysql.version>
        <ureport.version>2.2.9</ureport.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <!-- 引入数据库连接相关jar -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- 引入mysql数据库 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- 引入ureport相关依赖 -->
        <dependency>
            <groupId>com.syyai.spring.boot</groupId>
            <artifactId>ureport-spring-boot-starter</artifactId>
            <version>${ureport.version}</version>
        </dependency>

    </dependencies>

</project>

​ 配置核心配置文件application.yml,引入spring和数据库配置

server:
  port: 8080

spring:
  type: com.alibaba.druid.pool.DruidDataSource
  datasource:
    url: jdbc:mysql://localhost:3306/ureport?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  main:
    allow-bean-definition-overriding: true

  # 配置文件激活
  profiles:
    active: dev

​ 引入UreportConfig实现BuildinDatasource接口构建数据源连接

import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@ImportResource("classpath:ureport-console-context.xml")// 引入ureport-console-context.xml配置文件
@Configuration
public class UreportConfig implements BuildinDatasource {
    @Resource
    DataSource dataSource;
    private Logger log = LoggerFactory.getLogger(getClass());

    @Bean //定义ureport的启动servlet
    @SuppressWarnings("unchecked")
    public ServletRegistrationBean ureportServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new UReportServlet());
        servletRegistrationBean.addUrlMappings("/ureport/*");
        return servletRegistrationBean;
    }

    // 构建数据源名称
    @Override
    public String name() {
        return "customUReportDatasource";
    }

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

}

​ 构建完成,启动项目访问链接:localhost:8080/ureport/designer

构建结果

image-20220126213758559

​ 测试项目构建,随后测试数据源能够正常访问:选择数据源选项卡,配置数据源链接,点击测试链接是否访问正常,如果异常则根据相应提示排查问题

image-20220126213910605

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3