You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # spring-boot-demo-ureport2
  2. > 本 demo 主要演示了 Spring Boot 项目如何快速集成 ureport2 实现任意复杂的中国式报表功能。
  3. UReport2 是一款基于架构在 Spring 之上纯 Java 的高性能报表引擎,通过迭代单元格可以实现任意复杂的中国式报表。 在 UReport2 中,提供了全新的基于网页的报表设计器,可以在 Chrome、Firefox、Edge 等各种主流浏览器运行(IE 浏览器除外)。使用 UReport2,打开浏览器即可完成各种复杂报表的设计制作。
  4. ## 1. 主要代码
  5. 因为官方没有提供一个 starter 包,需要自己集成,这里使用 [pig](https://github.com/pig-mesh/pig) 作者 [冷冷同学](https://github.com/lltx) 开发的 starter 偷懒实现,这个 starter 不仅支持单机环境的配置,同时支持集群环境。
  6. ### 1.1. 单机使用
  7. #### 1.1.1. `pom.xml` 新增依赖
  8. ```xml
  9. <dependency>
  10. <groupId>com.pig4cloud.plugin</groupId>
  11. <artifactId>ureport-spring-boot-starter</artifactId>
  12. <version>0.0.1</version>
  13. </dependency>
  14. ```
  15. #### 1.1.2. `application.yml` 修改配置文件
  16. ```yaml
  17. server:
  18. port: 8080
  19. servlet:
  20. context-path: /demo
  21. spring:
  22. datasource:
  23. url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
  24. username: root
  25. password: root
  26. driver-class-name: com.mysql.cj.jdbc.Driver
  27. ureport:
  28. debug: false
  29. disableFileProvider: false
  30. disableHttpSessionReportCache: true
  31. # 单机模式,本地路径需要提前创建
  32. fileStoreDir: '/Users/yk.shen/Desktop/ureport2'
  33. ```
  34. #### 1.1.3. 新增一个内部数据源
  35. ```java
  36. @Component
  37. public class InnerDatasource implements BuildinDatasource {
  38. @Autowired
  39. private DataSource datasource;
  40. @Override
  41. public String name() {
  42. return "内部数据源";
  43. }
  44. @SneakyThrows
  45. @Override
  46. public Connection getConnection() {
  47. return datasource.getConnection();
  48. }
  49. }
  50. ```
  51. #### 1.1.4. 使用 `doc/sql/t_user_ureport2.sql` 初始化数据
  52. ```mysql
  53. DROP TABLE IF EXISTS `t_user_ureport2`;
  54. CREATE TABLE `t_user_ureport2` (
  55. `id` bigint(13) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  56. `name` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '姓名',
  57. `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  58. `status` tinyint(4) NOT NULL COMMENT '是否禁用',
  59. PRIMARY KEY (`id`)
  60. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
  61. BEGIN;
  62. INSERT INTO `t_user_ureport2` VALUES (1, '测试人员 1', '2020-10-22 09:01:58', 1);
  63. INSERT INTO `t_user_ureport2` VALUES (2, '测试人员 2', '2020-10-22 09:02:00', 0);
  64. INSERT INTO `t_user_ureport2` VALUES (3, '测试人员 3', '2020-10-23 03:02:00', 1);
  65. INSERT INTO `t_user_ureport2` VALUES (4, '测试人员 4', '2020-10-23 23:02:00', 1);
  66. INSERT INTO `t_user_ureport2` VALUES (5, '测试人员 5', '2020-10-23 23:02:00', 1);
  67. INSERT INTO `t_user_ureport2` VALUES (6, '测试人员 6', '2020-10-24 11:02:00', 0);
  68. INSERT INTO `t_user_ureport2` VALUES (7, '测试人员 7', '2020-10-24 20:02:00', 0);
  69. INSERT INTO `t_user_ureport2` VALUES (8, '测试人员 8', '2020-10-25 08:02:00', 1);
  70. INSERT INTO `t_user_ureport2` VALUES (9, '测试人员 9', '2020-10-25 09:02:00', 1);
  71. INSERT INTO `t_user_ureport2` VALUES (10, '测试人员 10', '2020-10-25 13:02:00', 1);
  72. INSERT INTO `t_user_ureport2` VALUES (11, '测试人员 11', '2020-10-26 21:02:00', 0);
  73. INSERT INTO `t_user_ureport2` VALUES (12, '测试人员 12', '2020-10-26 23:02:00', 1);
  74. INSERT INTO `t_user_ureport2` VALUES (13, '测试人员 13', '2020-10-26 23:02:00', 1);
  75. COMMIT;
  76. ```
  77. #### 1.1.5. 访问报表设计器
  78. http://127.0.0.1:8080/demo/ureport/designer
  79. ![报表设计页](http://static.xkcoding.com/spring-boot-demo/ureport2/035330.png)
  80. #### 1.1.6. 开始设计
  81. ##### 1.1.6.1. 选择数据源
  82. 这里就需要使用到上面步骤 1.1.3 创建的内部数据源如图
  83. ![选择数据源](http://static.xkcoding.com/spring-boot-demo/ureport2/040032.png)
  84. 选择数据源
  85. ![选择数据源](http://static.xkcoding.com/spring-boot-demo/ureport2/040117.png)
  86. 此时列表里就会出现数据源
  87. ![数据源列表](http://static.xkcoding.com/spring-boot-demo/ureport2/040237.png)
  88. ##### 1.1.6.2. 选择数据集
  89. 在刚才选中的数据源右键,选择添加数据集
  90. ![选中数据源右键](http://static.xkcoding.com/spring-boot-demo/ureport2/063315.png)
  91. 这里选择上面步骤 1.1.4 中初始化的用户表
  92. ![创建用户报表](http://static.xkcoding.com/spring-boot-demo/ureport2/063845.png)
  93. 预览数据看一下
  94. ![预览数据集数据](http://static.xkcoding.com/spring-boot-demo/ureport2/063955.png)
  95. 点击确定,保存数据集
  96. ![保存数据集](http://static.xkcoding.com/spring-boot-demo/ureport2/064049.png)
  97. ##### 1.1.6.3. 报表设计
  98. 创建报表表头的位置
  99. ![合并单元格](http://static.xkcoding.com/spring-boot-demo/ureport2/064425.png)
  100. 表头内容
  101. ![image-20201124144752390](http://static.xkcoding.com/spring-boot-demo/ureport2/064752.png)
  102. 操作完成之后,长这样~
  103. ![表头美化](http://static.xkcoding.com/spring-boot-demo/ureport2/064916.png)
  104. 然后设置数据的标题行,跟表头设置一样,效果如下图
  105. ![数据的标题行](http://static.xkcoding.com/spring-boot-demo/ureport2/065125.png)
  106. 接下来设置数据
  107. ![id字段配置](http://static.xkcoding.com/spring-boot-demo/ureport2/065658.png)
  108. 其他字段同理,完成之后如下
  109. ![数据配置](http://static.xkcoding.com/spring-boot-demo/ureport2/070440.png)
  110. 此时你可以尝试预览一下数据了
  111. ![预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/070634.png)
  112. ![预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/070813.png)
  113. 关掉,稍微美化一下
  114. ![美化后的预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/070910.png)
  115. 此时数据虽然正常显示了,但是「是否可用」这一列显示0/1 是否可以支持自定义呢?
  116. ![映射数据集](http://static.xkcoding.com/spring-boot-demo/ureport2/071352.png)
  117. 再次预览一下
  118. ![字典映射预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/071428.png)
  119. 顺带再把创建时间的数据格式也改一下
  120. ![时间格式修改](http://static.xkcoding.com/spring-boot-demo/ureport2/072725.png)
  121. 修改后,预览数据如下
  122. ![预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/072753.png)
  123. ##### 1.1.6.4. 保存报表设计文件
  124. ![image-20201124153244035](http://static.xkcoding.com/spring-boot-demo/ureport2/073244.png)
  125. ![保存](http://static.xkcoding.com/spring-boot-demo/ureport2/074228.png)
  126. 点击保存之后,你本地在 `application.yml` 文件中配置的地址就会出现一个 `demo.ureport.xml` 文件
  127. 下次可以直接通过 http://localhost:8080/demo/ureport/preview?_u=file:demo.ureport.xml 这个地址预览报表了
  128. ##### 1.1.6.5. 增加报表查询条件
  129. 还记得我们上面新增数据集的时候,加的条件吗?现在用起来
  130. ![查询表单设计器](http://static.xkcoding.com/spring-boot-demo/ureport2/074641.png)
  131. 查询表单设计
  132. ![拖动元素设计表单查询](http://static.xkcoding.com/spring-boot-demo/ureport2/074936.png)
  133. 配置查询参数
  134. ![完善查询表单](http://static.xkcoding.com/spring-boot-demo/ureport2/075248.png)
  135. 美化按钮
  136. ![按钮样式美化](http://static.xkcoding.com/spring-boot-demo/ureport2/075410.png)
  137. 在预览一下~
  138. ![预览数据-查询条件](http://static.xkcoding.com/spring-boot-demo/ureport2/075640.png)
  139. ### 1.2. 集群使用
  140. 如上文设计好的模板是保存在服务本机的,在集群环境中需要使用统一的文件系统存储。
  141. #### 1.2.1. 新增依赖
  142. ```xml
  143. <dependency>
  144. <groupId>com.pig4cloud.plugin</groupId>
  145. <artifactId>oss-spring-boot-starter</artifactId>
  146. <version>0.0.3</version>
  147. </dependency>
  148. ```
  149. #### 1.2.2. 仅需配置云存储相关参数, 演示为minio
  150. ```yaml
  151. oss:
  152. access-key: lengleng
  153. secret-key: lengleng
  154. bucket-name: lengleng
  155. endpoint: http://minio.pig4cloud.com
  156. ```
  157. > 注意:这里使用的是冷冷提供的公共 minio,请勿乱用,也不保证数据的可靠性,建议小伙伴自建一个minio,或者使用阿里云 oss
  158. ## 2. 坑
  159. Ureport2 最新版本是 `2.2.9`,挺久没更新了,存在一个坑:在报表设计页打开一个已存在的报表设计文件时,可能会出现无法预览的情况,参考 ISSUE:https://github.com/youseries/ureport/issues/393
  160. ![无法预览](http://static.xkcoding.com/spring-boot-demo/ureport2/084852.png)
  161. 解决办法:
  162. ![image-20201124164953947](http://static.xkcoding.com/spring-boot-demo/ureport2/084954.png)
  163. 条件表达式变成 `undefined`,这里需要注意的是,我们的 xml 文件是正常的,只不过是 ureport 解析的时候出错了。
  164. ![条件表达式](http://static.xkcoding.com/spring-boot-demo/ureport2/085114.png)
  165. 点击编辑,重新选择表达式即可解决
  166. ![image-20201124165202295](http://static.xkcoding.com/spring-boot-demo/ureport2/085202.png)
  167. 再次尝试预览
  168. ![斑马纹预览数据](http://static.xkcoding.com/spring-boot-demo/ureport2/085228.png)
  169. > 注意:该可能性出现在报表设计文件中使用了条件属性的情况下,修复方法就是打开文件之后,重新配置条件属性,此处是坑,小伙伴使用时注意下就好,最好的方法就是避免使用条件属性。
  170. ## 3. 感谢
  171. 再次感谢 [@冷冷](https://github.com/lltx) 提供的 starter 及 PR,因个人操作失误,PR 未被合并,抱歉~
  172. ## 4. 参考
  173. - [ureport2 使用文档](https://www.w3cschool.cn/ureport)
  174. - [ureport-spring-boot-starter](https://github.com/pig-mesh/ureport-spring-boot-starter) UReport2 的 spring boot 封装
  175. - [oss-spring-boot-starter](https://github.com/pig-mesh/oss-spring-boot-starter) 兼容所有 S3 协议的分布式文件存储系统

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。