将Java与Java代码分离 [英] Separating SQL from the Java Code

查看:149
本文介绍了将Java与Java代码分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我必须连接到数据库时遇到的问题;如何从正常的java代码中分离SQL?我通常为数据库连接使用单独的类,但是,当您在每个数据库中有多个数据库和多个表时,总是难以做到这一点100%

This is a problem that I always encounter when I have to connect to a data base; how to separate SQL from the normal java code? I usually use a separate class for the Database connections, however, when you have several databases and several tables in each database, it is always difficult to do this 100%

一个例子,如果我们想把所有的java SQL放在一个名为DBConnector.java的类中,那么我们如何对不同的Insert,Deletions,Data Retrieval等进行一般的代码?我认为理想情况是所有的SQL语句应该在同一个类中,并且应该与数据库应用程序范围内的不同类型的相同操作兼容,从而提供与其余代码的逻辑分离。

As an example, if we want to put all the java SQL in a class named DBConnector.java, how do we code generically for different Insertions, Deletions, Data Retrieval, etc? What I'm thinking as the ideal case is that all the SQL statements should be in the same class and should be compatible with different flavors of same operation within the scope of the database application, thus providing a logical separation from rest of the code.

public void insertData (String db, String table, <Whatever Fields to be Inserted>)
{
  //generic SQL INSERT statement and execution 
}

public ResultSet retrieveData (String db, String table, <Whatever Fields Relevant>) 
{
  //generic retrieval of data
}

有什么方法可以实现吗?

Is there any way to accomplish this? Or should we just add functionality for different flavors of Inserting, Querying, etc?

谢谢!

推荐答案

如果你想要一个声音体系结构,你至少需要几层来分离问题。

If you want a sound architecture you'll want at least few layers to seperate concerns.

首先,类(大多数时候,你需要一个数据库中的每个表)。自己编写它们,或者使用ORM(例如EclipseLink,Hibernate)自动生成它们。这些应该是POJO(普通Java对象),这意味着它们是具有属性的简单对象(例如 Name String, Id ,类型为integer等...)。

First of all, start off with model classes (most of the time, you'll want one for every table in your database). Write them yourself, or have them generated automatically by using an ORM (e.g. EclipseLink, Hibernate). These should be POJO (Plain Old Java Objects), which means they are simple objects with properties (e.g. Name of type String, Id of type integer etc...). Your model objects should be carriers of data and nothing more (certainly no logic or processing).

然后,为所有模型类创建DAO(数据访问对象)你可以构建一个GenericDao类继承自己,如果你想)。在这里,您将通过将模型对象作为参数的方法提供CRUD操作(插入,更新,删除)。这是数据库后台特定的,虽然你可以插入一个数据库不可知的DAO层如果你想要的。

Then, make DAO (Data Access Objects) for all of the model classes (you can build a GenericDao class to inherit from if you want to). Here you will provide CRUD operations (insert, update, delete) via methods which take the model objects as arguments. This is database-backend specific, although you can insert a database-agnostic DAO-layer if you want.

第三,有一个服务每个逻辑组类(这是所有前端和控制器代码应该与所有想要的功能交谈)的 manager 层。一个典型的方法可以被称为 registerCustomer(...)(可能使用不同的DAO类)。或 findCustomerByName()等。

Third, have a service or manager layer per logical group of classes (this is the layer all frontend and controller code should be talking to for all wanted functionality). A typical method could be called registerCustomer(...) (which may use different DAO classes). Or findCustomerByName() etc.

以这种方式构造应用程序称为 MVC (Model - View - Controller),所以如果你想要更多的信息,这是google的术语。

Structuring your application in this way is called MVC (Model - View - Controller), so that's the term to google for if you want more information.

这样,你通常不会SQL查询任何高于DAO层,这意味着您的应用程序是a)可维护的和b)稍后更容易更改后端。

This way, you typically will not have SQL queries any higher then the DAO layer, which means your application is a) maintainable and b) it's easier to change backends later.

这篇关于将Java与Java代码分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