jasperreport JAVA API 를 spring boot 에 통합 합 니 다.

6406 단어 jasper
이전 글http://blog.csdn.net/pspr2/article/details/72843356spring MVC 에 어떻게 통합 하 는 지 에 대해 서 말씀 드 렸 는데, 이번 에는 오픈 소스 프레임 워 크 spring boot 에 통합 하 는 방법 을 소개 합 니 다.
1. 자체 프로젝트 를 만 들 고 데이터 베이스 와 의 연결 과 웹 프로젝트 를 구축한다.
다음 설정 과 유사 합 니 다:
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 컨트롤 러 입구
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;
  }
}

위의 contrller 는 2 에서 약간 다른 보고서 생 성 방식 을 제공 하고 뒤의 하 나 는 약간 맞 춤 형 으로 만 들 수 있 습 니 다.
방문 하 다.
localhost: 8080 / reports / {템 플 릿 이름}?format = {PDF 또는 XLS}
localhost: 8080 / reports / query / {템 플 릿 이름}?format = {PDF 또는 XLS}
됐다.

좋은 웹페이지 즐겨찾기