ureport2整合springboot详细教程,从代码到数据源的添加完成

1、官网文档,可以根据文档说明来完成代码部分

http://wiki.bsdn.org/pages/viewpage.action?pageId=76448360

项目效果:

ureport2整合springboot详细教程,从代码到数据源的添加完成

2、springboot搭建就不介绍了!进入正题,一步一步操作,springboot项目建好后,先打开pom.xml即可

①、打开pom.xml,但是这个一般在搭建好springboot项目就会有的,也不排除一些人是手动操作的就可能没有,我这里也写上

<!-- 第一步 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

②、添加web依赖

<!-- 第二步 web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

③、添加ureport2依赖

<!-- 第三步 ureport -->
<dependency>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-console</artifactId>
    <version>2.2.9</version>
</dependency>

④、打开启动类,在启动类中添加以下方法

// 第四部,ureport2使用到servlet
@Bean
public ServletRegistrationBean buildUReportServlet(){
    return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
}

⑤、监听加载UReport2提供的spring配置文件ureport-console-context.xml,由于要扩展 所以引入context.xml(这里第四步也一起复制进去了,以防一些人看不懂)

@SpringBootApplication
// 第五步,监听加载UReport2提供的spring配置文件ureport-console-context.xml,由于要扩展 所以引入context.xml
@ImportResource("classpath:context.xml")
public class UreportUpbusApplication {

    public static void main(String[] args) {
        SpringApplication.run(UreportUpbusApplication.class, args);
    }

    // 第四部,ureport2使用到servlet
    @Bean
    public ServletRegistrationBean buildUReportServlet(){
        return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
    }
}

⑥、在resources下创建context.xml(classpath属性对应的配置文件在resources目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    
<-- 这里空白添加下面的第七步 -->

</beans>

⑦、添加以下内容,这里在第五步的时候本应该是引入classpath:ureport-console-context.xml,变成classpath:context.xml后,在本文件中导入以让启动类找到此配置文件

<!-- 创建context.xml是第六步,引入ureport2报表xml配置文件 -->
<import resource="classpath:ureport-console-context.xml" />
<!-- 第七步,修改目录文件位置。这里多说几句,官方文档默认是在/WEB-INF/config.properties下面,
WEB-INF每次运行的时候都会动态生成一个临时目录,我们是找不到的,所以我们将位置修改成绝对路径,自己可以找到。
由于config.properties是在resouces配置下,所以使用classpath: -->

<bean id="propertyConfigurer" parent="ureport.props">
    <property name="location">
        <value>classpath:config.properties</value>
    </property>
</bean>

⑧、根据第七步classpath:config.properties,在resources下添加config.properties配置文件,并添加以下内容。这个自己去指定哪个盘符下的哪个目录。前提是文件夹手动建好

ureport.fileStoreDir=D:/ureportfiles

⑨、打开pom添加连接池和驱动

<!-- 第九步,添加数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
</dependency>

<!-- 加载Driver驱动程序 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
    <scope>runtime</scope>
</dependency>

⑩、创建application.yml文件,添加以下内容。(和第九步两个依赖是对应的)

server:
  port: 8080    # HTTP(Tomcat) Port,端口自定义

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/upbus?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai #数据库连接池(druid)
    driver-class-name: com.mysql.cj.jdbc.Driver # 加载Driver驱动

⑩①、启动项目,查看效果,输入localhost:8080/ureport/designer(根据上面第五步中的/ureport/*来的

ureport2整合springboot详细教程,从代码到数据源的添加完成

⑩②、这就成功了。下面写了一些解释,有些人会对application.yml中的配置有点疑惑

ureport2整合springboot详细教程,从代码到数据源的添加完成

⑩③、添加数据源,后续查看此链接即可。这里要讲一下,配置中的数据库

https://www.cnblogs.com/Seven-cjy/p/9542740.html

注意点:我目前才用的是第一种方式

ureport2整合springboot详细教程,从代码到数据源的添加完成

ureport2整合springboot详细教程,从代码到数据源的添加完成