2019-05-21 SpringBoot集成UReport2

1. 引入依赖

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

2. 配置Servlet

1 @Bean
2 public ServletRegistrationBean<Servlet> ureport2Servlet(){
3   return new ServletRegistrationBean<Servlet>(new UReportServlet(), "/ureport/*");
4 }

注意:"/ureport/*" 是固定的

3. 配置自己的报表存储方式(我的是数据库存储)

  1    /**
  2      *    配置报表存储
  3      * @return
  4      */
  5     @Bean
  6     public ReportProvider reportProvider() {
  7         final String NAME = "数据库存储";
  8         final boolean DISABLED = false;
  9         final String PREFIX = "DB:"; // 不能为空,否则报表存取不了
 10         
 11         return new ReportProvider() {
 12 
 13             @Override
 14             public InputStream loadReport(String file) {
 15             
 16 
 17                 Template one = templateService.getOne(new QueryWrapper<Template>().eq(false, "name", file));//("fName", file));
 18             
 19                 if(one!=null) {
 20                     return new ByteArrayInputStream(one.getTemplate()); 
 21                 }
 22             
 23                 return null;
 24             }
 25 
 26             @Override
 27             public void deleteReport(String file) {                
 28                 
 29                 templateService.remove(new QueryWrapper<Template>().eq(false,"name", file));
 30                 
 31             }
 32 
 33             @Override
 34             public List<ReportFile> getReportFiles() {
 35                 List<Template> list = templateService.list();
 36                 List<ReportFile> reportList = new ArrayList<>();
 37                 
 38                 for (Template template : list) {
 39                     reportList.add(new ReportFile(getCorrectName(template.getName()), 
                          Date.from(template.getCreateTime().atZone(ZoneId.systemDefault()).toInstant())));
40 } 41 return reportList ; 42 } 43 44 @Override 45 public void saveReport(String file, String content){ 46 47 Template one = templateService.getOne(new QueryWrapper<Template>().eq(false,"name", file)); 48 49 if(one == null){ 50 51 one = new Template(); 52 one.setName(file); 53 54 one.setTemplate(content.getBytes()); 55 56 one.setCreateTime(LocalDateTime.now()); 57 58 User user = (User) SecurityUtils.getSubject().getPrincipal(); 59 one.setCreator(user.getUsername()); 60 one.setCreatorID(user.getUserId()+""); 61 62 templateService.save(one); 63 }else{ 64 one.setTemplate(content.getBytes()); 65 one.setCreateTime(LocalDateTime.now()); 66 templateService.updateById(one); 67 } 68 69 70 } 71 72 /** 73 * 报表存储命名 74 */ 75 @Override 76 public String getName() { 77 78 return NAME; 79 } 80 81 @Override 82 public boolean disabled() { 83 return DISABLED; 84 } 85 86 @Override 87 public String getPrefix() { 88 89 return PREFIX; 90 } 91 92 private String getCorrectName(String name){ 93 if(name.startsWith(PREFIX)){ 94 name = name.substring(PREFIX.length(), name.length()); 95 } 96 return name; 97 } 98 99 100 }; 101 }

注意:① 前缀不能为空;② 获得模板列表时需要把前缀去掉;因为删除和加载模板时会自动加上前缀,假如原本前缀是DB:,那么进行加载和删除时,报表前缀则会变成 DB:DB:

4. 隐藏系统自带的保存配置

从UReport2核心包中取出ureport.properties放到类根路径下,修改配置

1 ## UReport2 配置文件
2 
3 ureport.disableHttpSessionReportCache=true
4 
5 #UReport2默认报表存储
6 ureport.disableFileProvider=true
7 ureport.fileStoreDir=d:/ureportfiles
8 
9 ureport.debug=true

5. 配置内置数据源

 1     /**
 2      *     配置数据源
 3      * @param dataSource
 4      * @return
 5      */
 6     @Bean
 7     public BuildinDatasource buildinSystemDatasource(@Qualifier("systemDataSource")DataSource dataSource) {
 8         return new BuildinDatasource() {
 9 
10             @Override
11             public String name() {
12                 return "System";
13             }
14 
15             @Override
16             public Connection getConnection() {
17                 try {
18                     return dataSource.getConnection();
19                 } catch (SQLException e) {
20                     
21                     e.printStackTrace();
22                 }
23                 return null;
24 
25             }
26             
27         };
28     }
29     
30     /**
31      *     配置数据源
32      * @param dataSource
33      * @return
34      */
35     @Bean
36     public BuildinDatasource buildinDemo1Datasource(@Qualifier("demo1DataSource")DataSource dataSource) {
37         return new BuildinDatasource() {
38             
39             @Override
40             public String name() {
41                 return "Demo1";
42             }
43             
44             @Override
45             public Connection getConnection() {
46                 try {
47                     return dataSource.getConnection();
48                 } catch (SQLException e) {
49                     
50                     e.printStackTrace();
51                 }
52                 return null;
53                 
54             }
55             
56         };
57     }

6. UReport2的使用可以参考

网页文档:https://www.w3cschool.cn/ureport/?

视频教程:http://pan.baidu.com/s/1boWTxF5,密码:98hj

posted @ 2019-05-21 23:25  JaminLee  阅读(3269)  评论(0编辑  收藏  举报