公司經常更換框架,好不容易寫好的業務模型經常要推倒重來。
為此考慮將業務模型獨立出來,不受框架的影響。
具體考慮如下:
1.接口定義
DBConnection.java
public interface DBConnection {
? PreparedStatement prepareStatement(String aSql);
?....
? void free();
? void commit();
? void rollback();
}
DBContext.java
public interface DBContext {
? DBConnection newDBConnection();
}
2.模型實現
public class ActualFlow {
? DBContext dbContext=null;
? public ActualFlow() {
? }
? public void init(DBContext aContext){
??? dbContext=aContext;
? }
? public int calculate(String aBranchId,String aYear,String aMonth,String aUserId){
??? int iRtn=-1
??? PreparedStatement objState=null;
??? ResultSet objSet=null;
??? DBConnection objCon=null;
??? try {
????? objCon=dbContext.newDBConnection();
????? objState=objCon.prepareStatement("具體的sql語句");
????? //業務模型操作......

??? }
??? catch (Exception ex) {
????? ex.printStackTrace();
????? iRtn=-1;
??? }finally{
????? if (objSet!=null) {
??????? try {
????????? objSet.close();//關閉結果集合
??????? }
??????? catch (Exception ex) {
????????? ex.printStackTrace();
??????? }
????? }
????? if (objState!=null) {
??????? try {
????????? objState.close();//關閉statement
??????? }
??????? catch (Exception ex1) {
????????? ex1.printStackTrace();
??????? }
????? }
????? if (objCon!=null) {
??????? objCon.free();//釋放連接
????? }
??? }
??? return iRtn;
? }
}
3.在具體的開發框架下面
?? 定義EAPDBContext 實現 DBContext
???如下方式復用模型:
????? ActualFlow objActualFlow=new ActualFlow();
? ??? objActualFlow.init(new EAPDBContext("actualFlow"));
????? int i=objActualFlow.calculate(this.m_branchID,this.m_year,this.m_month);

不知道是否合理,先練習練習