jasperreport JAVA API集成到spring boot中

上一篇文章http://blog.csdn.net/pspr2/article/details/72843356讲了如何集成到spring MVC中,这次介绍集成到开源框架spring boot中。

1.自建项目,并建立好与数据库的连接以及web项目。

类似以下配置:

1.1 application.yml

# Server settings
server:
  port: 8080
  address: localhost

# SPRING PROFILES
spring:
  http:
    encoding.charset: UTF-8
    encoding.enable: true
    encoding.force: true
  datasource:
    name: gendb
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/{数据库}?useConfigs=maxPerformance&characterEncoding=utf8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    minIdle: 1
    maxActive: 2
    initialSize: 1
    timeBetweenEvictionRunsMillis: 3000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 1
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    filters: stat,wall,slf4j
1.2 pom.xml

  
    
    
      net.sf.jasperreports
      jasperreports
      6.4.0 
    
    
      org.apache.poi
      poi
      3.10.1 
    
    
      com.lowagie
      itextasian
      2.1.7.js2 
    
    
      org.springframework.boot
      spring-boot-starter-web
      1.4.3.RELEASE
    
  

这样,配置方面就完成了。

2.具体代码部分

2.1 先定义ViewResolver

package *.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
import org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver;

/**
 * jasper report 配置class
 * Created by X-J on 2017/6/1.
 */
@Configuration
public class JasperConfig extends WebMvcConfigurerAdapter {

  private final static String REPORT_DATA_KEY = "datasource";
  private final static String PATH_KEY = "classpath:jaspertemplate/";
  private final static String TYPE_KEY = ".jrxml";
  private final static String VIEW_KEY = "Report";

  @Override
  public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }

  @Bean
  public JasperReportsViewResolver getJasperReportsViewResolver() {
    JasperReportsViewResolver resolver = new JasperReportsViewResolver();
    resolver.setPrefix(PATH_KEY); //resource文件夹下放模板的路径
    resolver.setSuffix(TYPE_KEY); //模板文件的类型,这里选用jrxml而不是编译之后的jasper

    //JasperReportsMultiFormatView定义了ReportDataKey,这里给定key为datasource,后面controller的时候会用到
    resolver.setReportDataKey(REPORT_DATA_KEY);
    resolver.setViewNames("*" + VIEW_KEY + "*"); //视图名称,模板名称需要符合 *你定义的key* 如*Report*
    resolver.setViewClass(JasperReportsMultiFormatView.class); //视图类
    resolver.setOrder(0); //顺序为第一位
    return resolver;
  }
}
2.2 controller入口

package *.controller.demo;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by X-J on 2017/6/1.
 */
@Controller
@RequestMapping(value = "/reports")
public class DemoReportController {

  private static final String REPORT_NAME = "reportName";
  private static final String FILE_FORMAT = "format";
  private static final String DATASOURCE = "datasource";

  private static final String queryStr = "你的sql语句";

  @Autowired
  private DataSource dbSource;

  /**
   * 直接使用模板文件中的sql语句得到报表
   * @param modelMap
   * @param reportName
   * @param format
   * @return
   */
  @GetMapping("/{reportName}")
  public ModelAndView getReportByParam(final ModelMap modelMap,
      @PathVariable(REPORT_NAME) final String reportName,
      @RequestParam(FILE_FORMAT) final String format) {
    modelMap.put(DATASOURCE, dbSource);
    modelMap.put(FILE_FORMAT, format);
    ModelAndView modelAndView = new ModelAndView(reportName, modelMap);
    return modelAndView;
  }
  
  /**
   * 自定义sql语句并生成报表,要注意一点,select ? from ? where XXX
   * where前面的语句需要写死,后面的where条件可以灵活传入
   * 因为模板中的field需要先定义好并对应select A from中的A,才能出数据
   * @param modelMap
   * @param reportName
   * @param format
   * @param modelAndView
   * @return
   */
  @GetMapping("/query/{reportName}")
  public ModelAndView getReportByParamAndQuery(final ModelMap modelMap,
      @PathVariable(REPORT_NAME) final String reportName,
      @RequestParam(FILE_FORMAT) final String format, ModelAndView modelAndView) {
    try {
      ResultSet resultSet = dbSource.getConnection().createStatement().executeQuery(queryStr);
      JRDataSource jrDataSource = new JRResultSetDataSource(resultSet);
      modelMap.put(DATASOURCE, jrDataSource);
      modelMap.put(FILE_FORMAT, format);
      modelAndView = new ModelAndView(reportName, modelMap);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return modelAndView;
  }
}

如上的controller提供了2中略微不同的报表生成方式,后面一个稍微可定制化一些。

访问

localhost:8080/reports/{模板名称}?format={PDF或者XLS}

localhost:8080/reports/query/{模板名称}?format={PDF或者XLS}

即可。


你可能感兴趣的:(jasper)