码迷,mamicode.com
首页 > Web开发 > 详细

MVC设计流程(粗略)实践篇

时间:2016-05-16 01:41:19      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:


以下只是简要说明几个核心类
数据库连接控制器
ConnectionManager
public class ConnectionManager {

    private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();

    // 取得当前本地线程局部变量的副本--- 获取连接
    public static Connection getConnection() {
        Connection conn = connectionHolder.get();
        if (conn == null) {
            conn = new DBUtil().getConn();
            connectionHolder.set(conn);
        }
        return conn;
    }

    /**
     * 关闭数据库连接
     */
    public static void closeConnection() {
        Connection conn = connectionHolder.get();
        if (conn != null) {
            try {
                conn.close();
                connectionHolder.remove();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 手工开启事务
     * 
     * @param conn
     */
    public static void beginTransaction(Connection conn) {
        try {
            if (conn != null) {
                if (conn.getAutoCommit()) {
                    conn.setAutoCommit(false);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置事务的提交
     * 
     * @param conn
     */
    public static void commitTransaction(Connection conn) {
        try {
            if (conn != null) {
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置事务的回滚
     * 
     * @param conn
     */
    public static void rollbackTransaction(Connection conn) {
        try {
            if (conn != null) {
                if (!conn.getAutoCommit()) {
                    conn.rollback();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复原始的事务提交
     * 
     * @param conn
     */
    public static void recoverTransaction(Connection conn) {
        try {
            if (conn != null) {
                if (conn.getAutoCommit()) {
                    conn.setAutoCommit(false);
                } else {
                    conn.setAutoCommit(true);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

数据访问层抽象类:AbstractDAO
public abstract class AbstractDAO {

    protected Connection conn = null;
    protected PreparedStatement pstmt = null;

    public AbstractDAO(){
        this.conn = ConnectionManager.getConnection();
    }


}

数据访问层工厂: DAOFactory

public class DAOFactory {

    public static IMemberDAO getIMemberDAOInstance() throws Exception {
        String baseName = ResourceUtil.getResourceBundle("Database", "db.name");
        String className = ResourceUtil.getResourceBundle(baseName,
                "member.class");
        return (IMemberDAO) Class.forName(className).newInstance();
    }

业务层代理类:ServiceProxy

public class ServiceProxy implements InvocationHandler, Serializable {

    private Object obj; // 真实主题

    public Object bind(Object obj) {
        this.obj = obj; // 实现的MemberServiceImpl
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
                .getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        Object value = null;
        // 绑定对象的时候进行数据库连接的初始化验证    
        if (method.getName().contains("login")
                || method.getName().contains("add")
                || method.getName().contains("edit")
                || method.getName().contains("rm")) { // 如果现在是更新操作
            Connection conn = ConnectionManager.getConnection(); 
            try {
                ConnectionManager.beginTransaction(conn);
                value = method.invoke(this.obj, args);
                ConnectionManager.commitTransaction(conn);
            } catch (Exception e) {
                ConnectionManager.rollbackTransaction(conn);
            } finally {
                ConnectionManager.recoverTransaction(conn);
                ConnectionManager.closeConnection();
            }
        } else {
            try {
                value = method.invoke(this.obj, args);
             }catch (Exception e) {
                throw e;
            } finally {
                ConnectionManager.closeConnection();
            }
        }
        return value;
    }

}

业务层工厂类:ServiceFactory 

public class ServiceFactory {

    public static IMemberService getIMemberServiceInstance() {
        return (IMemberService) new ServiceProxy()
                .bind(new MemberServiceImpl());
    }
}

 

MVC设计流程(粗略)实践篇

标签:

原文地址:http://www.cnblogs.com/denied/p/5496680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!