使用 JPA 和 JasperReports 生成复杂的报告

发布于 2024-09-25 18:21:22 字数 385 浏览 2 评论 0原文

我一直在使用 jasperreports 和 JPA 进行报告,所有这些报告都只是“目录”,其中唯一的信息是一个实体,有时是一个实体及其相关实体。

现在我必须设计包含非常复杂的信息(分组、汇总、不属于实体的字段等)的报告,这对于我的实体来说是不可能的(是的,我知道我可以修改我的设计,但我认为这不好对于我的应用程序)。

我一直在考虑不同的替代方案,例如传递 jdbc 连接,即使这意味着在我的类中创建方法以将此对象传递给视图(生成报告)。

另一种方法是创建另一个上下文(应用程序),以自己的方式(例如 BI)访问数据库,因此该应用程序将与我的原始应用程序分开。

希望有人可以评论或给我一个更好的方法来实现这一目标。

顺便说一下,我的框架是spring。

谢谢。

I have been reporting with jasperreports and JPA, all this reports are just "catalogs" in which the only information is an entity or some times an entity with it's related entities.

Now i have to design reports with very complex information (grouping, summarization, fields not part of an entity and so on) which is not possible with my entities (Yes i know that i can modify my design but i do't think it's good for my app).

I've been thinking in different alternatives, like passing a jdbc connection even when this implies to create methods in my classes to pass this object to the view (Which generates the report).

Another one is to create another context (application) that accesses the data base in is own way (something like BI), so this application would be separated from my original application.

Hope some one can comment or give me a better way to achieve this.

By the way, my framework it's spring.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-10-02 18:21:22

我建议在生成复杂报告时使用 JDBC 连接选项,即使只是为了通过直接编写 SQL 获得灵活性。但是在视图层中侵入 JDBC 连接听起来不太好,所以在不了解 Spring 的情况下,我说将实际的报告生成移至服务层,让它将 JasperPrint 对象(可序列化)返回到视图层,其中它可以被呈现。

这是一个普通的 RMI 示例,与我的做法几乎一样。我想这可以转化为某种 Spring bean。

public interface ReportService extends Remote {
  public JasperPrint fillReport(final JasperReport report, final Map reportParameters)throws RemoteException, JRException;
}

public class ReportServiceImpl implements ReportService {

  final Connection connection;

  public ReportServiceImpl(final Connection connection) {
    this.connection = connection;
  }

  public JasperPrint fillReport(final JasperReport report, final Map reportParameters) throws RemoteException, JRException {
    return JasperFillManager.fillReport(report, reportParameters, connection);
  }
}

我想到的一项修改(没有双关语)是让 fillReport 方法采用报告名称或路径作为参数,而不是实际的 JasperReport 对象,并让服务层加载它(并缓存它,因为加载报告是相对昂贵的操作)。

如果您希望我充实这个示例的某些部分,请告诉我,但我必须警告您,我是一名桌面/Swing 程序员,在 Web 前端方面只有基本的经验。

I'd recommend using the JDBC connection option when generating a complex report, if only for the flexibility gained by writing the SQL directly. But having a JDBC connection trespassing in your view layer does not sound good, so without knowing much about Spring, I say move the actual report generation down to the service layer having it return the JasperPrint object (which is serializable) to the view layer where it can be presented.

Here is a plain-vanilla RMI example, pretty much the way I do it. I suppose this can be translated into a Spring bean of some sort.

public interface ReportService extends Remote {
  public JasperPrint fillReport(final JasperReport report, final Map reportParameters)throws RemoteException, JRException;
}

public class ReportServiceImpl implements ReportService {

  final Connection connection;

  public ReportServiceImpl(final Connection connection) {
    this.connection = connection;
  }

  public JasperPrint fillReport(final JasperReport report, final Map reportParameters) throws RemoteException, JRException {
    return JasperFillManager.fillReport(report, reportParameters, connection);
  }
}

One modification that springs to mind, no pun intended, would be to have the fillReport method take a report name or path as an argument instead of the actual JasperReport object and have the service layer load it (and cache it, since loading the report is a relatively expensive operation).

Let me know if you want me to flesh out some parts of this example, I must warn you though that I am a Desktop/Swing programmer with only a rudimentary experience in web front-ends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文