Java 如何在spring boot ireport中从applicationon.properties获取MySQL连接

Java 如何在spring boot ireport中从applicationon.properties获取MySQL连接,java,spring,jdbc,datasource,Java,Spring,Jdbc,Datasource,目前我正在使用此代码生成ireport。但是我需要直接从application.properties获取数据源,因为我不想再次为sql连接使用用户名和密码 File file = ResourceUtils.getFile("classpath:report/collectorInvoice.jrxml"); InputStream input = new FileInputStream(file); // Compile the Jasper

目前我正在使用此代码生成ireport。但是我需要直接从application.properties获取数据源,因为我不想再次为sql连接使用用户名和密码

        File file = ResourceUtils.getFile("classpath:report/collectorInvoice.jrxml");
        InputStream input = new FileInputStream(file);

        // Compile the Jasper report from .jrxml to .japser
        JasperReport jasperReport = JasperCompileManager.compileReport(input);

        conn = DriverManager.getConnection("jdbc:mysql://localhost/divron?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","aha","123");

        // Get the parameter
        Map<String, Object> parameters = new HashMap<>();

        parameters.put("invoiceId",id);
        try (Connection connection = dataSource.getConnection()) {

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);

            // Export the report to a PDF file
            JasperExportManager.exportReportToPdfFile(jasperPrint, "D://" +"collectorInvoice -"+ id + ".pdf");

        }
        return new ResponseEntity<Object>(null, HttpStatus.OK);

因此,请帮助我从sql连接获取全部数据,而无需再次使用sql连接的用户名和密码。

如果spring boot已经创建了数据源,您可以在Bean中自动连接它(或使用另一种注入方式)并使用来自数据源的连接

@Autowired
DataSource dataSource;
....
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());

谢谢。。我已经添加了@Autowired@Qualifier(“数据源”)私有数据源数据源;而且效果很好
@Autowired
DataSource dataSource;
....
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());