2

我有一个工作正常的代码。重要部分如下:

我的模型课:

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... 
}

我的课:

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 连接器并使用它,但目前我猜不到?

4

1 回答 1

2

第一步是抽象出一些接口。例如,制作ActorDao一个接口,将实现移至ActorDaoImpl或其他。创建一个传递给ActorDaoFactory您的 an ActorDao,在幕后,它是 an ActorDaoImpl,但 servlet 不需要知道这一点。

第二步比较复杂……如果你只想使用Tomcat,那么注入之类的就不行了,但是你可以配置Tomcat来创建这些新的接口,并把它们放在JNDI中。这个过程可能太复杂了,无法在这里给出答案,但是关于 JNDI 的 Tomcat 文档非常好。这个过程基本上包括创建一个工厂,就像我上面提倡的那样,然后让 Tomcat 通过配置调用该工厂。

一旦你这样做了,从 JNDI 中查找它们就像

// 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");

祝你好运!

于 2015-06-26T13:00:48.017 回答