|
官方 http://jsptags.com/tags/navigation/pager/index.jsp先向 demo 頁面樣式: ######################################################################################## request.getParameter 中參數
'pager.offset'=6 <%@?page?session="false"?%> <%@?taglib?uri="http://jsptags.com/tags/navigation/pager"?prefix="pg"?%> <%@?page?contentType="text/html;?charset=gb2312"%> <html> <head> <title>Pager?Tag?Library?Demo</title> <meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">
<!--?參數展現?--> request.getParameter?中參數<br/> <%for(Object?otmp?:?request.getParameterMap().keySet()?){%> ????<%="'"+otmp.toString()+"'"%>=<%=request.getParameter(otmp.toString())%><br\> <%}%>
???? ???? <form?action="<%=?request.getRequestURI()?%>"?method="get"> <br/> <!--? items=數據總條?? index=分頁標明展現格式;"center",?"forward"?or?"half-full". maxPageItems=每頁最大展現行數;? maxIndexPages=每頁最大可選頁數; isOffset=數據是否展現; scope=參數作用范圍;
-->
<pg:pager ????items="<%=?webPalette.length?%>" ????index="<%=?"center"?%>" ????maxPageItems="<%=?3?%>" ????maxIndexPages="<%=?10?%>" ????isOffset="<%=?true?%>" ????export="offset,currentPageNumber=pageNumber" ????scope="request">
<br/>分頁中標簽中隱含屬性?'offset'=<%=offset%><br/>
??<!--? ??????分頁需要向下傳遞的參數; ??????????????比如組合查詢使用參數.? ??????????此為get提交;亂碼問題特別注意; ??????????如:parameter(google)?參數由本頁面傳遞給下一分頁頁面; ??--> ??<pg:param?name="google"/>
<!--?分頁具體表現頁面?--> <pg:index> ??<jsp:include?page="/WEB-INF/jsp/texticon.jsp"?flush="true"/> </pg:index>
<hr> <table?width="90%"?cellspacing="4"?cellpadding="4"> ????<%for?(int?i?=?offset.intValue(),l?=?Math.min(i?+?10,?webPalette.length);i?<?l;?i++){%> ????????<pg:item> ????????????<tr><th?bgcolor="<%=?webPalette[i][0]?%>"><font?color="<%=?webPalette[i][1]?%>"><%=?i?+?1?%></font></th></tr> ????????</pg:item> ????<%}%> </table> <hr>
<pg:index> ??<jsp:include?page="/WEB-INF/jsp/texticon.jsp"?flush="true"/> </pg:index>
</pg:pager> </center> </body> </html>
<%! private?static?final?String?BLACK?=?"#000000",?WHITE?=?"#ffffff"; private?static?final?String[][]?webPalette?=?{ ????{?WHITE,???BLACK}, ????{"#cccccc",BLACK}, ????{"#999999",BLACK}, ????{"#666666",WHITE}, ????{"#333333",WHITE}, ????{?BLACK,???WHITE}, ????{"#ffcc00",BLACK}, ????{"#ff9900",BLACK}, ????{"#ff6600",BLACK}, ????{"#ff3300",WHITE}, ????{"#99cc00",BLACK}, ????{"#cc9900",BLACK}, ????{"#ffcc33",BLACK}, ????{"#ffcc66",BLACK}, ????{"#ff9966",BLACK}, ????{"#ff6633",BLACK}, ????{"#cc3300",WHITE}, ????{"#cc0033",WHITE}, ????{"#ccff00",BLACK}, ????{"#ccff33",BLACK}, ????{"#333300",WHITE} };
private?static?final?String?getParam(ServletRequest?request,?String?name, ????String?defval) { ????String?param?=?request.getParameter(name); ????return?(param?!=?null???param?:?defval); }
private?static?final?int?getParam(ServletRequest?request,?String?name, ????int?defval) { ????String?param?=?request.getParameter(name); ????int?value?=?defval; ????if?(param?!=?null)?{ ????try?{?value?=?Integer.parseInt(param);?} ????catch?(NumberFormatException?ignore)?{?} ????} ????return?value; }
%>
<!--?分頁具體表現頁面?-->
<%@?page?session="false"?%> <%@?taglib?uri="http://jsptags.com/tags/navigation/pager"?prefix="pg"?%>
<jsp:useBean?id="currentPageNumber"?type="java.lang.Integer"?scope="request"/>
<font?face="fixed">
<!--?到第一頁?--> <pg:first?export="firstPageUrl=pageUrl"?unless="current"> ??<a?href="<%=?firstPageUrl?%>">?|<?</a> </pg:first>
<!--?每次后退跳10頁?--> <pg:skip?export="skipBackPageUrl=pageUrl"?pages="<%=?-10?%>"> ??<a?href="<%=?skipBackPageUrl?%>"><<<</a> </pg:skip>
<!--?每次后退跳?1?頁?--> <pg:prev?export="prevPageUrl=pageUrl"> ??<a?href="<%=?prevPageUrl?%>"><<</a> </pg:prev>
<!--?定位?跳頁數?--> <pg:pages><% ??if?(pageNumber?==?currentPageNumber)?{ ????%>?<b><%=?pageNumber?%></b>?<% ??}?else?{ ????%>?<a?href="<%=?pageUrl?%>"><%=?pageNumber?%></a>?<% ??} %></pg:pages>
<!--?每次前進跳?1?頁?--> <pg:next?export="nextPageUrl=pageUrl"> ??<a?href="<%=?nextPageUrl?%>">>></a> </pg:next>
<!--?每次后前進10頁?--> <pg:skip?export="skipForwardPageUrl=pageUrl"?pages="<%=?10?%>"> ??<a?href="<%=?skipForwardPageUrl?%>">>>></a> </pg:skip>
<!--?到最后頁?--> <pg:last?export="lastPageUrl=pageUrl"?unless="current"> ??<a?href="<%=?lastPageUrl?%>">>|</a> </pg:last>
</font>
-> http://displaytag.sourceforge.net分頁流程描述: ? 1.組合查詢參數得取。 ??? 比如時間范圍,用戶名模糊查詢。。這些存入 session->data;name ? 2.通過持久層得到 ‘數據總條數’‘當前展現頁數據’; ? 3.分頁點擊;根據session 組合參數,再次向數據庫申請書及。 代碼: <jsp:root?version="1.2"?xmlns:jsp="http://java.sun.com/JSP/Page" ????xmlns:display="urn:jsptld:http://displaytag.sf.net"> ????<jsp:directive.page?import="java.util.regex.Pattern"?/> ????<jsp:directive.page?import="test.Bean"?/> ????<jsp:directive.page?import="java.util.ArrayList"?/> ????<jsp:directive.page?import="java.util.List"?/> ????<jsp:directive.page?contentType="text/html;?charset=UTF-8"?/> ????<jsp:include?page="inc/header.jsp"?flush="true"?/>
<jsp:scriptlet>?<![CDATA[
????Pattern?pattern?=?Pattern.compile("d-[0-9]+-p"); ????String?pageIndexName?=?null?; ????for(Object?otmp?:?request.getParameterMap().keySet()?){ ????????if(?pattern.matcher(otmp.toString()).find()?){ ????????????pageIndexName?=?otmp.toString(); ????????????break; ????????}? ????} ???? ???? ????int?pageSize?=?10;???//每頁顯示的條數 ????int?pageIndex?=?0?; ????if(pageIndexName!=null){ ????????pageIndex?=?(request.getParameter(pageIndexName)==null)? ????????????????0:(Integer.parseInt(request.getParameter(pageIndexName))?-?1);??//當前頁數 ????} ???? ???????????? ?????List?list?=?new?ArrayList();??? ?????for(int?i=0;i<pageSize;i++){ ?????????list.add(?new?Bean(pageIndex*10+i,"liu-"+pageIndex*10+i)??); ?????} ????request.setAttribute("data",list); ????session.setAttribute(?"resultSize",?100?); ????out.print("!"+pageIndexName);
????]]></jsp:scriptlet>
????<h2>數據分頁展現開始</h2> ????<display:table?name="data"?pagesize="10"?partialList="true" ????????size="sessionScope.resultSize"> ????????<display:column?property="id"?title="ID"></display:column> ????????<display:column?property="name"?title="名字"></display:column> ????</display:table> ???? ???? ????<jsp:include?page="inc/footer.jsp"?flush="true"?/>
</jsp:root>
?? 項目開發:就好像是一個取得真理的一個過程。 ?? 在開始“沒有人”會知道什么是對的,什么是錯的。所謂的客戶(中世紀教會的教徒),告訴你月亮是“熱脹冷縮”造成的“陰晴圓缺”。 ?? 在初期你敢于否定“熱脹冷縮”原理?或者說根本就是認為月亮是受“熱脹冷縮”原理影響的。 ?? ?? 那好,下面我們根據月亮圓缺原理,寫個統計溫度與月亮亮度報表。
?? 客戶自己想要的東西也是一個認知的過程。編碼要在開始就要確定是在一個不穩定的環境(即使錯了我也能容易修改,這是軟件最有價值的地方)。對于這些理解為項目的可變性總結出的一些見解: ?? 1.盡量明確各層使用框架。這樣能統一技術,明確編碼風格,統一存放,查找地址。這樣就能很好的 定位要修改文件的物理地址和 盡量不與個人技術有關 。 ?? 2.盡量明確各種動作的命名規范。這樣不但能很好的使用 aop ,而且為修改提供了 邏輯地址查找提供便利。 ?? 3.減少個人英雄主義。由于某些個人原因,引入與項目不兼容的技術,這是很危險的。只有這為“英雄”能修改的后果很嚴重。 ?? 4.編碼中對“可預見性”的代碼結構適應,擴展接口預留。月亮缺失可能不是“熱脹冷縮”引起的怎么辦(當然也是最難做到)這只要編碼想到可能就有“可變性”就要有好的相應對策,比如:公司鼓勵程序員的為“可變性預留接口”,當然最好也注意下預留接口的 規范 。
參考引用: 主題:使用全功能Tomcat簡化調試讓classpath參數走開直接用eclipse 調試 : tomcat - > service.xml <Context debug="5" docBase="E:/tomcat/tomcatwebroot" path="/tomcatwebroot" reloadable="true" privileged="true"> </Context>這引入一個java 文件 到你的 工程中 import?java.io.File; import?java.io.FileFilter; import?java.lang.reflect.Method; import?java.net.URL; import?java.net.URLClassLoader; import?java.util.ArrayList; import?java.util.List;
public?class?MainClassLoad?{ ????//commons-lang-2.0.jar ????static?String?TOMCAT_HOME?=?"D:\\apache\\apache-tomcat-5.5.20\\apache-tomcat-5.5.20";
????public?static?void?main(String[]?args)?throws?Exception?{ ????????System.setProperty("catalina.home",?TOMCAT_HOME); ???????? ????????final?ClassLoader?classLoader?=?getClassLoader(new?String[]{ ????????????????TOMCAT_HOME+"\\common\\lib", ????????????????TOMCAT_HOME+"\\server\\lib", ????????????????TOMCAT_HOME+"\\bin" ????????}); ????????Object?obj?=?getObject(classLoader,"org.apache.catalina.startup.Catalina"); ????????Method?setConfig?=?obj.getClass().getMethod("setConfig",?new?Class[]{String.class}); ????????setConfig.invoke(obj,?TOMCAT_HOME?+?"/conf/server.xml"); ???????? ????????Method?start?=?obj.getClass().getMethod("start",?new?Class[]{}); ????????start.invoke(obj,?null); ????} ???? ???? ???? ????public?static?Object?getObject(ClassLoader?classLoader,String?className)?throws?Exception{ ????????return?classLoader.loadClass(className).newInstance(); ????}
???? ????public?static?ClassLoader?getClassLoader(String[]?libPath)?throws?Exception{ ????????List<URL>?list?=?new?ArrayList<URL>(); ????????FileFilter?fileFilter?=?new?FileFilter()?? ?????????{?? ???????????public?boolean?accept(File?dir)?? ???????????{?? ?????????????String?name?=?dir.getName().toLowerCase();?? ?????????????return?name.endsWith("jar")?||?name.endsWith("zip");?? ???????????}?? ?????????}; ????????? ????????for(String?stmp?:?libPath){ ????????????for(File?ftmp?:?new?File(stmp).listFiles(fileFilter)?){ ????????????????list.add(?new?URL("file",null,ftmp.getPath())?);??? ????????????} ????????} ????????URL[]?urls?=?new?URL[list.size()];?? ?????????//?fill?the?urls?array?with?URLs?to?library?files?found?in?libRoot?? ?????????for(int?i?=?0;?i?<?list.size();?i++)?{?? ???????????urls[i]?=?new?URL("file",null,list.get(i).getPath()?);?? ?????????} ????????ClassLoader?classLoader?=?new?URLClassLoader(urls,?? ?????????????????Thread.currentThread().?? ?????????????????getContextClassLoader());?? ????????return??classLoader?; ????} ????
}
代碼編輯耗時 40 分鐘 ;自己感覺很慢!!
文本文件,我想寫個Java程序,讀一遍這個 文件,然后打印出來文件中英文字母(a 到 z)出現的次數。不區分大小寫。 代碼: import?java.io.FileInputStream; import?java.io.InputStreamReader; import?java.util.Collections; import?java.util.LinkedHashMap; import?java.util.Map;
public?class?LinjiawangMain?{ ???? ????public?static?void?main(String[]?args)?throws?Exception?{ ????????Map<Character,?Integer>?map?=?getCountByFileLetters("chars.txt")?; ????????for(char?ctmp?:?map.keySet()){ ????????????System.out.println(?ctmp+"="+map.get(ctmp)?); ????????} ????} ???? ???? ????//統計?文件字符工具方法 ????public?static?Map<Character,?Integer>?getCountByFileLetters(String?filePath)?throws?Exception{ ????????//邏輯?參數定義 ????????int?int_A?=?'A'; ????????int?int_Z?=?'Z'; ???????? ????????int?int_a?=?'a'; ????????int?int_z?=?'z'; ???????? ????????//數據收集?map?(保持存儲順序?使用?LinkedHashMap?) ????????Map<Character,?Integer>?map?=?new?LinkedHashMap<Character,?Integer>(); ???????? ????????//以?ISO-8859-1?字符?讀取?.classpath?下的?文件? ????????InputStreamReader?read?=?new?InputStreamReader(new?FileInputStream( ????????????????LinjiawangMain.class.getClassLoader().getResource(filePath) ????????????????????????.getPath()),?"ISO-8859-1"); ????????//臨時數據收集 ????????int[]?iis?=?new?int[int_z-int_a]; ????????while(read.ready()?){ ????????????int?itmp?=?read.read(); ????????????//?當?'a'<=itmp<='z'?(短路) ????????????//?當?'A'<=itmp<='Z'?(非短路)?后修改itmp到?'a'?到?'z'范圍? ????????????if(? ????????????????(?itmp?>=int_a?&&?itmp<=int_z?)?|| ????????????????(?itmp>=int_A?&&?itmp<=int_Z??)?|?(itmp=(itmp+(int_a-int_A)))>=int_a? ????????????){ ????????????????iis[itmp-int_a]++?; ????????????} ????????} ???????? ????????//由?臨時數據收集?轉?到正式收集? ????????for(int?i=0;i<int_z-int_a;i++){ ????????????map.put(?(char)(i+int_a),iis[i]?); ????????} ????????//非修改?map? ????????return?Collections.unmodifiableMap(map)?; ????} ???? }
文件:aAbbbbCCCCc2345ABdb abc d
? 那位來開個頭吧
正則匹配 html標題
??? <[hH]([1-9])>.*?</[hH]\1> 數據庫查詢執行順序:??? from->where->group by->聚集函數->having->order by??? table level(id,name) ??? select count(*) as cu from level where id>1 group by name desc? having cu>=1 order by id ; 火狐 xpath 取 document dom 值:??? id('table8')/tbody/tr[6]/td/@valign 數據庫有為空列排放位置:??? 1.? select name, ??? ??? ??? ??? ? case ??? ??? ??? ??? ???? ??? when name is null then 0 ??? ??? ??? ??? ???? ??? else? 1? ??? ??? ??? ??? ? end ??? ??? ??? ??? ??? as is_null ???????? from level ???????? order by is_null? ; ??? 2.select name from level where name is null ?????? union????? select name from level where name is not null ; 數據庫報表表鏈為空項替換為0table a (id); values - > 1 ,2 ,3 table b (id,num); -> (1,15),(1,5) 查詢結果為 要為 1??? 20 2??? 0 3??? 0 select?a.id?as?id, ????????sum( ????????????case ????????????????when?b.num?is??null?then?0 ????????????????else?b.num ?????????????end ????????????)?as?num from?a?left?join?b?on?a.id=b.id? group?by?id?;
package
?uu;
import
?java.io.BufferedReader;
import
?java.io.File;
import
?java.io.FileInputStream;
import
?java.io.FileOutputStream;
import
?java.io.InputStreamReader;
import
?java.io.OutputStreamWriter;
import
?java.util.ArrayList;
import
?java.util.HashMap;
import
?java.util.List;
import
?java.util.Map;
import
?java.util.regex.Pattern;
public
?
class
?MainExc?{ ????
//
數據文本?
????
static
?String?filePath?
=
?
"
smsservice.data
"
; ????
//
輸出文本
????
static
?String?outfile?
=
?
"
outfile.html
"
; ????
//
輸出?文本格式
????
static
?String?charset?
=
?
"
GBK
"
;
????
//
數據邏輯?展現
????
static
?Map
<
Pattern,?String
>
?map?
=
?
new
?HashMap
<
Pattern,?String
>
(); ????
static
?{ ????????map.put(Pattern.compile(
"
^XZT
"
),?
"
sina
"
);
??????? 。。。。。 ????}
????
//
類型定義
????
static
?Map
<
Pattern,?String
>
?maptype?
=
?
new
?HashMap
<
Pattern,?String
>
(); ????
static
?{
????????maptype.put(Pattern.compile(
"
^3P45
"
),?
"
客戶端
"
); ????????maptype.put(Pattern.compile(
"
^6930
"
),?
"
圖表
"
);
??????? ......
????}
????
//
禁止展現
????
static
?List
<
Pattern
>
?listNo?
=
?
new
?ArrayList
<
Pattern
>
(); ????
static
?{ ????????listNo.add(Pattern.compile(
"
^9588Command$
"
)); ????????listNo.add(Pattern.compile(
"
^5kjsCommand$
"
)); ????????listNo.add(Pattern.compile(
"
^529901001011021$
"
)); ????????listNo.add(Pattern.compile(
"
^529901001011022$
"
)); ????}
???? ???? ????
public
?
static
?
void
?main(String[]?args)?
throws
?Exception?{ ????????
//
?read
????????InputStreamReader?read?
=
?
new
?InputStreamReader( ????????????????
new
?FileInputStream(MainExc.
class
.getClassLoader().getResource( ????????????????????????filePath).getPath()),?charset); ???????? ????????
//
?writer
????????
if
?(MainExc.
class
.getClassLoader().getResource(outfile)?
==
?
null
)?{ ????????????
new
?File(MainExc.
class
.getClassLoader().getResource(
"
.
"
).getPath() ????????????????????
+
?
"
/outfile.html
"
).createNewFile(); ????????} ????????OutputStreamWriter?writer?
=
?
new
?OutputStreamWriter( ????????????????
new
?FileOutputStream(MainExc.
class
.getClassLoader() ????????????????????????.getResource(outfile).getPath()),?charset); ???????? ????????
//
?內存?裝載?處
????????List
<
Map
<
String,?String
>>
?datas?
=
?
new
?ArrayList
<
Map
<
String,?String
>>
();
???????? ???????? ????????StringBuffer?buffer?
=
?
new
?StringBuffer(); ????????BufferedReader?reader?
=
?
new
?BufferedReader(read); ????????String?stmp?
=
?
null
; ????????List
<
String
>
?colName?
=
?
new
?ArrayList
<
String
>
(); ????????
//
得到?列名
????????
if
?((stmp?
=
?reader.readLine())?
!=
?
null
?
&&
?
!
stmp.trim().equals(
""
))?{ ????????????
//
?split?-?>?Tab
????????????
for
?(String?stmp2?:?stmp.split(
"
????
"
))?{ ????????????????colName.add(stmp2); ????????????} ????????} ???????? ????????
//
列數據收集
????????
while
?((stmp?
=
?reader.readLine())?
!=
?
null
?
&&
?
!
stmp.trim().equals(
""
))?{
????????????Map
<
String,?String
>
?data?
=
?
new
?HashMap
<
String,?String
>
(); ????????????String[]?sdata?
=
?stmp.split(
"
????
"
); ????????????
for
?(
int
?i?
=
?
0
;?i?
<
?colName.size();?i
++
)?{ ????????????????data.put(colName.get(i),?sdata[i]); ????????????} ????????????datas.add(data); ????????}
???????? ????????
//
?writer
????????writer.append(
"
<center><table??border>\n
"
); ????????writer.append(
"
<tr>
"
?
+
?
"
<td>通道伙伴</td>
"
?
+
?
"
<td>指令</td>
"
?
+
?
"
<td>長號碼</td>
"
????????????????
+
?
"
<td>資費</td>
"
?
+
?
"
<td>業務類型(圖表/客戶端)</td>
"
?
+
?
"
</tr>\n
"
); ????????
//
?通道伙伴?指令?長號碼?資費?業務類型(圖表?客戶端?)
????????wfor:?
for
?(Map
<
String,?String
>
?mtmp?:?datas)?{
????????????
for
?(Pattern?ptmp?:?listNo)?{ ????????????????
if
?(ptmp.matcher(mtmp.get(
"
command
"
)).find())?{ ????????????????????
continue
?wfor; ????????????????} ????????????}
????????????String?hzhb?
=
?
""
;
????????????
for
?(Pattern?ptmp?:?map.keySet())?{ ????????????????
if
?(ptmp.matcher(mtmp.get(
"
command
"
)).find())?{ ????????????????????hzhb?
=
?map.get(ptmp); ????????????????} ????????????}
????????????String?type?
=
?
""
; ????????????
for
?(Pattern?ptmp?:?maptype.keySet())?{ ????????????????
if
?(ptmp.matcher(mtmp.get(
"
command
"
)).find())?{ ????????????????????type?
=
?maptype.get(ptmp); ????????????????} ????????????}
????????????writer.append(
"
<tr>
"
?
+
?
"
<td?align='center'>
"
?
+
?hzhb?
+
?
"
</td>
"
????????????????????
+
?
"
<td?align='center'>
"
?
+
?mtmp.get(
"
command
"
)?
+
?
"
</td>
"
????????????????????
+
?
"
<td?align='center'>
"
?
+
?mtmp.get(
"
num
"
)?
+
?
"
</td>
"
????????????????????
+
?
"
<td?align='center'>
"
?
+
?mtmp.get(
"
money
"
)?
+
?
"
</td>
"
????????????????????
+
?
"
<td?align='center'>
"
?
+
?type?
+
?
"
</td>
"
?
+
?
"
</tr>\n
"
); ????????} ????????writer.append(
"
</table></center>\n
"
);
????????
//
?save?
????????writer.flush(); ????????writer.close(); ????}
}
一篇非常好的 Hibernate? 文章 出自: http://guxing.blog.enorth.com.cn/article/174717.shtml Hibernate最讓人頭大的就是對集合的加載形式。 書看了N次了,還是沒有真正理解Hibernate。所以下午專門做了下測試,對配置文件的意思加深了認識。
假設有兩個表,Photos(一)? ---? picture(多)Photo包含picture集合
結論1: HQL代碼 > fetch(配置) > lazy (配置) 結論2: 默認 lazy="true" 結論3: fetch 和 lazy 主要是用來級聯查詢的,?? 而 cascade 和 inverse 主要是用來級聯插入和修改的 結論4: 如果你是用spring來幫你管理你的session, 并且是自動提交,延遲加載就等于沒加載~_~(當然除非你手動重新打開session然后手動Hibernate.initialize(set);然后關閉session. 結論5:? cascade主要是簡化了在代碼中的級聯更新和刪除。 j結論6:老爸可以有多個孩子,一個孩子不能有多個老爸,而且老爸說的算, 孩子圍著老爸轉。所以Photos老爸要有權力所以 cascade?這個關鍵子都是送給老爸的,?也就是級聯更新,老爸改姓了,兒子也得跟著改,呵呵。“不然,就沒有零花錢咯”。而Picture兒子整體挨罵,但是還是要維護父子之間良好的關系,對老爸百依百順,所以老爸就說,兒子,“關系,由你來維護(inverse="true")?,不然就不給零花錢。呵。”。 ?????????????? <set name="pictures" inverse="true" cascade="all"> ??????????????????? <key> ?????????????????????? <column name="photosid" not-null="true" /> ??????????????????? </key> ???????????????? <one-to-many class="girl.domain.Picture" /> ???????? ??? </set> ??????????????? 測試代碼:
???Photos p = ps.getById(1); ??Set<Picture> set = p.getPictures(); ??for(Picture pic : set){ ? ???System.out.println(pic.getId()); ??}
? 配置文件的一部分: ?????? <set name="pictures" inverse="true" cascade="all" > ??????????? <key> ??????????????? <column name="photosid" not-null="true" /> ??????????? </key> ??????????? <one-to-many class="girl.domain.Picture" /> ??????? </set>
測試過程會對配置文件不斷修改:并且從來不曾手動重新打開session
測試結構:
當配置條件為 lazy=true一句查詢 測試代碼中沒有調用getPicture() ?正常 Hibernate:
select photos0_.id as id0_0_, photos0_.userid as userid0_0_,
photos0_.typeid as typeid0_0_, photos0_.name as name0_0_,
photos0_.createtime as createtime0_0_, photos0_.description as
descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_
from super.photos photos0_ where photos0_.id=? lazy=true 一句查詢 有getPicture() Hibernate: select photos0_.id as
id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_,
photos0_.name as name0_0_, photos0_.createtime as createtime0_0_,
photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_,
photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=? lazy=true 一句查詢? 有getPicture() 并且訪問了里面的元數Picture 且有異常拋出 Hibernate:
select photos0_.id as id0_0_, photos0_.userid as userid0_0_,
photos0_.typeid as typeid0_0_, photos0_.name as name0_0_,
photos0_.createtime as createtime0_0_, photos0_.description as
descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_
from super.photos photos0_ where photos0_.id=?
lazy="false" 兩句查詢? 肯定沒問題,因為全部數據都個查了出來 所以怎么調用都正常 Hibernate:
select photos0_.id as id0_0_, photos0_.userid as userid0_0_,
photos0_.typeid as typeid0_0_, photos0_.name as name0_0_,
photos0_.createtime as createtime0_0_, photos0_.description as
descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_
from super.photos photos0_ where photos0_.id=? Hibernate: select
pictures0_.photosid as photosid1_, pictures0_.id as id1_, pictures0_.id
as id2_0_, pictures0_.photosid as photosid2_0_, pictures0_.name as
name2_0_, pictures0_.clicked as clicked2_0_, pictures0_.uploaddate as
uploaddate2_0_, pictures0_.size as size2_0_, pictures0_.description as
descript7_2_0_, pictures0_.uri as uri2_0_ from super.picture pictures0_
where pictures0_.photosid=?
fetch="join"? 一句查詢? 效果 == lazy="false" 呵呵,哪個效率高,我就不知道了。。。。。。。。。。。 Hibernate:
select photos0_.id as id0_1_, photos0_.userid as userid0_1_,
photos0_.typeid as typeid0_1_, photos0_.name as name0_1_,
photos0_.createtime as createtime0_1_, photos0_.description as
descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_,
pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id
as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as
name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as
uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as
descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_
left outer join super.picture pictures1_ on
photos0_.id=pictures1_.photosid where photos0_.id=?
不加fetch="join"一句查詢? 沒有getPicture() 正常 Hibernate: select
photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid
as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as
createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid
as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where
photos0_.id=? 不加fetch="join" 一句查詢? 有getPicture() 正常 Hibernate: select
photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid
as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as
createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid
as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where
photos0_.id=? 不加fetch="join"一句查詢 有getPicture() 并且訪問里面的元素Picture的ID 有異常拋出 Hibernate:
select photos0_.id as id0_0_, photos0_.userid as userid0_0_,
photos0_.typeid as typeid0_0_, photos0_.name as name0_0_,
photos0_.createtime as createtime0_0_, photos0_.description as
descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_
from super.photos photos0_ where photos0_.id=? 來個兩兵交戰 fetch="join" lazy="true"? 呵呵 結果,一句查詢, 結構正常 所以就當lazy不存在好了。 看來fetch 是老大。、、、、、、、、、、、、、 Hibernate:
select photos0_.id as id0_1_, photos0_.userid as userid0_1_,
photos0_.typeid as typeid0_1_, photos0_.name as name0_1_,
photos0_.createtime as createtime0_1_, photos0_.description as
descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_,
pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id
as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as
name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as
uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as
descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_
left outer join super.picture pictures1_ on
photos0_.id=pictures1_.photosid where photos0_.id=?
參考引用: 使用說明: lib添加dwr.jar web.xml添加 <?xml?version="1.0"?encoding="ISO-8859-1"?> <!DOCTYPE?web-app?PUBLIC ????"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN" ????"http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app?id="dwr"> ??<display-name>DWR?(Direct?Web?Remoting)</display-name> ??<description>A?Simple?Demo?DWR</description>
??<servlet> ????<servlet-name>dwr-invoker</servlet-name> ????<display-name>DWR?Servlet</display-name> ????<description>Direct?Web?Remoter?Servlet</description> ????<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
????<!--?This?should?NEVER?be?present?in?live?--> ????<init-param> ??????<param-name>debug</param-name> ??????<param-value>true</param-value> ????</init-param>
??</servlet> ? <!-- 服務起來后 在地址中直接輸入 http://.../dwr 就可以查看對外提供的服務類 --> ??<servlet-mapping> ????<servlet-name>dwr-invoker</servlet-name> ????<url-pattern>/dwr/*</url-pattern> ??</servlet-mapping>
</web-app>
dwr.xml <?xml?version="1.0"?encoding="UTF-8"?> <!DOCTYPE?dwr?PUBLIC ????"-//GetAhead?Limited//DTD?Direct?Web?Remoting?1.0//EN" ????"http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> ????<allow> ????????<!--?遠程調用類?定義 updateItem,getItem?和?findItems?方法可用??--> ????????<create?creator="new"?javascript="CatalogDAO"> ????????????<param?name="class"?value="com.ajaxlab.ajax.CatalogDAO"?/> ????????????<include?method="getItem"?/> ????????????<include?method="findItems"?/> ??????????? <include?method="updateItem"?/> ????????</create> ???????? ????????<!--?數據展現定義格式?(javascriot?json?格式)? ????????????此定義?id?name?description?為可見? ??????????????????price?隱藏? ????????????E.g?->? ????????????????{ ??????????????????description:"中國制造.",? ??????????????????id:"產品-",? ??????????????????name:"新品-" ????????????????} ????????--> ????????<convert?converter="bean"?match="com.ajaxlab.ajax.Item"> ????????????<param?name="include" ????????????????value="id,name,description,formatted-?Price"?/> ????????</convert> ????</allow> </dwr>
類說明 bean Item: ? private String id = ""; ? private String name = ""; ? private String description = ""; ? private int price = 0; ? get();set(); dao ????public?Item?getItem(String?id)?{ ????????Item?item?=?new?Item("產品-"+id); ????????item.setName("新品-"+id); ????????item.setPrice(100); ????????item.setDescription("中國制造."); ????????return?item; ????}
????public?List?findItems(String?expression)?{ ????????List?list?=?new?ArrayList(); ????????Item?item1?=?new?Item("產品-001"); ????????item1.setName("新品-001"); ????????item1.setDescription(expression); ????????item1.setPrice(10); ????????Item?item2?=?new?Item("產品-002"); ????????item2.setName("新品-002"); ????????item2.setDescription(expression); ????????item2.setPrice(15); ????????Item?item3?=?new?Item("產品-003"); ????????item3.setName("新品-003"); ????????item3.setDescription(expression); ????????item3.setPrice(35); ????????list.add(item1); ????????list.add(item2); ????????list.add(item3); ????????return?list;
????}
?? /* html input text -> ??????? { ??? ??? ??? ? description:"中國制造.", ??? ??? ??? ? id:"1", ??? ??? ??? ? name:"劉凱毅" ??? ??? } ??? ??? return true ; ??? ??? ???? */ ??? public boolean updateItem(Item item ){ ??? ??? if( item!=null && item.getName().equals("劉凱毅") ) ??? ??? ??? return true; ??? ??? ??? ??? return false ; ??? }
js 方法: 在 input 輸入->> method( !! )
bean(id,name...) {id:1,name:'liukaiyi'}
當多參數 為 map {dd:'liu',aa:'gg'}
js多參數 class.method( ??? objectEval($("p00").value), ??? objectEval($("p01").value), reply0);展現頁: <html> <head> ??<title>DWR?Test</title> ??<!--?These?paths?use?..?so?that?they?still?work?behind?a?path?mapping?proxy.?The?fully?qualified?version?is?more?cut?and?paste?friendly.?--> ??<script?type='text/javascript'?src='/testDwr/dwr/interface/CatalogDAO.js'></script> ??<script?type='text/javascript'?src='/testDwr/dwr/engine.js'></script> ??<script?type='text/javascript'?src='/testDwr/dwr/util.js'></script> ?? ??<script?type='text/javascript'> ? //輸入到方法中 參數 格式轉換 ??function?objectEval(text){ ????text?=?text.replace(/\n/g,?'?'); ????text?=?text.replace(/\r/g,?'?'); ????if?(text.match(/^\s*\{.*\}\s*$/)) ????{ ??????text?=?'['?+?text?+?'][0]'; ????} ????return?eval(text); ??} ? ? //本例 alert 展現 ??var?reply?=?function(data){ ??????alert(dwr.util.toDescriptiveString(data,?2)); ??}
??</script>
</head> <body?>
<li> ??findItems(????<input??type='text'??value='""'?id='p00'?/>??); ??<input?class='ibutton'?type='button'?onclick='CatalogDAO.findItems(objectEval($("p00").value),?reply);'?value='Execute'??/>
</li> <li> ??getItem(????<input?class='itext'?type='text'?size='10'?value='""'?id='p10'?title='Will?be?converted?to:?java.lang.String'/>??); ??<input?class='ibutton'?type='button'?onclick='CatalogDAO.getItem(objectEval($("p10").value),?reply);'?value='Execute'??title='Calls?CatalogDAO.getItem().?View?source?for?details.'/> </li>
<li> ? updateItem(???<input?class='itext'?type='text'?size='10'?value='{}'?id='p20'/>? ); ? <input class='ibutton' type='button' onclick='CatalogDAO.updateItem(objectEval($("p20").value), reply);' value='Execute' /> </li>
</body></html>
|