Abstract
在開發中,如果某個實例的創建需要消耗很多系統資源,那么我們通常會使用惰性加載機制,也就是說只有當使用到這個實例的時候才會創建這個實例,這個好處在單例模式中得到了廣泛應用。這個機制在single-threaded環境下的實現非常簡單,然而在multi-threaded環境下卻存在隱患。本文重點介紹惰性加載機制以及其在多線程環境下的使用方法。(作者numberzero,參考IBM文章《Double-checked locking and the Singleton pattern》,歡迎轉載與討論)
1 單例模式的惰性加載
通常當我們設計一個單例類的時候,會在類的內部構造這個類(通過構造函數,或者在定義處直接創建),并對外提供一個static getInstance方法提供獲取該單例對象的途徑。例如:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20Singleton%20%20%20%20%20%20%0A%7B%20%20%20%20%20%20%0A%20%20%20%20private%20static%20Singleton%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%0A%20%20%20%20private%20Singleton()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%E2%80%A6%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20public%20static%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20return%20instance%3B%20%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%7D%20%20%20%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public class Singleton
{
private static Singleton instance = new Singleton();
private Singleton(){
…
}
public static Singleton getInstance(){
return instance;
}
}
public class Singleton
{
private static Singleton instance = new Singleton();
private Singleton(){
…
}
public static Singleton getInstance(){
return instance;
}
}
這樣的代碼缺點是:第一次加載類的時候會連帶著創建Singleton實例,這樣的結果與我們所期望的不同,因為創建實例的時候可能并不是我們需要這個實例的時候。同時如果這個Singleton實例的創建非常消耗系統資源,而應用始終都沒有使用Singleton實例,那么創建Singleton消耗的系統資源就被白白浪費了。
為了避免這種情況,我們通常使用惰性加載的機制,也就是在使用的時候才去創建。以上代碼的惰性加載代碼如下:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20Singleton%7B%20%20%20%20%20%20%0A%20%20%20%20private%20static%20Singleton%20instance%20%3D%20null%3B%20%20%20%20%20%20%0A%20%20%20%20private%20Singleton()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%E2%80%A6%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20public%20static%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(instance%20%3D%3D%20null)%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20instance%3B%20%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%7D%20%20%20%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public class Singleton{
private static Singleton instance = null ;
private Singleton(){
…
}
public static Singleton getInstance(){
if (instance == null )
instance = new Singleton();
return instance;
}
}
public class Singleton{
private static Singleton instance = null;
private Singleton(){
…
}
public static Singleton getInstance(){
if (instance == null)
instance = new Singleton();
return instance;
}
}
這樣,當我們第一次調用Singleton.getInstance()的時候,這個單例才被創建,而以后再次調用的時候僅僅返回這個單例就可以了。
2 惰性加載在多線程中的問題
先將惰性加載的代碼提取出來:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20static%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20if%20(instance%20%3D%3D%20null)%20%20%20%20%20%20%0A%20%20%20%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%20%0A%20%20%20%20return%20instance%3B%20%20%20%20%20%20%20%0A%7D%20%20%20%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public static Singleton getInstance(){
if (instance == null )
instance = new Singleton();
return instance;
}
public static Singleton getInstance(){
if (instance == null)
instance = new Singleton();
return instance;
} 這是如果兩個線程A和B同時執行了該方法,然后以如下方式執行:
1. A進入if判斷,此時foo為null,因此進入if內
2. B進入if判斷,此時A還沒有創建foo,因此foo也為null,因此B也進入if內
3. A創建了一個Foo并返回
4. B也創建了一個Foo并返回
此時問題出現了,我們的單例被創建了兩次,而這并不是我們所期望的。
3 各種解決方案及其存在的問題
3.1 使用Class鎖機制
以上問題最直觀的解決辦法就是給getInstance方法加上一個synchronize前綴,這樣每次只允許一個現成調用getInstance方法:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20static%20synchronized%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20if%20(instance%20%3D%3D%20null)%20%20%20%20%20%20%0A%20%20%20%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%20%0A%20%20%20%20return%20instance%3B%20%20%20%20%20%20%20%0A%7D%20%20%20%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public static synchronized Singleton getInstance(){
if (instance == null )
instance = new Singleton();
return instance;
}
public static synchronized Singleton getInstance(){
if (instance == null)
instance = new Singleton();
return instance;
}
這種解決辦法的確可以防止錯誤的出現,但是它卻很影響性能:每次調用getInstance方法的時候都必須獲得Singleton的鎖,而實際上,當單例實例被創建以后,其后的請求沒有必要再使用互斥機制了
3.2 double-checked locking
曾經有人為了解決以上問題,提出了double-checked locking的解決方案
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20static%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20if%20(instance%20%3D%3D%20null)%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20synchronized(instance)%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if(instance%20%3D%3D%20null)%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20return%20instance%3B%20%20%20%20%20%20%20%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public static Singleton getInstance(){
if (instance == null )
synchronized (instance){
if (instance == null )
instance = new Singleton();
}
return instance;
}
public static Singleton getInstance(){
if (instance == null)
synchronized(instance){
if(instance == null)
instance = new Singleton();
}
return instance;
} 讓我們來看一下這個代碼是如何工作的:首先當一個線程發出請求后,會先檢查instance是否為null,如果不是則直接返回其內容,這樣避免了進入 synchronized塊所需要花費的資源。其次,即使第2節提到的情況發生了,兩個線程同時進入了第一個if判斷,那么他們也必須按照順序執行 synchronized塊中的代碼,第一個進入代碼塊的線程會創建一個新的Singleton實例,而后續的線程則因為無法通過if判斷,而不會創建多余的實例。
上述描述似乎已經解決了我們面臨的所有問題,但實際上,從JVM的角度講,這些代碼仍然可能發生錯誤。
對于JVM而言,它執行的是一個個Java指令。在Java指令中創建對象和賦值操作是分開進行的,也就是說instance = new Singleton();語句是分兩步執行的。但是JVM并不保證這兩個操作的先后順序,也就是說有可能JVM會為新的Singleton實例分配空間,然后直接賦值給instance成員,然后再去初始化這個Singleton實例。這樣就使出錯成為了可能,我們仍然以A、B兩個線程為例:
1. A、B線程同時進入了第一個if判斷
2. A首先進入synchronized塊,由于instance為null,所以它執行instance = new Singleton();
3. 由于JVM內部的優化機制,JVM先畫出了一些分配給Singleton實例的空白內存,并賦值給instance成員(注意此時JVM沒有開始初始化這個實例),然后A離開了synchronized塊。
4. B進入synchronized塊,由于instance此時不是null,因此它馬上離開了synchronized塊并將結果返回給調用該方法的程序。
5. 此時B線程打算使用Singleton實例,卻發現它沒有被初始化,于是錯誤發生了。
4 通過內部類實現多線程環境中的單例模式
為了實現慢加載,并且不希望每次調用getInstance時都必須互斥執行,最好并且最方便的解決辦法如下:
Java代碼 < type="application/x-shockwave-flash" width="14" height="15" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://xupo.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20Singleton%7B%20%20%20%20%20%20%0A%20%20%20%20private%20Singleton()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%E2%80%A6%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20private%20static%20class%20SingletonContainer%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20private%20static%20Singleton%20instance%20%3D%20new%20Singleton()%3B%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20public%20static%20Singleton%20getInstance()%7B%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20SingletonContainer.instance%3B%20%20%20%20%20%20%0A%20%20%20%20%7D%20%20%20%20%20%20%0A%7D%20%20%20%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">
public class Singleton{
private Singleton(){
…
}
private static class SingletonContainer{
private static Singleton instance = new Singleton();
}
public static Singleton getInstance(){
return SingletonContainer.instance;
}
}
public class Singleton{
private Singleton(){
…
}
private static class SingletonContainer{
private static Singleton instance = new Singleton();
}
public static Singleton getInstance(){
return SingletonContainer.instance;
}
}
JVM內部的機制能夠保證當一個類被加載的時候,這個類的加載過程是線程互斥的。這樣當我們第一次調用getInstance的時候,JVM能夠幫我們保證instance只被創建一次,并且會保證把賦值給instance的內存初始化完畢,這樣我們就不用擔心3.2中的問題。此外該方法也只會在第一次調用的時候使用互斥機制,這樣就解決了3.1中的低效問題。最后instance是在第一次加載SingletonContainer類時被創建的,而 SingletonContainer類則在調用getInstance方法的時候才會被加載,因此也實現了惰性加載。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/fancyerII/archive/2010/03/15/5382349.aspx
posted @
2010-05-14 14:17 David1228 閱讀(468) |
評論 (0) |
編輯 收藏
通過dos窗口 進入到Oracle安裝bin目錄下
輸入導出命令: exp
system/system@mis file="d:\backup\2010-1-5.dmp" 導出的是dmp文件
導入命令: imp
rmp/rmp@mis fromuser="system" touser="rmp" file="d:\backup\2010-1-5.dmp"
通過pl/sql 選擇Tools--->Export Table --->選中要導出的表--->選擇SQL Inserts 選擇Drop tables 選擇導出的目錄 --點擊Export 導出的文件data.sql
導入的時候通過選擇Command Window 然后在顯示的窗口下輸入@后點擊回車鍵(Enter) --》選擇導出的data.sql ---->執行中,,,,OK
posted @
2010-01-05 18:12 David1228 閱讀(316) |
評論 (0) |
編輯 收藏
基本介紹:
showModalDialog() (IE 4+ 支持)
showModelessDialog() (IE 5+ 支持)
window.showModalDialog() 方法用來創建一個顯示HTML內容的模態對話框。
window.showModelessDialog() 方法用來創建一個顯示HTML內容的非模態對話框。
使用方法:
vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])
vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures])
參數說明:
sURL -- 必選參數,類型:字符串。用來指定對話框要顯示的文檔的URL。
vArguments -- 可選參數,類型:變體。用來向對話框傳遞參數。傳遞的參數類型不限,包括數組等。對話框通過window.dialogArguments來取得傳遞進來的參數。
sFeatures -- 可選參數,類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
----------------
1. dialogHeight: 對話框高度,不小于100px
2. dialogWidth: 對話框寬度。
3. dialogLeft: 離屏幕左的距離。
4. dialogTop: 離屏幕上的距離。
5. center: { yes | no | 1 | 0 } : 是否居中,默認yes,但仍可以指定高度和寬度。
6. help: {yes | no | 1 | 0 }: 是否顯示幫助按鈕,默認yes。
7. resizable: {yes | no | 1 | 0 } [IE5+]: 是否可被改變大小。默認no。
8. status: {yes | no | 1 | 0 } [IE5+]: 是否顯示狀態欄。默認為yes[ Modeless]或no[Modal]。
9. scroll: { yes | no | 1 | 0 | on | off }:是否顯示滾動條。默認為yes。
下面幾個屬性是用在HTA中的,在一般的網頁中一般不使用。
10. dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印預覽時對話框是否隱藏。默認為no。
11. edge:{ sunken | raised }:指明對話框的邊框樣式。默認為raised。
12. unadorned:{ yes | no | 1 | 0 | on | off }:默認為no。
參數傳遞:
1. 要想對話框傳遞參數,是通過vArguments來進行傳遞的。類型不限制,對于字符串類型,最大為4096個字符。也可以傳遞對象,例如:
-------------------------------
parent.htm
<script>
var obj = new Object();
obj.name="51js";
window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px");
</script>
modal.htm
<script>
var obj = window.dialogArguments
alert("您傳遞的參數為:" + obj.name)
</script>
-------------------------------
2. 可以通過window.returnValue向打開對話框的窗口返回信息,當然也可以是對象。例如:
------------------------------
parent.htm
<script>
str =window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px");
alert(str);
</script>
modal.htm
<script>
window.returnValue="http://homepage.yesky.com";
posted @
2009-12-15 11:36 David1228 閱讀(215) |
評論 (0) |
編輯 收藏
<script type="text/javascript">
function btn_change_click()

