SpringBoot整合UReport2(报表开发)(配置类版)
⽂章⽬录
前⾔
最近在公司⾥⾯要做对报表的修改,原项⽬⽤的是第三⽅报表软件,需要安装第三⽅软件,有⽔印,因此选择了基于Apache-2.0协议开源的中式报表引擎UReport2
⼀、UReport2是什么?
UReport2是⼀款⾼性能的架构在Spring之上纯Java报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。
在UReport2中,提供了全新的基于⽹页的报表设计器,可以在Chrome、Firefox、Edge等各种主流浏览器运⾏(IE浏览器除外),打开浏览器即可完成各种复杂报表的设计制作。
⼆、使⽤步骤
当然该项⽬是基于springboot的,需要创建springboot⼯程
1.导⼊POM
<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>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
2.添加context.properties
在resources⽬录下创建context.properties⽂件
内容为:
# ⽤于定义UReport2中提供的默认基于⽂件系统的报表存储⽬录
ureport.fileStoreDir=F:/ureportfiles
3.编写config配置类
//导⼊l⽂件
@ImportResource("l")
@Slf4j
public class ReportConfig implements BuildinDatasource {
//添加 report 的servlet
@Bean
public ServletRegistrationBean<Servlet>ureport2Servlet(){
return new ServletRegistrationBean<>(new UReportServlet(),"/ureport/*");
}
//这⼀步省略了创建配置⽂件
@Bean
public UReportPropertyPlaceholderConfigurer UReportPropertyPlaceholderConfigurer(){
UReportPropertyPlaceholderConfigurer propertyConfigurer=new UReportPropertyPlaceholderConfigurer();
propertyConfigurer.setIgnoreUnresolvablePlaceholders(true);
ClassPathResource pathResource=new ClassPathResource("context.properties");
propertyConfigurer.setLocation(pathResource);
return propertyConfigurer;
}
//创建数据源,应该单独在⼀个配置类中,这⾥就写在同⼀个配置类中
@Primary
@Bean
public DataSource businessDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("sql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
/**
* 数据源名称
**/
@Override
public String name(){
return"ReportSource";
}
/**
* 获取连接
**/
@Override
springboot架构图public Connection getConnection(){
try{
return businessDataSource().getConnection();
}catch(SQLException e){
<("Ureport 数据源获取连接失败!");
e.printStackTrace();
}
return null;
}
}
省略的配置⽂件(因为⽤配置类配置类了,这⾥可以不⽤创建) ![如果不⽤配置类,那么⽤该配置⽂件也可以](img-
blog.csdnimg/ca898a98c219495fa0a4d1e5e0d5196a.png?x-oss-
process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6YW36YW355qE54ix5L2g,size _20,color_FFFFFF,t_70,g_se,x_16)
4.访问{ip}{端⼝}/ureport/designer
5.配置数据源
6.数据集配置
7.数据映射
映射前
映射后
8.过滤条件
过滤后
9.⾏列操作
⾏列操作和excel差不多
选择单元格,右键
10.⾏类型
1.标题⾏
不算⾏内容,此时显⽰的话⾏内容是2,标题⾏只显⽰1次
2.重复表头
3.重复表尾
重复表尾,和表头差不多,只不过⼀个是头,⼀个是尾
4.总结⾏
总结
UReport2使⽤起来还是⽐较⽅便的,和现有的项⽬集成起来也⽐较简单相关代码查看我的gitee库