<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot CMD JasperReports 集成 > 原文: [http://zetcode.com/articles/jasperspringbootcmd/](http://zetcode.com/articles/jasperspringbootcmd/) 在本教程中,我們展示如何在 Spring Boot 框架中使用 JasperReports。 Spring Boot 在命令行中運行。 JasperReports 是一個 Java 開源報告庫。 它可以創建各種格式的報告,包括 PDF,HTML,XLS 或 CSV。 JasperReports 以一種簡單而靈活的方式創建了面向頁面的可打印文檔。 JdbcTemplate 是一個 Spring 庫,可以幫助程序員創建與關系數據庫和 JDBC 一起使用的應用。 它會處理許多繁瑣且容易出錯的底層細節,例如處理事務,清理資源以及正確處理異常。 `JdbcTemplate`在 Spring 的`spring-jdbc`模塊中提供。 Spring 是用于開發 Java 企業應用的流行 Java 應用框架。 它還有助于集成各種企業組件。 Spring Boot 使創建具有 Spring 動力的生產級應用和服務變得很容易,而對安裝的要求卻最低。 Apache Derby 是完全用 Java 實現的開源關系數據庫。 它占地面積小,易于部署和安裝。 它可以在嵌入式和客戶端/服務器模式下運行。 ## `CARS`表 在我們的應用中,我們使用下表: `cars.sql` ```java -- SQL for the CARS table CREATE TABLE CARS(ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), NAME VARCHAR(30), PRICE INT); INSERT INTO CARS(Name, Price) VALUES('Audi', 52642); INSERT INTO CARS(Name, Price) VALUES('Mercedes', 57127); INSERT INTO CARS(Name, Price) VALUES('Skoda', 9000); INSERT INTO CARS(Name, Price) VALUES('Volvo', 29000); INSERT INTO CARS(Name, Price) VALUES('Bentley', 350000); INSERT INTO CARS(Name, Price) VALUES('Citroen', 21000); INSERT INTO CARS(Name, Price) VALUES('Hummer', 41400); INSERT INTO CARS(Name, Price) VALUES('Volkswagen', 21600); ``` `cars.sql`文件創建`CARS`表。 ```java $ $DERBY_HOME/bin/ij ij version 10.11 ij> CONNECT 'jdbc:derby:testdb'; ij> RUN 'cars.sql'; ``` 一種選擇是使用`ij`工具從 SQL 腳本創建表。 請參考 [Apache Derby 教程](/db/apachederbytutorial/)以熟悉 Derby。 ```java $ $DERBY_HOME/bin/NetworkServerControl start & ``` 使用`NetworkServerControl`工具啟動 Derby 服務器。 ## 應用 以下 Spring Boot 應用從數據庫表加載數據,并使用 JasperReports 庫從中生成 PDF 報告。 ```java $ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── bean │ │ │ └── Car.java │ │ ├── conf │ │ │ └── AppConfig.java │ │ ├── MyRunner.java │ │ ├── report │ │ │ └── ReportGenerator.java │ │ └── service │ │ ├── CarService.java │ │ └── ICarService.java │ └── resources │ ├── application.yml │ └── report2.xml └── test └── java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>JasperSpringBootCmd</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version>10.13.1.1</version> </dependency> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <name>JasperSpringBootCmd</name> </project> ``` Maven `pom.xml`文件包含以下依賴項:`derbyclient`,`jasperreports`和`spring-boot-starter-jdbc`。 `jasperreports`依賴項是 JasperReports 庫; `spring-boot-starter-jdbc`包含`JdbcTemplate`庫。 `report2.xml` ```java <?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <field name="Id" class="java.lang.Long"> <fieldDescription><![CDATA[id]]></fieldDescription> </field> <field name="Name" class="java.lang.String"> <fieldDescription><![CDATA[name]]></fieldDescription> </field> <field name="Price" class="java.lang.Integer"> <fieldDescription><![CDATA[price]]></fieldDescription> </field> <detail> <band height="15"> <textField> <reportElement x="0" y="0" width="50" height="15" /> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Long"> <![CDATA[$F{Id}]]> </textFieldExpression> </textField> <textField> <reportElement x="150" y="0" width="100" height="15" /> <textElement textAlignment="Left" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"> <![CDATA[$F{Name}]]> </textFieldExpression> </textField> <textField> <reportElement x="200" y="0" width="100" height="15"/> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{Price}]]> </textFieldExpression> </textField> </band> </detail> </jasperReport> ``` 這是報告模板文件。 模板僅包含明細帶。 在詳細信息區域內,將對數據源提供的每個記錄重復每個元素。 ```java <field name="Id" class="java.lang.Long"> <fieldDescription><![CDATA[id]]></fieldDescription> </field> <field name="Name" class="java.lang.String"> <fieldDescription><![CDATA[name]]></fieldDescription> </field> <field name="Price" class="java.lang.Integer"> <fieldDescription><![CDATA[price]]></fieldDescription> </field> ``` 報告中有三個字段。 這些字段映射到數據源 bean 的元素。 ```java <textField> <reportElement x="0" y="0" width="50" height="15" /> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Long"> <![CDATA[$F{Id}]]> </textFieldExpression> </textField> ``` 文本字段是充滿動態數據的元素。 我們將一個字段中的值放在文本字段中。 我們使用`$F{}`語法引用該變量。 `Car.java` ```java package com.zetcode.bean; public class Car { private Long id; private String name; private int price; public Car() {} public Car(Long id, String name, int price) { this.id = id; this.name = name; this.price = price; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car{" + "id=" + id + ", name=" + name + ", price=" + price + '}'; } } ``` 這是`Car` bean 類。 它包含商品 ID,名稱和價格。 `application.yml` ```java datasource: url: jdbc:derby://localhost:1527/testdb username: app password: app driverClassName: org.apache.derby.jdbc.ClientDriver ``` `application.yml`是主要的 Spring Boot 配置文件。 它包含 Derby 數據源設置。 `AppConfig.java` ```java package com.zetcode.conf; import javax.sql.DataSource; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @Configuration public class AppConfig { @Bean @Primary @ConfigurationProperties(prefix = "datasource") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } } ``` `AppConfig`是 Java 配置類。 它從配置文件創建數據源 bean。 `ReportGenerator.java` ```java package com.zetcode.report; import com.zetcode.bean.Car; import java.util.HashMap; import java.util.List; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; public class ReportGenerator { public void generatePdfReport(List<Car> cars) throws JRException { String report = "src/main/resources/report2.xml"; JasperReport jreport = JasperCompileManager.compileReport(report); JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(cars); HashMap params = new HashMap(); JasperPrint jprint = JasperFillManager.fillReport(jreport, params, ds); JasperExportManager.exportReportToPdfFile(jprint, "src/main/resources/report2.pdf"); } } ``` `ReportGenerator`根據提供的數據創建 PDF 報告。 ```java JasperReport jreport = JasperCompileManager.compileReport(report); ``` 使用`JasperCompileManager.compileReport()`方法,我們將 XML 報告模板編譯為中介`JasperReport`。 ```java JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(cars); ``` 從提供的汽車對象列表中,我們創建一個`JRBeanCollectionDataSource`。 Bean 的屬性將被映射到已編譯報表對象的字段。 ```java JasperPrint jprint = JasperFillManager.fillReport(jreport, params, ds); ``` 已編譯的 Jasper 對象使用`JasperFillManager.fillReport()`方法填充了數據。 產生一個`JasperPrint`對象。 ```java JasperExportManager.exportReportToPdfFile(jprint, "src/main/resources/report2.pdf"); ``` `JasperExportManager.exportReportToPdfFile()`方法將`JasperPrint`對象轉換為 PDF 文件。 `ICarService.java` ```java package com.zetcode.service; import com.zetcode.bean.Car; import java.util.List; public interface ICarService { public List<Car> findAll(); } ``` `ICarService`提供了一種從數據源獲取所有汽車的契約方法。 `CarService.java` ```java package com.zetcode.service; import com.zetcode.bean.Car; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; @Service public class CarService implements ICarService { @Autowired private JdbcTemplate jtm; @Override public List<Car> findAll() { String sql = "SELECT * FROM Cars"; List<Car> cars = jtm.query(sql, new BeanPropertyRowMapper(Car.class)); return cars; } } ``` `CarService`包含`findAll()`方法的實現。 我們借助`JdbcTemplate`從`CARS`表中檢索所有汽車。 `MyRunner.java` ```java package com.zetcode; import com.zetcode.bean.Car; import com.zetcode.report.ReportGenerator; import com.zetcode.service.ICarService; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyRunner implements CommandLineRunner { private static final Logger LOG = Logger.getLogger(MyRunner.class.getName()); @Autowired private ICarService carService; @Override public void run(String... args) throws Exception { List<Car> cars = carService.findAll(); ReportGenerator rg = new ReportGenerator(); rg.generatePdfReport(cars); LOG.log(Level.INFO, "Generating PDF report"); } } ``` `MyRunner`是 Spring Boot 應用的命令行運行程序。 ```java @Autowired private ICarService carService; ``` 我們注入服務對象。 ```java List<Car> cars = carService.findAll(); ``` 我們使用服務對象獲取所有汽車。 ```java ReportGenerator rg = new ReportGenerator(); rg.generatePdfReport(cars); ``` `ReportGenerator`用于創建 PDF 報告。 該報告包含檢索到的汽車。 `Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`設置 Spring Boot 應用。 在本教程中,我們使用 JasperReports 創建了 PDF 報告。 該應用使用了 Spring Boot 框架,并在命令行中運行。 您可能也對這些相關教程感興趣: [Spring Boot JasperReports Web 集成](/articles/jasperspringbootweb/),[使用 JasperReports API 創建報告](/articles/jasperprog/),[使用 JasperReports 從 CSV 創建報告](/articles/jaspercsv/)和 [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看