posts - 167,  comments - 30,  trackbacks - 0

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 阅读(450) | 评论 (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 阅读(293) | 评论 (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 阅读(195) | 评论 (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 阅读(236) | 评论 (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 阅读(292) | 评论 (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 阅读(277) | 评论 (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 阅读(3864) | 评论 (0)编辑 收藏
     摘要: province_code-----Char(2) --》Character cert-----Clob 类型 ---》SerializableClob certstate------Number类型 --》BigDecimal    usercode------Varchar2类型 ---》String addtime ---...  阅读全文
posted @ 2009-08-31 16:26 David1228 阅读(487) | 评论 (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 阅读(828) | 评论 (0)编辑 收藏
仅列出标题
共16页: First 上一页 8 9 10 11 12 13 14 15 16 下一页 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(4)

随笔分类

随笔档案

文章档案

新闻分类

新闻档案

相册

收藏夹

Java

Linux知识相关

Spring相关

云计算/Linux/虚拟化技术/

友情博客

多线程并发编程

开源技术

持久层技术相关

搜索

  •  

积分与排名

  • 积分 - 354659
  • 排名 - 155

最新评论

阅读排行榜

评论排行榜