如何处理用 Java 编写的网站(更具体地说是 Wicket)的数据库?
我是使用 Java 进行网站开发的新手,但我已经开始使用 Wicket 并制作了一个小网站。我想扩展我已经制作的内容(带有表单、标签和链接的网站)并实现数据库连接。
我查看了几个示例,例如 Mystic Paste,我发现他们正在使用 Hibernate 和 Spring。我以前从未接触过 Hibernate 或 Spring,说实话,注释的大量使用让我有点害怕,因为我以前没有真正使用过它们,除了抑制警告和覆盖之外。
此时,我在初始化时在 WebApplication 类中设置了一个 Connection 对象。然后,每当我需要执行查询时,我都会检索该连接对象。我不知道这对于生产 Web 应用程序来说是否是一个糟糕的方法。
非常感谢所有帮助。
I'm new to website development using Java but I've got started with Wicket and make a little website. I'd like to expand on what I've already made (a website with a form, labels and links) and implement database connectivity.
I've looked at a couple of examples, in example Mystic Paste, and I see that they're using Hibernate and Spring. I've never touched Hibernate or Spring before and to be honest the heavy use of annotations scare me a little bit as I haven't really made use of them before, with the exception of supressing warnings and overriding.
At this point I have one Connection object which I set up in the WebApplication class upon initialization. I then retrieve this connection object whenever I need to perform queries. I don't know if this is a bad approach or not for a production web application.
All help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Wicket、Spring 和 Hibernate 几乎是 Wicket 应用程序的标准堆栈。或者更确切地说,任何 Web 框架,Spring 和 Hibernate 几乎都是任何 Web 框架的标准堆栈。
对于 Wicket,在组件内部使用
@SpringBean
注入对象是一个非常好的功能。此外, OpenSessionInViewFilter 为您管理 Hibernate 会话(而 Hibernate 本身负责连接)。因此,我真的建议您研究 Spring 和 Hibernate - 两者都不需要注释,但它们大多数时候比配置文件(通常是 XML)更容易使用。
如果您仍然不想使用 Spring 或 Hibernate,我建议您查看 OpenSessionInViewFilter 并自己创建类似的内容:为每个请求创建一个连接,在一个请求期间使用它,最后关闭它。由于这不会很好地执行,您可能宁愿选择从池中获取连接,并在请求结束时将连接返回到池中。但是,您可以将 bean 注入到组件中,而不是编写此代码;)
Wicket, Spring and Hibernate is pretty much the standard stack for Wicket applications. Or let's rather say that any web framework, Spring and Hibernate is pretty much the standard stack for any web framework.
Regarding Wicket, injecting objects using
@SpringBean
inside components is an extremely nice to have feature. Additionally, the OpenSessionInViewFilter manages Hibernate sessions for you (while Hibernate itself takes care of connections).Therefore, I'd really suggest you look into Spring and Hibernate - both of which don't require annotations, but they are most of the time easier to use than configuration files (typically XML).
If you still don't want to use Spring or Hibernate, I'd suggest you look at the OpenSessionInViewFilter and create something similar yourself: create a connection for each request, use it during one request, close it at the end. As this won't perform very well, you might rather choose to get connections from a pool to which you return it at the end of a request. But instead of writing this code, you could already be injecting beans into your components ;)
不好的方法,因为 Connection 对象旨在供单个线程使用,而 Web 应用程序请求是从线程池中处理的。
在最好的情况下,您将遭受严重的性能问题,因为连接对象不会同时执行查询。
解决这个问题的方法是使用连接池。
Bad approach because a Connection object is intended for use by a single thread and web application requests are processed from a pool of thread.
In the best case you'll suffer for big performance problems cause the connection object won't execute queries concurrently.
A solution to this problem is the usage of a connection pool.
如果您有时间,您可以深入了解 Apache Cayenne,它比 Hibernate 轻得多,并且可以与依赖注入结合使用Google Guice,同样非常轻量级。 Wicket 有 wicket-guice 子项目,它在 wicket 组件中提供 DI,很像 Spring Context。
恕我直言,这是一个公平的选择,到目前为止效果很好。
If you have time you can dig around Apache Cayenne, it's far more light than Hibernate and for dependency injection combine with Google Guice, again very lightweight. Wicket has wicket-guice subproject, which provides DI in wicket components, much like Spring Context.
IMHO it's fair alternative, works very nice so far.