在 2 层和 3 层应用程序中使用来自表示层的 JDBC 连接
我正在编写一个将在不同应用程序(2 层和 3 层)中使用的模块。 我需要连接到数据库。因此,我使该模块在与 2 层应用程序一起使用时需要 java.sql.Connection 对象作为参数。那里没有问题。
我面临的问题是,在 3 层应用程序的情况下,将从表示层使用该模块,因此,我不想为模块提供用于数据库访问的连接对象。
你建议我用什么来解决这个问题?
I am writing a module that will be used in different applications (2-tiers and 3-tiers).
I'll need to connect to a DB. so, I made the module requires a java.sql.Connection object as a parameter when used with a 2-tier application. there's no problem there.
the problem i'm facing is that in case of a 3-tier application, the module will be used from the Presentation tier and as such, I don't want to give the module a Connection object for DB access.
What do you suggest I use to solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Spring 中,您定义一个应用程序上下文,在大多数情况下它只是一个 XML 文件,并且包含许多称为 beans 的应用程序对象。
myDbConnection 是在同一应用程序上下文中定义的 bean,其中包含所有连接详细信息。
然后在表示层中,您只需使用 applicationContext.getBean("myPersistance") 即可获得使用所有依赖项初始化的持久性管理器的实例。您可以为不同的部署选项提供不同的应用程序上下文。
In Spring you define an application context which is in most cases just an XML file, and that contains a number of appliction object called beans.
myDbConnection is a bean defined in the same application context and that contains all connection details.
Then in your presentation layer you just use applicationContext.getBean("myPersistance") and you get an instance of your persistance manager initialised with all dependecies. And you can have different application contexts for different deployment options.
您将 Connection 传递给持久性对象的本能是一件好事,您不愿意让表示层负责获取它也是一件好事。
我建议阅读 MVC 模式并研究 Spring。 Spring 习惯用法将向您展示对应用程序进行分层的正确方法。它也将有助于解决您的依赖性问题。
更新:
您必须阅读有关 Spring 的更多内容。
使用 Spring 常见的 web->service->persistence 接口分层习惯将有助于正确地对应用程序进行分层。
Spring 具有依赖注入来帮助您管理依赖项。
Spring 的框架类可帮助您获取和管理数据库连接等内容。
我认为重写别人已经写得更好的东西对你来说没有什么意义。我建议您首先浏览 Spring MVC 逐步看看您是否同意。
如果您不想学习 Spring,我建议您至少看看他们为 DataSource 和 JDBC 连接编写的类。也许你可以通过观察他们的方式来改进你的工作方式。
Your instinct to pass a Connection to the persistence object is a good one, as is your reluctance to make the presentation tier responsible for acquiring it.
I'd recommend reading about the MVC pattern and looking into Spring. The Spring idiom will show you the proper way to layer an application. It'll help with your dependency issues as well.
UPDATE:
You'll have to read more about Spring.
Using Spring's common idiom of web->service->persistence interface layering will help layer your app properly.
Spring has dependency injection to help you manage your dependencies.
Spring's framework classes help you with acquiring and managing things like database connections.
I'm arguing that it makes little sense for you to rewrite what someone else has already written better. I'm suggesting that you start by browsing through Spring MVC Step By Step to see if you agree.
If you'd rather not learn Spring, I'd advise you to at least look at the classes they wrote for DataSource and JDBC connections. Maybe you can improve the way you're doing yours by looking at theirs.