我有一个工作正常的代码。重要的部分如下:
我的模型类:
package biz.tugay.sakila.model;
/* User: koray@tugay.biz Date: 25/06/15 Time: 12:48 */
public class Actor {
private long id;
private String firstName;
private String lastName;
// Getters, setters...
}
我的 dao 类:
package biz.tugay.sakila.dao;
/* User: koray@tugay.biz Date: 25/06/15 Time: 12:12 */
import biz.tugay.sakila.model.Actor;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ActorDao {
protected static final Connection connection = DBConnector.getConnection();
public List<Actor> getAllActors() throws SQLException {
List<Actor> allActors = new ArrayList<Actor>();
Statement stmt = connection.createStatement();
String sql = "SELECT * FROM Actor";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
// You get the idea... Setters again..
allActors.add(actor);
}
rs.close();
stmt.close();
return allActors;
}
}
和 DBConnector
package biz.tugay.sakila.dao;
/* User: koray@tugay.biz Date: 25/06/15 Time: 12:35 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnector {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/sakila";
static final String USER = "root";
static final String PASS = "";
private static Connection connection = null;
public static final Connection getConnection() {
if (connection != null) {
return connection;
} else {
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(DB_URL, USER, PASS);
return connection;
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}
throw new UnsupportedOperationException();
}
}
}
我的 Servlet 类:
package biz.tugay.sakila.servlet;
/* User: koray@tugay.biz Date: 26/06/15 Time: 14:31 */
import biz.tugay.sakila.dao.ActorDao;
import biz.tugay.sakila.model.Actor;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet(urlPatterns = "/actors")
public class ActorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ActorDao actorDao = new ActorDao();
List<Actor> allActors = null;
try {
allActors = actorDao.getAllActors();
req.setAttribute("allActors",allActors);
req.getRequestDispatcher("/actors.jsp").forward(req, resp);
} catch (SQLException e) {
}
}
}
/actors.jsp 会向用户显示HTML表格。
我自己使用MySQL提供的 sakila示例数据库进行了此练习。
我的问题是,如果不使用Spring或Struts等任何框架,我怎样才能实现更好的分离?例如,目前ActorServlet具体依赖于ActorDao,我可以解决这个问题,如果是这样的话怎么样? ActorDao也很大程度上依赖于DBConnector。例如,我希望能够创建一个NoSQL连接器并使用它,但目前我猜不到?
答案 0 :(得分:2)
第一步是抽象出一些接口。例如,将ActorDao
设为接口,将实现移至ActorDaoImpl
或其他任何内容。创建一个ActorDaoFactory
,向您提供一个ActorDao
ActorDaoImpl
,这是// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our DAO
ActorDao ad = (ActorDao)envCtx.lookup("dao/actor");
,但servlet并不需要知道。
第二步更复杂......如果你只想 使用Tomcat,那么注入等就会出来,但你可以配置Tomcat来创建这些新接口并将它们放在JNDI中。这个过程可能太复杂了,无法在这里提出答案,但Tomcat documentation on JNDI非常好。该过程基本上涉及创建一个工厂,就像我上面提到的那样,然后让Tomcat通过配置调用该工厂。
一旦你这样做,从JNDI查找它们就像
一样简单{{1}}祝你好运!