{
var url = "<%=request.getContextPath()%>/rmp/reqchange/beforeChangeOrder.do";
var inputChecked = document.getElements("input[name='ckids']").filterByAttribute("checked","=",true);
if(inputChecked.length>1)

{
alert("每次只能操作一條記錄");
return;
}
if(inputChecked.length<1)

{
alert("請選擇一條記錄");
return;
}
window.location =url + "?orderId=" + inputChecked[0].value;
}
</script>


<input type="button" class="button_1" id="button4" value=" 項目變更 " onclick="btn_change_click()">

<input type="checkbox" name="ckids" value="111"/>
<input type="checkbox" name="ckids" value="222"/>
posted @
2009-12-09 16:37 David1228 閱讀(254) |
評論 (0) |
編輯 收藏
對于梅花雪樹選中節點的刪除:級聯刪除該節點下所有子孫節點
private List processGetSysFunPointList(String id, TReqSysDTO sysDTO)

throws ParseException
{
List rslist = new ArrayList();
//思路:
//1,找出當前節點
//2,找出當前節點的所有直接子節點
//3,分別找出每個直接子節點的直接子節點
//4,重復第3步操作,直至找出當前節點的所有子節點
//算法:
//1,先找出當前所有最新版本的節點,并封裝成Map[pid,vo]數據結構,以提高算法查找效率。
// 注: 1)封裝時遍歷采用while循環結構以提高循環效率。
// 2)封裝時對頂級節點,即父節點為空的節點,不進行封裝處理。
// 3)封裝結構的結果是:所有的非葉子節點的ID都包含map的keySet集合中;
//2,根據上面的思路寫遞歸算法來求解當前節點的所有子節點
Map map = new HashMap();
List list = this.myReqSystemFunctionPointDAO.findAll(sysDTO);

if(list!=null)
{
Iterator itr = list.iterator();

while(itr.hasNext())
{
SysFunTreeNodeVO vo = (SysFunTreeNodeVO)itr.next();
//找出當前當前節點

if(id.equals(vo.getId()))
{
rslist.add(vo);
}
//封裝Map[pid,vo]數據結構,供遞歸算法使用
String pid = vo.getParentSysFunPointId();

if(pid!=null && !pid.equals(""))
{//過濾頂級節點,頂級節點無需封裝進map結構

if(map.containsKey(pid))
{
((List)map.get(pid)).add(vo);
}

else
{
List tmp = new ArrayList();
tmp.add(vo);
map.put(pid, tmp);
}
}
}
}

//遞歸算法,找出當前節點的所有子節點
List sons = findSons(id, map);

if(sons!=null)
{
rslist.addAll(sons);//添加子節點
}
return rslist;
}


