MVC design Process (rough) practice

Source: Internet
Author: User


The following is a brief description of several core classes
Database Connection Controller
 Public classConnectionManager {Private StaticThreadlocal<connection> Connectionholder =NewThreadlocal<connection>(); //gets a copy of the current local thread local variable---Get the connection     Public StaticConnection getconnection () {Connection conn=Connectionholder.get (); if(conn = =NULL) {Conn=Newdbutil (). Getconn ();        Connectionholder.set (conn); }        returnConn; }    /*** Close Database connection*/     Public Static voidCloseConnection () {Connection conn=Connectionholder.get (); if(Conn! =NULL) {            Try{conn.close ();            Connectionholder.remove (); } Catch(SQLException e) {e.printstacktrace (); }        }    }    /*** Open Transaction manually * *@paramConn*/     Public Static voidBeginTransaction (Connection conn) {Try {            if(Conn! =NULL) {                if(Conn.getautocommit ()) {Conn.setautocommit (false); }            }        } Catch(SQLException e) {e.printstacktrace (); }    }    /*** SET TRANSACTION submission * *@paramConn*/     Public Static voidCommitTransaction (Connection conn) {Try {            if(Conn! =NULL) {                if(!Conn.getautocommit ())                {Conn.commit (); }            }        } Catch(Exception e) {e.printstacktrace (); }    }    /*** SET TRANSACTION rollback * *@paramConn*/     Public Static voidRollbackTransaction (Connection conn) {Try {            if(Conn! =NULL) {                if(!Conn.getautocommit ())                {Conn.rollback (); }            }        } Catch(Exception e) {e.printstacktrace (); }    }    /*** Restore the original transaction commit * *@paramConn*/     Public Static voidrecovertransaction (Connection conn) {Try {            if(Conn! =NULL) {                if(Conn.getautocommit ()) {Conn.setautocommit (false); } Else{conn.setautocommit (true); }            }        } Catch(Exception e) {e.printstacktrace (); }    }}

 Public Abstract class Abstractdao {    protectednull;     protected NULL ;      Public Abstractdao () {        this. conn = connectionmanager.getconnection ();    }} 

Data Access Layer Factory: Daofactory

 Public class daofactory {    publicstaticthrows  Exception {        = Resourceutil.getresourcebundle ("Database", "Db.name");         = Resourceutil.getresourcebundle (baseName,                "Member.class");         return (Imemberdao) class.forname (className). newinstance ();    

Business layer proxy class: Serviceproxy

 Public classServiceproxyImplementsInvocationhandler, Serializable {PrivateObject obj;//Real Themes     Publicobject bind (Object obj) { This. obj = obj;//implementation of the Memberserviceimpl        returnproxy.newproxyinstance (Obj.getclass (). getClassLoader (), obj. GetClass (). Getinterfaces (), This); } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object value=NULL; //Initialize validation of database connections when binding objects        if(Method.getname (). Contains ("Login")                || Method.getname (). Contains ("Add")                || Method.getname (). Contains ("Edit")                || Method.getname (). Contains ("rm")) {//If the update operation is nowConnection 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) {Throwe; } finally{connectionmanager.closeconnection (); }        }        returnvalue; }}

Business Layer Factory class:servicefactory

 Public class servicefactory {    publicstatic  imemberservice getimemberserviceinstance () {         returnnew  serviceproxy ()                . bind (new  Memberserviceimpl ());}    } 

MVC design Process (rough) practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.