锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
涓嬮潰緗楀垪浜嗗悇縐嶆暟鎹簱浣跨敤JDBC榪炴帴鐨勬柟寮忥紝鍙互浣滀負涓涓墜鍐屼嬌鐢ㄣ?
1銆丱racle8/8i/9i鏁版嵁搴擄紙thin妯″紡錛?
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl涓烘暟鎹簱鐨凷ID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
String url="jdbc:db2://localhost:5000/sample"; //sample涓轟綘鐨勬暟鎹簱鍚?nbsp;
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb涓烘暟鎹簱
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB涓轟綘鐨勬暟鎹簱鍚?nbsp;
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB涓烘暟鎹簱鍚?nbsp;
Connection conn= DriverManager.getConnection(url);
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB涓烘暟鎹簱鍚?nbsp;
Connection conn= DriverManager.getConnection(url);
String url ="jdbc:postgresql://localhost/myDB" //myDB涓烘暟鎹簱鍚?nbsp;
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;
]]>
1.榛樿鐨勬誕鐐圭被鍨嬫槸鍙岀簿搴︼紙double錛夛紝瑕佹兂瑕佷竴涓猣loat蹇呴』鍦ㄦ誕鐐規暟鍚庨潰鍔燜鎴栬協銆傚錛歠loat pi = 3.14;鏄敊璇殑
2.嫻偣鍨嬭漿鍖栦負鏁村瀷鏃訛紝涓嶈繘琛屽洓鑸嶄簲鍏ワ紝鐩存帴鎴柇灝忔暟鐐瑰悗闈㈢殑鏁?Java鐨勭被鍨嬫鏌ュ緢涓ユ牸錛屼粠浣庣簿搴﹁漿鎹㈠埌楂樼簿搴︽槸鏃犻』鏄懼紡杞崲鐨勶紝double d = 123;銆備絾鏄弽榪囨潵錛岃繘琛岀獎鍖栬漿鎹紝鐢遍珮綺懼害鍚戜綆綺懼害錛屾垨鑰呬竴縐嶇被鍨嬪埌鍙︿竴縐嶇被鍨嬶紝鍒欏繀欏諱嬌鐢ㄥ己鍒剁被鍨嬭漿鍖栥?br />
3.鍦ㄦ暟緇勫0鏄庝腑鍖呭惈鏁扮粍闀垮害姘歌繙鏄笉鍚堟硶鐨勶紒濡傦細int[5] arr; 銆傚洜涓猴紝澹版槑鐨勬椂鍊欏茍娌℃湁瀹炰緥鍖栦換浣曞璞★紝鍙湁鍦ㄥ疄渚嬪寲鏁扮粍瀵硅薄鏃訛紝JVM鎵嶅垎閰嶇┖闂達紝榪欐椂鎵嶄笌闀垮害鏈夊叧銆?br />
4.瀵硅薄綾誨瀷鐨勬暟緇勮櫧鐒惰榛樿鍒濆鍖栦簡錛屼絾鏄茍娌℃湁璋冪敤鍏舵瀯閫犲嚱鏁般備篃灝辨槸璇達細Car[] myCar = new Car[10];鍙垱寤轟簡涓涓猰yCar鏁扮粍瀵硅薄錛佸茍娌℃湁鍒涘緩Car瀵硅薄鐨勪換浣曞疄渚嬶紒榪欎笌鍗曠函鐨刵ew涓涓璞′笉鍚岋紒鍗曚釜鐨刵ew瀵硅薄鏃訛紝灝變細璋冪敤鐩稿簲鐨勬瀯閫犲嚱鏁般?br />
5.鏁扮粍鏈塴ength灞炴э紝瀛楃涓叉湁length錛堬級鏂規硶
6.String涓殑姣忎釜瀛楃閮芥槸涓涓?6浣嶇殑Unicode瀛楃錛岀敤Unicode寰堝鏄撹〃杈句赴瀵岀殑鍥介檯鍖栧瓧絎﹂泦錛屾瘮濡傚緢濂界殑涓枃鏀寔銆俢har涓彲浠ヤ繚瀛樹竴涓腑鏂囨眽瀛楋紝搴斾負char涔熸槸涓涓?6浣嶇殑Unicode瀛楃搴︿負0銆?br />
8.String瀵硅薄鏄笉鍙樼殑錛佸彲浠ュ彉鍖栫殑鏄疭tring瀵硅薄鐨勫紩鐢ㄣ?br />
9.String鐨勪笉鍙樻х殑鏈哄埗鏄劇劧浼氬湪String甯擱噺鍐呮湁澶ч噺鐨勫啑浣欍傚錛?1" + "2" + "3" +......+ "n" 浜х敓浜唍+(n+1)涓猄tring瀵硅薄錛佸洜姝ava涓轟簡鏇存湁鏁堝湴浣跨敤鍐呭瓨錛孞VM鐣欏嚭涓鍧楃壒孌婄殑鍐呭瓨鍖哄煙錛岃縐頒負“String甯擱噺姹?#8221;銆傚 String澶氫箞鐓ч【鍟婏紒褰撶紪璇戝櫒閬囪String甯擱噺鐨勬椂鍊欙紝瀹冩鏌ヨ姹犲唴鏄惁宸茬粡瀛樺湪鐩稿悓鐨凷tring甯擱噺銆傚鏋滄壘鍒幫紝灝辨妸鏂板父閲忕殑寮曠敤鎸囧悜鐜版湁鐨?String錛屼笉鍒涘緩浠諱綍鏂扮殑String甯擱噺瀵硅薄銆?br />
10.java涓殑瀵硅薄鏄湪鍫嗕腑琚垱寤虹殑錛坣ew錛夛紝浣嗗父閲忕被鐨凷tring鏈夌偣鐗瑰埆錛佸儚“haha”榪欐牱鐨勫瓧絎︿覆鏍規嵁絎?鏉℃槸瀛樻斁鍦⊿tring甯擱噺姹犵殑錛岃孲tring s=new String("haha");紜疄鍦ㄥ爢涓鍒涘緩鐨勩?br />
11.throw鍜宼hrows鍏抽敭瀛椼倀hrow鐢ㄥ湪鏂規硶浣撲腑鎶涘嚭寮傚父錛屽悗闈㈡槸涓涓叿浣撶殑寮傚父瀵硅薄銆倀hrows鐢ㄥ湪鏂規硶鍙傛暟鍒楄〃鎷彿鐨勫悗闈紝鐢ㄦ潵澹版槑姝ゆ柟娉曚細鎶涘嚭鐨勫紓甯哥綾伙紝鍚庨潰璺熺潃涓涓紓甯哥被銆?br />
12.瑕佹妸鐩稿簲鐨凷ystem.out鍜孲ystem.err淇℃伅杈撳叆鍒版枃浠朵腑銆備唬鐮侊細
System.out.println("娌′笂");
System.setOut(new PrintStream(new FileOutputStream(new File( "c:/test.txt "))));
System.out.println( "haha ");
System.out.println("error");
} catch (Exception e) {
e.printStackTrace();
}
]]>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
//濡傛湁澶氫釜spring閰嶇疆鏂囦歡鐢ㄩ楀彿闅斿紑
</context-param>
<!-- 閫氳繃Listener閰嶇疆Spring瀹瑰櫒闅廤eb搴旂敤鐨勫惎鍔ㄨ屽垵濮嬪寲 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
浜屻乻truts1鐨勫惎鍔ㄦ柟娉?br />
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml" />
</plug-in>
]]>
Struts鍙厑璁鎬竴涓簲鐢ㄤ腑閰嶇疆涓涓?/span> ActionServlet錛屼絾鏄彲浠ュ皢ActionServlet鎵╁睍銆?/span>
Servlet瀹瑰櫒鍦ㄥ惎鍔ㄦ椂錛屾垨鑰呯敤鎴烽嬈¤姹?/span>ActionServlet鏃跺姞杞?/span>ActionServlet綾伙紝鍦ㄤ笂榪版儏鍐典笅錛?/span>Servlet瀹瑰櫒閮戒細鍦?/span>ActionServlet琚姞杞藉悗绔嬪嵆鎵ц瀹冪殑init()鏂規硶銆?/span>Init()榪囩▼濡備笅錛?/span>
銆銆1.璋冪敤initInternal()鏂規硶錛屽垵濮嬪寲struts妗嗘灦鍐呭湪鐨勬秷鎭祫婧愶紝濡備笌緋葷粺鏃ュ織鐩稿叧鐨勯氱煡銆佽鍛婂拰閿欒淇℃伅銆?/span>
銆銆2.璋冪敤initOther()錛屼粠web.xml鏂囦歡涓姞杞?/span>ActionServlet鐨勫垵濮嬪寲鍙傛暟錛屽config
鍙傛暟
銆銆3.璋冪敤initServlet(),浠?/span>web.xml鏂囦歡涓姞杞?/span>ActionServlet鐨?/span>URL鏄犲皠淇℃伅銆傛澶栬繕浼氭敞鍐?/span>web.xml鍜?/span>
struts閰嶇疆鏂囦歡鎵浣跨敤鐨?/span>DTD鏂囦歡錛岃繖浜?/span>DTD鏂囦歡鐢ㄦ潵楠岃瘉web.xml鍜?/span>struts閰嶇疆鏂囦歡鐨勮娉?/span>
銆銆4.璋冪敤initModuleConfig()錛屽姞杞藉茍瑙f瀽榛樿瀛愬簲鐢ㄦā鍧楃殑struts閰嶇疆鏂囦歡錛涘垱寤?/span>ModuleConfig瀵硅薄錛屾妸瀹冨瓨鍦?/span>ServletContext涓?/span>
5.璋冪敤intiModuleMessageResources()錛屽姞杞藉茍鍒濆鍖栭粯璁ゅ瓙搴旂敤妯″潡鐨勬秷鎭祫婧愶紝鍒涘緩
MessageResources瀵硅薄錛屾妸瀹冨瓨鍦?/span>ServletContext涓?/span>
6.璋冪敤initModuleDataSources()鏂規硶錛屽姞杞藉茍鍒濆鍖栭粯璁ゅ瓙搴旂敤妯″潡鐨勬暟鎹簮銆傚鏋滄病鏈夊湪struts閰嶇疆鏂囦歡涓病鏈夊畾涔?/span><data-sources>鍏冪礌錛屽氨蹇界暐榪欎竴嫻佺▼
7.璋冪敤initModulePlugins(),鍔犺澆騫跺垵濮嬪寲榛樿瀛愬簲鐢ㄦā鍧楃殑鎵鏈夋彃浠?/span>
8.褰撻粯璁ゅ瓙搴旂敤妯″潡琚垚鍔熷垵濮嬪寲鍚庯紝濡傛灉搴旂敤榪樺寘鎷叾浠栧瓙搴旂敤妯″潡錛屽皢閲嶅嫻佺▼4-7錛屽垎鍒鍏朵粬瀛愬簲鐢ㄦā鍧楄繘琛屽垵濮嬪寲銆傛墿灞?/span>ActionServlet綾伙紝瀹氫箟濡備笅鐨勭被錛?/span>
<杞?/span>>
The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.)
2.濡傛灉鏈夊寘鍚叧緋?composition),閭d箞瀹冪殑鎴愬憳瀵硅薄鎸夌収澹版槑鐨勯『搴忚鏋勯?
Member initializers are called in the order of declaration.
3.媧劇敓綾繪瀯閫犲嚱鏁扮殑鍐呭(body)琚墽琛?
The body of the derived-class constructor is called.
涓涓疄渚?
杈撳嚭:
Meal()
Lunch()
Cake()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
main()鍑芥暟涓鏋勯犱竴涓猄andwich鐨勫璞?璋冪敤(騫朵笉鏄墽琛?瀹冨熀綾籔ortableLunch鐨勬瀯閫犲嚱鏁?PortableLunch鍙堥? 褰掔殑璋冪敤,鐒跺悗鏄疢eal,Meal鏄戶鎵跨殑鏈搴曞眰鐨勫熀綾?涓嶇畻Object)鎵浠ュ畠鐨勬瀯閫犲嚱鏁伴鍏堣鎵ц,鐒跺悗鎸夋搴忚繑鍥炲埌Lunch, PortableLunch,浣嗗湪PortableLunch鐨勬瀯閫犲嚱鏁拌鎵ц涔嬪墠,瀹冪殑鎴愬憳瀵硅薄Cake a鍏堟寜鐓у0鏄庣殑欏哄簭琚瀯閫?鐒跺悗鎵цPortableLunch(),鎺ョ潃鏄疭andwich鐨勬垚鍛樺璞?鏈鍚庢槸Sandwich().
娉?琚敞閲婃帀鐨勪唬鐮?灝哹ase-class鐨勫璞′綔涓篸erive-class鐨勬垚鍛樺璞?榪欐牱浼氶掑綊鏃犳硶緇撴潫,鏈鍚庣▼搴忓洜鍫嗘爤鑰楀敖鑰岀粨鏉?Exception in thread main java.lang.StackOverflowError).
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1134115(杞?
This Tutorial will explain you, what struts is, how it builds itself up and why it is advantage to use it for your web application. First, I will provide you some informations about Java Servlets, Java Server Pages and Java Beans, because they are parts of struts.
Servlets represents java programs that runs on a web server. They allow the developer to produce dynamic web sites with java.
A Servlet has the following tasks
It reads and processed data, which a user typed in a HTML form on a web page.
If necessary other informations will be processed. For Example what browser or system will be used.
It generate results with the existing data. It calls the business logic directly in the servlet or another class, which contain the logic or executes a database query.
The results will be formated. If the browser. If the Browser expects an answer in the HTML format, then the results must be formatted in accordance with the standard. It is possible to return different formats of data with a servlet. (gif, jpeg, doc, etc.).
Suitable answer parameters are set. Befor the servlet return the data to the browser, it sends some parameter. The parameter contains the format, that will returned by the servlet, what time the browser use to cache the site and some more.
Return the document, in the format that it sends befor, to the browser.
JavaServer Pages (JSP) are text documents, which are similar to HTML Files. But you find also java code in the JSP File. JavaServer Pages allow you to mix regulare, static HTML with dynamic generated contents of servlets. The java code is inserted in the HTML document on a JSP File, differently to a servlet, where the HTML code is embedded in the java code.
Java BeansJava Beans are nothing else as classes, which keep a fixed naming convention, defined by Sun, for their event processing and methods. The attributes (variables) of the java beans are private. The access to these attributes is managed by access methods. The java specification specify these access methodes (getter and setter methods). If a java bean contains a attribute name, you can set or get the value of the attributes with the methode setName() or getName().
The business logic is the core of the application. The processes are implemented in the business logic to manage the data. For Example: when someone borrows a book in a library, that is a process in the business logic. The process change the state of the data (is the book borrowsed or not) or reads the state and then provide this information for a dialog.
This short outline should be sufficient, in order to understand the structure of struts.
Why is struts helpful?Some peoples develop web applications with Perl or PHP and implement their SQL Querys and the business logics directly in the HTML document.
The source code looks like the following example:
You can develop in this way, when you use java servlets or JSP. It is convenient in small projects. But imagine you have 70 dialogs, many database querys in this dialogs and you want to define a field status, setting if a book is deleted or not.
Good luck
In order to alter functions and database querys easily, we should seperate these from the dialogs.
Advantage of separationChanges on the functionality, without working in the dialogs.
Better overview, functionality not mixed with the dialogs.
Easy maintain a application
Different dialogs, but the same functionality
In order to control the interaction between the dialogs and business processes, you need a central control unit. This control unit manage all importants courses of the application, when which business process and which dialog will be used.
You have some disadvantages, if you implement the order of the processes directly to the business logic.
You can not reuse a part of the processes. In the following picture the process "Give Money" will bring you to the process "Select sort of icecream" every time.
If you want to change the order of the processes or add more options, you have to change it directly in the business logic.
It is easy to change the order of the business processes.
Better overview, which dialogs will be used on which business processes.
A central place to control the interaction between business processes and dialogs.
The name of this model is Model-View-Controller (MVC)
Model (business logic / business processes ? Java Beans)
View (dialogs ? JavaServer Pages)
Controller (central control unit - Java Servlets)
You will find more about the MVC later in this tutorial.
If you do not want to translate a dialog, you put the dialog texts directly into the JSP File.
Struts call the locale specific resource file by using the browser settings. You may overwrite this setting. It is quite easy to build an internationalized application with struts.
Struts saves error messages in the business logic and can display this in the dialogs. You can show all errors or bind an error to a field (ex. Emailaddress incorrect) and show the error beside of it.
The error messages will be added to the resource files, so the errors can be internationalised too.
Whether an input field of a form contains a valid date, an email, a number or something else, in each application you have to check it. Struts supports a complete solution to validate form fields. You only have to define which form field will be checked and which error message will be displayed.
Components of strutsJavaServer Pages (JSP) take over the role of dialogs in struts,
Java Beans take over the business logic and business processes and
Java Servlets take over the central control unit
Thats why we talk about three great parts of struts.
Model (business logic / business processes ? Java Beans)
View (dialogs ? JavaServer Pages)
Controller (central control unit - Java Servlets)
The model represent the actual state of the application. Two kinds of java beans are used. There are java beans which contains the data of a form or data to display (ex. The books of a library) and java beans which includes the functionality of the application or call the business logic (when a user borrows a book).
The view component is responsible for the presentation of the data. The java server pages contain HTML, XML and Java Script, like a normal HTML site. Futhermore you can use java code. Struts provide tag libraries, a summary of functions, which can be use to prepare the data for displaying.
The last component is the controller. The controller manage the request of the web browser, which a user called by an address (URL). But also forward to an action which are execute and which dialogs will be used to display the informations.
The picture below illustrates the interaction between these components.
If the user sends a query with a browser, the controller(servlet) gets and processes this query. It decides which action will be called or to which view component it must be forward.
After the controller calls an action, the action can read data from a database and provide this data to the model component, java beans. The action (business logic) returns the "next step" to the controller. The controller checks what kind is the next step. (JSP View, next action, ...) and forwards to it.
The view component (JSP) reads the updated data from the model component and prepare this for the presentation. Then it sends the answer as HTML site back to the browser. The user sees the result of his query.
Struts configurationStruts will be configured with various configuration files. The following files are very important.
web.xml
struts-config.xml
Struts-Tag-Bibliotheken
Properties-Dateien
The illustration will show you, on which places struts use the configuration files.
web.xml
With the web.xml you configure the web server for the struts application. In this file you can set where the web server find the struts-config.xml and some other global properties.
struts-config.xml
The controller calls the business logic or a view with a name. The allocation of the names to the action classes or JSP Files(Views) will be set in the struts-config.xml. The advantage is that you can change the definition of the Workflows (Action Mapping), without using the long class names everytime. If you change a class name, you only have to change the name in the struts-config.xml. You don´t update any other part of your application.
Struts-Tag-Libraries
The Struts-Tag-Libraries, a summary of functions, extends the functionality of JavaServer pages. They support the internationalisation (multi-language) of your web application, and the easy creation of form elements.
Properties files
This files will be used to keep the internationalized textes of your web application. You can create a seperated properties file for each language, which contain all textes of the application.
杞嚜錛歨ttp://www.laliluna.de
鐬椂鎬?/strong>
鐢眓ew鍛戒護寮杈熷唴瀛樼┖闂寸殑java瀵硅薄,
濡傛灉娌℃湁鍙橀噺瀵硅瀵硅薄榪涜寮曠敤,瀹冨皢琚玧ava铏氭嫙鏈哄洖鏀?
鐬椂瀵硅薄鍦ㄥ唴瀛樺绔嬪瓨鍦?瀹冩槸鎼哄甫淇℃伅鐨勮澆浣?涓嶅拰鏁版嵁搴撶殑鏁版嵁鏈変換浣曞叧鑱斿叧緋?鍦℉ibernate涓?鍙氳繃session鐨剆ave()鎴杝aveOrUpdate()鏂規硶灝嗙灛鏃跺璞′笌鏁版嵁搴撶浉鍏寵仈,騫跺皢鏁版嵁瀵瑰簲鐨勬彃鍏ユ暟鎹簱涓?姝ゆ椂璇ョ灛鏃跺璞¤漿鍙樻垚鎸佷箙鍖栧璞?
鎸佷箙鎬?/strong>
澶勪簬璇ョ姸鎬佺殑瀵硅薄鍦ㄦ暟鎹簱涓叿鏈夊搴旂殑璁板綍,騫舵嫢鏈変竴涓寔涔呭寲鏍囪瘑.濡傛灉鏄敤hibernate鐨刣elete()鏂規硶,瀵瑰簲鐨勬寔涔呭璞″氨鍙樻垚鐬椂瀵硅薄,鍥犳暟鎹簱涓殑瀵瑰簲鏁版嵁宸茶鍒犻櫎,璇ュ璞′笉鍐嶄笌鏁版嵁搴撶殑璁板綍鍏寵仈.
褰撲竴涓猻ession鎵цclose()鎴朿lear()銆乪vict()涔嬪悗,鎸佷箙瀵硅薄鍙樻垚鑴辯瀵硅薄,姝ゆ椂鎸佷箙瀵硅薄浼氬彉鎴愯劚綆″璞?姝ゆ椂璇ュ璞¤櫧鐒跺叿鏈夋暟鎹簱璇嗗埆鍊?浣嗗畠宸蹭笉鍦℉Ibernate鎸佷箙灞傜殑綆$悊涔嬩笅.
鎸佷箙瀵硅薄鍏鋒湁濡備笅鐗圭偣:
1.鍜宻ession瀹炰緥鍏寵仈;
2.鍦ㄦ暟鎹簱涓湁涓庝箣鍏寵仈鐨勮褰?
鑴辯鎬?br /> 褰撲笌鏌愭寔涔呭璞″叧鑱旂殑session琚叧闂悗,璇ユ寔涔呭璞¤漿鍙樹負鑴辯瀵硅薄.褰撹劚綆″璞¤閲嶆柊鍏寵仈鍒皊ession涓婃椂,騫跺啀嬈¤漿鍙樻垚鎸佷箙瀵硅薄.
鑴辯瀵硅薄鎷ユ湁鏁版嵁搴撶殑璇嗗埆鍊?鍙氳繃update()銆乻aveOrUpdate()絳夋柟娉?杞彉鎴愭寔涔呭璞?
鑴辯瀵硅薄鍏鋒湁濡備笅鐗圭偣:
1.鏈川涓婁笌鐬椂瀵硅薄鐩稿悓,鍦ㄦ病鏈変換浣曞彉閲忓紩鐢ㄥ畠鏃?JVM浼氬湪閫傚綋鐨勬椂鍊欏皢瀹冨洖鏀?
2.姣旂灛鏃跺璞″浜嗕竴涓暟鎹簱璁板綍鏍囪瘑鍊?
銆婅漿銆?/p>
This time I based the DAO example on interfaces. Tools like Hibernate already provide database portability, so persistence layer portability shouldn't be a driving motivation for interfaces. However, DAO interfaces make sense in more complex applications, when several persistence services are encapsulate in one persistence layer. I'd say that you should use Hibernate (or Java Persistence APIs) directly in most cases, the best reason to use an additional DAO layer is higher abstraction (e.g. methods like getMaximumBid() instead of session.createQuery(...) repeated a dozen times).
I use one interface per persistent entity, with a super interface for common CRUDfunctionality:
The DAO interface for a particular entity extends the generic interface and provides the type arguments:
We basically separate generic CRUD operations and actual business-related data access operations from each other. (Ignore the named query constants for now, they are convenient if you use annotations.) However, even if only CRUD operations are needed for a particular entity, you should still write an interface for it, even it it is going to be empty. It is important to use a concrete DAO in your controller code, otherwise you will face some refactoring once you have to introduce specific data access operations for this entity.
An implementation of the interfaces could be done with any state-management capable persistence service. First, the generic CRUD implementation with Hibernate:
There are some interesting things in this implementation. First, it clearly needs a Session to work, provided with setter injection. You could also use constructor injection. How you set the Session and what scope this Session has is of no concern to the actual DAO implementation. A DAO should not control transactions or the Session scope.
We need to suppress a few compile-time warnings about unchecked casts, because Hibernate's interfaces are JDK 1.4 only. What follows are the implementations of the generic CRUD operations, quite straightforward. The last method is quite nice, using another JDK 5.0 feature, varargs. It helps us to build Criteria queries in concrete entity DAOs. This is an example of a concrete DAO that extends the generic DAO implementation for Hibernate:
We could bring it all together in a DAO factory, which not only sets the Session when a DAO is constructed but also contains nested classes to implement CRUD-only DAOs with no business-related operations:
Note that this factory example is suitable for persistence layers which are primarily implemented with a single persistence service, such as Hibernate or EJB 3.0 persistence. If you have to mix persistence APIs, for example, Hibernate and plain JDBC, the pattern changes slightly. Keep in mind that you can also call session.connection() inside a Hibernate-specific DAO, or use one of the many bulk operation/SQL support options in Hibernate 3.1 to avoid plain JDBC.
Finally, this is how data access now looks like in controller/command handler code (pick whatever transaction demarcation strategy you like, the DAO code doesn't change):
鍑鴻嚜錛歨ttp://www.hibernate.org