public List findSons(String id,Map srcmap)
{
List rslist = new ArrayList();

if(id!=null && srcmap!=null && srcmap.containsKey(id))
{
//找出id的直接子節點。注:不是id的所有子節點,而是其一級子節點
List sons = (List) srcmap.get(id);

if(sons!=null)
{
rslist.addAll(sons);//添加直接子節點
Iterator itr = sons.iterator();

while(itr.hasNext())
{//遍歷添加直接子節點的所有子節點
SysFunTreeNodeVO vo = (SysFunTreeNodeVO) itr.next();
List sonslist = findSons(vo.getId(), srcmap);
rslist.addAll(sonslist);
}
}
}
return rslist;
}
//封裝成逗號隔開的串保存
String fids = "";
if(filesIdList !=null && filesIdList.size() > 0)
{
for(int i =0;i<filesIdList.size();i++)
{
fids = fids + filesIdList.get(i);
if(filesIdList.size() >1 && i!=filesIdList.size()-1)
{
fids = fids + ",";
}
}
}
posted @
2009-12-04 10:50 David1228 閱讀(312) |
評論 (0) |
編輯 收藏
sql語句:如何統計一列中的值重復出現的次數,查詢出的結果按次數的倒序排
解決了
select * from (select col,count(col) as c from table group by col) as t order by t.c
本來的目標是 MAX(...) ,結果 這么一句 HQL 語句總是得不到結構:
string query = "SELECT MAX(ID) FROM Student";
另外兩句:"SELECT MAX(Student.ID) FROM Student " 和 "SELECT MAX(ID) FROM Student AS stud";
同樣得不到結果,返回的的均是 Student 對象集合
剛開始以為不支持 MAX 函數,但是SELECT COUNT() FROM Student ,就可以正確返回紀律數,而且很多文檔也多證明了HQL支持ANSI SQL集合函數,
終于發現 只有 "SELECT MAX(stud.ID) FROM Student AS stud";
當然 "SELECT MAX(stud.ID) FROM Student stud" 也是正確的
轉載請標明出處:http://blog.csdn.net/Jinglecat/archive/2005/08/03/445296.aspx
posted @
2009-11-25 17:58 David1228 閱讀(298) |
評論 (0) |
編輯 收藏
select t1.id,t1.parent_sys_fun_point_id,t1.sys_fun_point_type,t1.sys_fun_point_state,t2.sys_fun_point_ver,
t4.sys_fun_point_name ,t2.create_time,t4.sys_fun_point_code,t4.sys_fun_point_desc,t2.sys_fun_point_info_id,t1.sys_fun_point_layer
from req_system_function_point t1,
(
select distinct t.sys_fun_point_id,t.sys_fun_point_ver,t.sys_fun_point_info_id,t.create_time,t.sys_ver
from req_sys_fun_point_version t
where t.sys_ver <= (select tt.cur_sys_ver from req_sys tt)) t2,
req_sys_fun_point_info t4
where t1.parent_sys_fun_point_id is not null
and t1.id = t2.sys_fun_point_id
and t4.id=t2.sys_fun_point_info_id
and t1.sys_fun_point_state = 0
and
t2.sys_ver=(select max(t3.sys_ver) from req_sys_fun_point_version t3 where t1.id=t3.sys_fun_point_id );
posted @
2009-11-25 14:58 David1228 閱讀(3886) |
評論 (0) |
編輯 收藏
摘要: province_code-----Char(2) --》Character
cert-----Clob 類型 ---》SerializableClob
certstate------Number類型 --》BigDecimal
usercode------Varchar2類型 ---》String
addtime ---...
閱讀全文
posted @
2009-08-31 16:26 David1228 閱讀(504) |
評論 (0) |
編輯 收藏
摘要: 1<%@ page language="java" pageEncoding="GBK"%>
2<%@page import="java.util.List"%>
3<%@page import="com.cns.certservice.vo....
閱讀全文
posted @
2009-08-28 14:19 David1228|
編輯 收藏
public List<CertInfoListView> getInfoList(CertInfoListView view) throws DAOException {
List<CertInfoListView> liinfo = new ArrayList<CertInfoListView>();
String sql="select b.usercode,b.agentcode,i.sn,i.cert,i.certstate,i.endtime, i.ipasskeyno,b.id,b.addtime from certbind b inner join certinfo i on b.ipasskeyno=i.ipasskeyno";
Session session = HibernateTemplate.getInstance().getSession();
StringBuffer sb = new StringBuffer(sql);
sb.append(" where 1=1 ");
if(!CheckEmpty.isEmpty(view.getIpasskeyno())){
sb.append(" and i.ipasskeyno ='").append(view.getIpasskeyno()).append("'");
}
if(!CheckEmpty.isEmpty(view.getAgentcode())){
sb.append(" and b.agentcode ='").append(view.getAgentcode()).append("'");
}
if(!CheckEmpty.isEmpty(view.getSn())){
sb.append(" and i.sn ='").append(view.getSn()).append("'");
}
if(!CheckEmpty.isEmpty(view.getUsercode())){
sb.append(" and b.usercode ='").append(view.getUsercode()).append("'");
}
if(view.getAddtime()!=null){
sb.append(" and b.addtime=").append(view.getAddtime());
}
sb.append(" order by ipasskeyno ,addtime desc");
Query q = session.createSQLQuery(sb.toString());
int pageno = view.getPageno();
int size = view.getPagesize();
if(pageno!=0&&size!=0){
q.setFirstResult((pageno-1)*size);
q.setMaxResults(size);
}
List list = q.list();
Iterator it = list.iterator();
while(it.hasNext()){
Object[] obj = (Object[])it.next();
CertInfoListView c = new CertInfoListView();
for(int i=0;i<obj.length;i++){
if(!CheckEmpty.isEmpty((String)obj[0])){
c.setUsercode((String)obj[0]);
}
if(!CheckEmpty.isEmpty((String)obj[1])){
c.setAgentcode((String)obj[1]);
}
if(!CheckEmpty.isEmpty((String)obj[2])){
c.setSn((String)obj[2]);
}
if(obj[3]!=null){
SerializableClob sc = (SerializableClob)obj[3];
String cc = null;
if(sc!=null){
try {
cc = sc.getSubString(1, (int)sc.length());
} catch (SQLException e) {
}
}
if(cc!=null)c.setCert(cc);
}
if(obj[4]!=null){
BigDecimal b = (BigDecimal)obj[4];
c.setCertstate(b.intValue());
}
if((obj[5])!=null){
c.setEndtime(((Date)obj[5]));
}
if(!CheckEmpty.isEmpty((String)obj[6])){
c.setIpasskeyno((String)obj[6]);
}
c.setCertbindid(Integer.parseInt((String)obj[7]));
c.setAddtime((Date)obj[8]);
}
liinfo.add(c);
}
if(session!=null&&session.isConnected())session.close();
return liinfo;
}
實現分頁的DAO
/*
* (non-Javadoc)
*
* @see com.cns.certservice.dao.CertBindTabDao#getCertBindList(com.cns.certservice.vo.CertBindView)
*/
public List<CertBindView> getCertBindList(CertBindView view)
throws DAOException {
List<CertBindView> li = null;
List<Certbind> lic = null;
Session session = null;
try {
session = HibernateTemplate.getInstance().getSession();
Criteria cri = session.createCriteria(Certbind.class);
Certbind c = getCertBind(view);
if (c.getAddtime() != null) {
cri.add(Expression.eq("addtime", c.getAddtime()));
}
if (!CheckEmpty.isEmpty(c.getAgentcode())) {
cri.add(Expression.eq("agentcode", c.getAgentcode()));
}
if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
}
if (!CheckEmpty.isEmpty(c.getUsercode())) {
cri.add(Expression.eq("usercode", c.getUsercode()));
}
if (!CheckEmpty.isEmpty(c.getExtend1())) {
cri.add(Expression.eq("extend1", c.getExtend1()));
}
if (!CheckEmpty.isEmpty(c.getExtend2())) {
cri.add(Expression.eq("extend2", c.getExtend2()));
}
if (c.getAgenttype() != 0) {
cri.add(Expression.eq("agenttype", c.getAgenttype()));
}
int pageno = view.getPageno();
int size = view.getPagesize();
if(pageno!=0&&size!=0){
cri.setFirstResult((pageno-1)*size);
cri.setMaxResults(size);
}
lic = cri.list();
if (lic != null) {
li = new ArrayList<CertBindView>();
for (Certbind cer : lic) {
CertBindView v = getCertBindView(cer);
li.add(v);
}
}
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return li;
}
public int getInfoListCount(CertBindView view) throws DAOException {
int count = 0;
Session session = null;
try {
session = HibernateTemplate.getInstance().getSession();
Criteria cri = session.createCriteria(Certbind.class);
Certbind c = getCertBind(view);
if (c.getAddtime() != null) {
cri.add(Expression.eq("addtime", c.getAddtime()));
}
if (!CheckEmpty.isEmpty(c.getAgentcode())) {
cri.add(Expression.eq("agentcode", c.getAgentcode()));
}
if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
}
if (!CheckEmpty.isEmpty(c.getUsercode())) {
cri.add(Expression.eq("usercode", c.getUsercode()));
}
if (!CheckEmpty.isEmpty(c.getExtend1())) {
cri.add(Expression.eq("extend1", c.getExtend1()));
}
if (!CheckEmpty.isEmpty(c.getExtend2())) {
cri.add(Expression.eq("extend2", c.getExtend2()));
}
if (c.getAgenttype() != 0) {
cri.add(Expression.eq("agenttype", c.getAgenttype()));
}
count = (Integer)cri.setProjection(Projections.rowCount()).uniqueResult();
cri.setProjection(null);
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return count;
}
/*
* (non-Javadoc)
*
* @see com.cns.certservice.dao.CertBindTabDao#updateCertBind(com.cns.certservice.vo.CertBindView)
*/
public boolean updateCertBind(CertBindView view) throws DAOException {
boolean res = false;
if (view.getId()==0) {
throw new DAOException("getId from CertBindView is null!");
}
Session session= null;
try {
session = HibernateTemplate.getInstance().getSession();
Certbind c = (Certbind) session.get(Certbind.class, view.getId());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
res = HibernateTemplate.getInstance().updateObject(c);
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return res;
}
private Certbind getCertBind(CertBindView view) {
Certbind c = new Certbind();
if (view.getAddtime() != null)
c.setAddtime(view.getAddtime());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if(view.getId()!=0)c.setId(view.getId());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
return c;
}
private CertBindView getCertBindView(Certbind view) {
CertBindView c = new CertBindView();
if (view.getAddtime() != null)
c.setAddtime(view.getAddtime());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if(view.getId()!=0)c.setId(view.getId());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
return c;
}
posted @
2009-08-20 13:37 David1228 閱讀(853) |
評論 (0) |
編輯 收藏