The framework is a little tricky. Although struts2, spring, hibernate, ibatis and so on are some excellent frameworks. However, it seems a waste of time to find that many framework functions are useless! So I thought about whether I had learned the idea of using frameworks, and then used traditional frameworks for development.
These two days I spent two hours on the road taking the bus and thought about how to develop the dao layer. Summary:
1. Use the template mode to develop a common jdbctemplate, and simply write the jdbc template class
Java code
Public class jdbctemplate <t> {/*** list of table objects */@ suppresswarnings ("unchecked") public list <t> query (string SQL, object [] args, basedao dao) {connection conn = null; preparedstatement ps = null; resultset rs = null; list <t> list = new arraylist <t> (); try {conn = dbutils. getconnectionbyc3p0 (); ps = conn. preparestatement (SQL); if (args! = Null) {for (int I = 0; I <args. length; I ++) {ps. setobject (I + 1, args [I]) ;}} rs = ps.exe cutequery (); while (rs. next () {t obj = dao. rowmapper (rs); list. add (obj) ;}} catch (exception e) {e. printstacktrace ();} finally {try {if (conn. getautocommit () {dbutils. release (rs, ps, conn);} else {dbutils. release (rs, ps, null) ;}} catch (exception e2) {e2.printstacktrace () ;}} return list ;}/ *** update table object */p Ublic void update (string SQL, object [] args) {connection conn = null; preparedstatement ps = null; try {conn = dbutils. getconnectionbyc3p0 (); ps = conn. preparestatement (SQL); if (args! = Null) {for (int I = 0; I <args. length; I ++) {ps. setobject (I + 1, args [I]) ;}} ps.exe cuteupdate ();} catch (exception e) {e. printstacktrace ();} finally {try {if (conn. getautocommit () {dbutils. release (null, ps, conn);} else {dbutils. release (null, ps, null) ;}} catch (exception e2) {e2.printstacktrace ();}}}}
In this template class, database connections are not directly closed for subsequent transaction management. Of course, the template class cannot be just these two methods. For details, refer to spring's jdbctemplate for template class development.
Basedao is an interface that maps rs to bean. The Code is as follows:
Java code
Public interface basedao <t> {public t rowmapper (resultset rs );}
2. Other dao layer classes are developed using jdbctemplate. The following is a simple example:
Java code
Public list <searchupdate> findbysql (string SQL, object [] objs) {return jt. query (SQL, objs, new basedao <user> () {public user rowmapper (resultset rs) {user = new user (); try {user. setname (rs. getstring ("name"); user. setid (rs. getint ("id");} catch (exception e) {e. printstacktrace () ;}return user ;}});}
3. Use proxy mode to process transactions. The following is the code about the business proxy class, in which the transaction is enabled, committed or rolled back.
Java code
Public class transactionproxy implements invocationhandler {private object targetobject; public object bind (object targetobject) {this.tar getobject = targetobject; return proxy. newproxyinstance (targetobject. getclass (). getclassloader (), targetobject. getclass (). getinterfaces (), this);} public object invoke (object proxy, method, object [] args) throws throwable {transactionmanager tm = dbutils. gettransactionmanager (); tm. begintransaction (); object result = null; try {result = method. invoke (targetobject, args); tm. committransaction ();} catch (exception e) {tm. rollbacktransaction () ;}return result ;}}
In this way, the premise is that the proxy object must be implemented by the corresponding interface. If the proxy object does not have an interface, you can use the third-party package cglib to implement the object proxy.
The above is just a personal experience, as a small record of your own. If there are any mistakes or omissions, I hope to raise them. The complete code is included in the attachment, including a database connection pool package and cglib package.
Author: "oracle experts, haha"