当前位置: 首页 > 工具软件 > UReport > 使用案例 >

springboot整合ureport

邹博裕
2023-12-01

1.pom.xm增加如下包

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

2.application.yml增加如下配置

ureport.mysql.provider:
  prefix: mysql-
  disabled: false

3.增加两个config配置文件


import com.bstek.ureport.console.UReportServlet;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.servlet.Servlet;
@ImportResource("classpath:ureport-console-context.xml")
@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackages = "com.fk.config")
public class UreportConfig {



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

}
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class UreportDataSource implements BuildinDatasource {

    private static final String NAME = "MyDataSource";
    private Logger log = LoggerFactory.getLogger(UreportDataSource.class);

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

4.重写部分方法吧报表数据保存到数据库

import cn.hutool.core.util.IdUtil;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.fk.mapper.UreportMapper;
import com.fk.model.Ureport;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Setter
@Component
// 该注解可以利用其 prefix属性值 + 类的属性名 在yml中配置属性值
public class MySQLProvider implements ReportProvider {
    private static final String NAME = "mysql-provider";

    // 特定前缀,ureport底层会调用 getPrefix 方法来获取报表操作的Provier类
    private String prefix = "mysql:";

// 是否禁用
    private boolean disabled;

    @Autowired
    private UreportMapper ureportMapper;

    @Override
    public InputStream loadReport(String file) {
        Templet ureportFile = ureportFileMapper.queryUreportFileByName(getCorrectName(file));
        byte[] content =null;
        if(StringUtils.isNullOrEmpty(ureportFile.getContentXml())){
            content= ureportFile.getContent();
        }else{
            content= ureportFile.getContentXml().getBytes();
        }
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    @Override
    public void deleteReport(String file) {
        ureportMapper.deleteReportFileByName(getCorrectName(file));
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<Ureport> list = ureportMapper.queryReportFileList();
        List<ReportFile> reportList = new ArrayList<>();
        for (Ureport ureportFileEntity : list) {
            reportList.add(new ReportFile(ureportFileEntity.getName(), ureportFileEntity.getCreateTime()));
        }
        return reportList	;
    }

    @Override
    public void saveReport(String file, String content) {
        file = getCorrectName(file);
        Ureport ureportFileEntity = ureportMapper.queryUreportFileEntityByName(file);
        Date currentDate = new Date();
        if(ureportFileEntity == null){
            ureportFileEntity = new Ureport();
            ureportFileEntity.setUuid(IdUtil.simpleUUID());
            ureportFileEntity.setName(file);
            ureportFileEntity.setContent(content.getBytes());
            ureportFile.setContentXml(content);
            ureportFileEntity.setCreateTime(currentDate);
            ureportFileEntity.setCreateCode("0000");
            ureportMapper.insertReportFile(ureportFileEntity);
        }else{
            ureportFileEntity.setContent(content.getBytes());
            ureportFileEntity.setCreateCode("0000");
            ureportFile.setContentXml(content);
            ureportMapper.updateReportFile(ureportFileEntity);
        }
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * 获取没有前缀的文件名
     * @param name
     * @return
     */
    private String getCorrectName(String name){
        if(name.startsWith(prefix)){
            name = name.substring(prefix.length(), name.length());
        }
        return name;
    }

}
具体保存更新方法就不写了

5.model类

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@TableName("ureport")
public class Ureport {
    @TableId
    private String uuid;
    private String name;
    private byte[] content;
    //报表xml
    private String contentXml;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    private String createCode;
}

7.访问地址

```java
ip+端口/ureport/designer

8.后续会出如何整合gateway

 类似资料: