??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美成a人免费观看久久,中文字幕人成一区,九色porny丨首页在线http://www.aygfsteel.com/ldwblog/category/41076.html态度军_一?/description>zh-cnTue, 07 Jan 2020 10:49:39 GMTTue, 07 Jan 2020 10:49:39 GMT60个h公众受Java爱好者社区」高质量原创文章持箋输出Q欢q各位小d?/title><link>http://www.aygfsteel.com/ldwblog/archive/2019/10/30/434887.html</link><dc:creator>David1228</dc:creator><author>David1228</author><pubDate>Wed, 30 Oct 2019 06:10:00 GMT</pubDate><guid>http://www.aygfsteel.com/ldwblog/archive/2019/10/30/434887.html</guid><wfw:comment>http://www.aygfsteel.com/ldwblog/comments/434887.html</wfw:comment><comments>http://www.aygfsteel.com/ldwblog/archive/2019/10/30/434887.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ldwblog/comments/commentRss/434887.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ldwblog/services/trackbacks/434887.html</trackback:ping><description><![CDATA[blogjava|站与博客园合ƈ了,对blogjavaq是有感情的?br />q且blogjava|站发布的文章是在博客园首页看不到的?br />blogjava|站首页能看刎ͼ你可以点L题进入可以看到图片二l码信息?br /><br /><strong style="color: red; font-size: 14pt;">Java爱好者社?/strong><br />公众号内容简介:<br /><p><span style="background-color: yellow; color: red;">专注于分享Java后端相关技术、老司机实战干货,不限于JVM、ƈ发、设计模式、性能优化、分布式&微服务、云原生、大数据相关主题?/span><br style="color: red;" /><span style="background-color: yellow; color: red;">希望x的你停下脚步Q定有所收获?/span><br style="color: red;" /><br />以前的个人博客内容不好迁U,所以我打算Ҏ自己的多q经验,不断整理输出有h值的内容?br />目前公众号内Ҏ关于<span style="background-color: yellow; color: red;">SpringCloudQ微服务框架Q、SkywalkingQAPM监控调用链)、JVMQGC分析、内存泄漏分析)、ƈ发编E?/span>相关原创实战文章已出炉?br /><br />最q刚开始已l有不少伙伴关注了Q期待能有幸搜烦到本博客的同学,可以扫码x一下,不胜感激?br />大家有Q何技术、职场、面试上的问题都可以与我交流?br /><br /><span style="background-color: yellow;"><strong>方式一Q?/strong></span><span style="color: red; background-color: yellow;"><strong>扫码以下公众号二l码Q?/strong></span><br /><img src="http://www.aygfsteel.com/images/blogjava_net/ldwblog/Java%E7%88%B1%E5%A5%BD%E8%80%85%E7%A4%BE%E5%8C%BA%E4%BA%8C%E7%BB%B4%E7%A0%81.png" border="0" alt="" /><br /><br />方式二:<span style="color: red;">在微信上直接搜烦Q?nbsp;</span><span style="color: red; background-color: yellow;">javatech_cbo</span><br /><br />感谢各位伙伴的支持Q后l会在该公众号上输出大量的有价值的实战q货 Q期待与你一同进步与成长?br /></p><img src ="http://www.aygfsteel.com/ldwblog/aggbug/434887.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ldwblog/" target="_blank">David1228</a> 2019-10-30 14:10 <a href="http://www.aygfsteel.com/ldwblog/archive/2019/10/30/434887.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate的HQL多表联合查询使用left join方式http://www.aygfsteel.com/ldwblog/archive/2013/09/12/403983.htmlDavid1228David1228Thu, 12 Sep 2013 03:57:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2013/09/12/403983.htmlhttp://www.aygfsteel.com/ldwblog/comments/403983.htmlhttp://www.aygfsteel.com/ldwblog/archive/2013/09/12/403983.html#Feedback2http://www.aygfsteel.com/ldwblog/comments/commentRss/403983.htmlhttp://www.aygfsteel.com/ldwblog/services/trackbacks/403983.html解决办法Q用原生SQL或者用HQL方式需要修改hbm文g(如果目中大部分不适用U联配置情况?
需求是查询|络信息Q网l信息中兌了分区、网lIP(1个网l-QN个网lテQ?Q网桥信?
HQLQ?br />
public void queryVnets(Page page) {
        StringBuffer hql = new StringBuffer();
        hql.append("select new Map(vn.id as id, vn.vnName as vnName, b.name as vnType, ");
        hql.append("vn.vnIptype as vnIptype, vn.vnIp as vnIp,vn.vnNat as vnNat, vn.vnFlag as vnFlag, ");
        hql.append("vn.vnetFlag as vnetFlag, (case when sum(vmvn.state)='0' then 0 else 1 endas vnState, ");
        hql.append("vn.useType as useType, vn.createTime as createTime, ");
        hql .append("zone.oneName as oneName, zone.oneHypervisor as oneHypervisor, zone.oneSeq as oneSeq) ");
        hql.append("from VnetTable vn, ZoneTable zone, BridgeTable b ");
        hql.append("left join fetch vn.VnTables vmvn where zone.id = vn.zoneId and vn.vnType = b.id ");

        ... ...
        hql.append(" group by vn.id");
        if (vnState != null && !"".equals(vnState)) {
            if ("0".equals(vnState)) {
                hql.append(" having sum(vmvn.state)=0");
            } else if ("1".equals(vnState)) {
                hql.append(" having sum(vmvn.state)!=0");
            }
        }
    }

hbm配置文gQ?br />
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.base.mapping.VnetTable" table="vnet_table" >
        <id name="id" type="java.lang.String">
            <column name="ID" length="32" />
            <generator class="uuid.hex" />
        </id>
        <property name="vnId" type="java.lang.Long">
            <column name="VN_ID">
                <comment>创徏虚拟|络时得到的ID</comment>
            </column>
        </property>
         ...
        <property name="zoneId" type="java.lang.String">
            <column name="ZONE_ID" length="32">
                <comment>分区ID</comment>
            </column>
        </property>
        <property name="vlanId" type="java.lang.Integer">
            <column name="VLAN_ID">
                <comment>VLAN_ID</comment>
            </column>
        </property>
        <set name="vnTables" lazy="false" order-by="id asc" inverse="false" fetch="join" cascade="all">
            <key column="VN_NID"/>
            <one-to-many class="com.base.mapping.VnTable"/>
        </set>
    </class>
</hibernate-mapping>

理解inverse和cascadeQ可以参考:http://blog.csdn.net/wkcgy/article/details/6271321
ȝQ?nbsp;
<one-to-many>中,inverse=”true”Q由“many”Ҏq行兌关系的维?nbsp;
<many-to-many>中,只设|其中一方inverse=”false”Q或双方都不讄 
CascadeQ通常情况下都不会使用。特别是删除Q一定要慎重?/span>
操作 
  一般对many-to-one和many-to-many不设|联,q要看业务逻辑的需?对one-to-one和one-to-many讄U联?/span>
  many-to-many兌关系中,一端设|inverse=”false”Q另一端设|ؓinverse=”true”。在one-to-many兌关系中,讄inverse=”true”,由多端来l护关系?/span>












David1228 2013-09-12 11:57 发表评论
]]>
Mybatis3.2.1整合Spring3.1http://www.aygfsteel.com/ldwblog/archive/2013/07/10/401418.htmlDavid1228David1228Wed, 10 Jul 2013 10:07:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2013/07/10/401418.htmlhttp://www.aygfsteel.com/ldwblog/comments/401418.htmlhttp://www.aygfsteel.com/ldwblog/archive/2013/07/10/401418.html#Feedback2http://www.aygfsteel.com/ldwblog/comments/commentRss/401418.htmlhttp://www.aygfsteel.com/ldwblog/services/trackbacks/401418.html阅读全文

David1228 2013-07-10 18:07 发表评论
]]>
Hibernate 的悲观锁说明Q自己复习之用,转自http://blog.sina.com.cn/s/blog_49cc672f0100ar8h.htmlhttp://www.aygfsteel.com/ldwblog/archive/2011/03/17/346473.htmlDavid1228David1228Thu, 17 Mar 2011 08:03:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2011/03/17/346473.htmlhttp://www.aygfsteel.com/ldwblog/comments/346473.htmlhttp://www.aygfsteel.com/ldwblog/archive/2011/03/17/346473.html#Feedback0http://www.aygfsteel.com/ldwblog/comments/commentRss/346473.htmlhttp://www.aygfsteel.com/ldwblog/services/trackbacks/346473.html

软gpȝ的ƈ发控制一般是通过加锁Q有乐观锁和悲观锁两U)来实玎ͼ乐观锁是一U事后补救措施,是通过E序的逻辑控制版本来实现的Q而悲观锁是事前的一U预防措施,它利用数据库的锁机制来实现?/p>

虽然乐观锁能够提高系l的性能Q但它是对发生冲H的讉Kq行事后的补救,应用在用戯入数据量很少的场合比较适合Q但如果在企?a class="infotextkey" target="_blank">ERPQ用户与pȝ交互涉及大量数据在页面表单上录入Q如果事后提交失败后才提C用戯重新录入是很不现实的Q所以有必要q行事前控制Q这p采用悲观锁?br />
在多个客L可能d同一W数据或同时更新一W数据的情况下,防止同一个数据被修改而造成混ؕQ最单的手段是在读取时Ҏ据进行锁定,其它客户端不能对同一W数据进行更新的d动作?/p>

一个典型的倚赖数据库的悲观锁调用:
select * from account where name=”John” for update

q条 sql 语句锁定?account 表中所有符合检索条Ӟname=”Erica”Q的记录。本ơ事务提交之前(事务提交时会释放事务q程中的锁)Q外界无法修改这些记录。Hibernate 的悲观锁Q也是基于数据库的锁机制实现?br /> 下面的代码实CҎ询记录的加锁Q?br /> String hqlStr ="from TUser as user where user.name='John'";
Query query = session.createQuery(hqlStr);
query.setLockMode("user",LockMode.UPGRADE); // 加锁
List userList = query.list();// 执行查询Q获取数?/font>

query.setLockMode Ҏ询语句中Q特定别名所对应的记录进行加锁(我们为TUser cL定了一个别?“user” Q,q里也就是对q回的所?user 记录q行加锁?br /> 观察q行?Hibernate 生成?SQL 语句Q?br /> select tuser0_.id as id, tuser0_.name as name, tuser0_.group_id
as group_id, tuser0_.user_type as user_type, tuser0_.sex as sex
from t_user tuser0_ where (tuser0_.name='John' ) for update

q里 Hibernate 通过使用数据库的 for update 子句实现了悲观锁机制?br /> Hibernate 的加锁模式有Q?br /> LockMode.NONE Q?/strong> 无锁机制?br /> LockMode.WRITE Q?/strong> Hibernate ?Insert ?Update 记录的时候会自动获取?br /> LockMode.READ Q?/strong> Hibernate 在读取记录的时候会自动获取?br /> 以上q三U锁机制一般由 Hibernate 内部使用Q如 Hibernate Z保证 Updateq程中对象不会被外界修改Q会?save Ҏ实现中自动ؓ目标对象加上 WRITE 锁?br /> LockMode.UPGRADE Q?/strong>利用数据库的 for update 子句加锁?br /> LockMode. UPGRADE_NOWAIT Q?/strong> Oracle 的特定实玎ͼ利用 Oracle ?for update nowait 子句实现加锁?br /> 上面q两U锁机制是我们在应用层较为常用的Q加锁一般通过以下Ҏ实现Q?br /> Criteria.setLockMode
Query.setLockMode
Session.lock

注意Q只有在查询开始之前(也就?Hiberate 生成 SQL 之前Q设定加锁,才会真正通过数据库的锁机制进行加锁处理,否则Q数据已l通过不包?for update 子句?Select SQL 加蝲q来Q所谓数据库加锁也就无从谈v?br />



David1228 2011-03-17 16:03 发表评论
]]>
在Hibernate中,对象有三U状?/title><link>http://www.aygfsteel.com/ldwblog/archive/2010/08/16/328954.html</link><dc:creator>David1228</dc:creator><author>David1228</author><pubDate>Mon, 16 Aug 2010 01:59:00 GMT</pubDate><guid>http://www.aygfsteel.com/ldwblog/archive/2010/08/16/328954.html</guid><wfw:comment>http://www.aygfsteel.com/ldwblog/comments/328954.html</wfw:comment><comments>http://www.aygfsteel.com/ldwblog/archive/2010/08/16/328954.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ldwblog/comments/commentRss/328954.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ldwblog/services/trackbacks/328954.html</trackback:ping><description><![CDATA[在Hibernate中,对象有三U状态:临时状态、持久状态和游离状态? <p> <wbr> <wbr> <wbr> <wbr> <wbr><font color="#ff0000"><strong> <wbr>临时状?/strong></font>Q当new一个实体对象后Q这个对象处于时状态,卌个对象只是一个保存时数据的内存区域Q如果没有变量引用这个对象,则会被jre垃圾回收机制回收。这个对象所保存的数据与数据库没有Q何关p,除非通过Session的save或者SaveOrUpdate把时对象与数据库关联,q把数据插入或者更新到数据库,q个对象才{换ؓ持久对象?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>例如QEmp e=new Emp(); <wbr> <wbr> <wbr> //创徏临时对象 <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> e.setEmpno((long) 8888); <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>e.setEName("mike"); <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>... <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>EmpDAO d=new EmpDAO(); <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>d.save(e);//持久?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>...</p> <p><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <font color="#ff0000"><strong>持久状?/strong></font>: 持久化对象的实例在数据库中有对应的记录,q拥有一个持久化表示QIDQ。对持久化对象进行delete操作后,数据库中对应的记录将被删除,那么持久化对象与数据库记录不再存在对应关p,持久化对象变成时状态?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>持久化对象被修改变更后,不会马上同步到数据库Q直到数据库事务提交。在同步之前Q持久化对象是脏的(DirtyQ?br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 例如Q?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>Emp e=new Emp(); //创徏了时的对象 <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>EmpDAO edao= new empDAO(); <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>e=edao.findbyEmpno((long) 7786); <wbr> <wbr> <wbr> //使对象与数据库记录对应,从而持久化 <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>e.setEname("新的名字"); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //修改了持久化对象Q之处?Dirty <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>...... <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>edao.saveorupdate(e); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //保存Q但是仍?Dirty <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>tran.commit(); <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> //提交Q实C数据库同步,不再Dirty <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>......</p> <p> <wbr></p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><font color="#ff0000"><strong> <wbr>游离状?/strong></font>Q当Sessionq行了Close、Clear或者evict后,持久化对象虽然拥有持久化标识W和与数据库对应记录一致的|但是因ؓ会话已经消失Q对象不在持久化理之内Q所以处于游ȝ态(也叫Q脱状态)。游ȝ态的对象与时状态对象是十分怼的,只是它还含有持久化标识?/p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>其中Q持久化状态的对象Q简UCؓQPO <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>而时状态和游离状态的对象Q简UCؓQVO</p> <p><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>1、通过get()或load()Ҏ得到的实例都是持久化状态的?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>2、当把session关闭Ӟsession~存中的持久化对象也变成游离?/p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 因关闭session而变成游L的可以通过lock、save、update变成持久?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 3、持久态实例可以通过调用delete()变成游离?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>而游ȝ态的实例可以通过调用lock()或者replicate()q行持久?nbsp;<wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>4、save()和persist()会引发SQL的insertQdelete()会引发SQL的delete <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>update()或merge()会引发SQL的updateQ对持久化实例的修改在刷新提交的时候会</p> <p> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr>被检到, 它会引发SQL的update <wbr><br />  <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>  <wbr>saveOrUpdate()或者replicate ()会引发SQL的insert或者update</p> <img src ="http://www.aygfsteel.com/ldwblog/aggbug/328954.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ldwblog/" target="_blank">David1228</a> 2010-08-16 09:59 <a href="http://www.aygfsteel.com/ldwblog/archive/2010/08/16/328954.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据库表中定义数据类型与Hibernate中获取值方?/title><link>http://www.aygfsteel.com/ldwblog/archive/2009/08/31/293332.html</link><dc:creator>David1228</dc:creator><author>David1228</author><pubDate>Mon, 31 Aug 2009 08:26:00 GMT</pubDate><guid>http://www.aygfsteel.com/ldwblog/archive/2009/08/31/293332.html</guid><wfw:comment>http://www.aygfsteel.com/ldwblog/comments/293332.html</wfw:comment><comments>http://www.aygfsteel.com/ldwblog/archive/2009/08/31/293332.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ldwblog/comments/commentRss/293332.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ldwblog/services/trackbacks/293332.html</trackback:ping><description><![CDATA[     摘要: province_code-----Char(2) --》Character cert-----Clob cd ---》SerializableClob certstate------Numbercd --》BigDecimal    usercode------Varchar2cd ---》String addtime ---...  <a href='http://www.aygfsteel.com/ldwblog/archive/2009/08/31/293332.html'>阅读全文</a><img src ="http://www.aygfsteel.com/ldwblog/aggbug/293332.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ldwblog/" target="_blank">David1228</a> 2009-08-31 16:26 <a href="http://www.aygfsteel.com/ldwblog/archive/2009/08/31/293332.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HibernateDAO实现c?查询ListҎhttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291937.htmlDavid1228David1228Thu, 20 Aug 2009 05:37:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291937.htmlhttp://www.aygfsteel.com/ldwblog/comments/291937.htmlhttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291937.html#Feedback0http://www.aygfsteel.com/ldwblog/comments/commentRss/291937.htmlhttp://www.aygfsteel.com/ldwblog/services/trackbacks/291937.html 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;
 }



David1228 2009-08-20 13:37 发表评论
]]>
HibernateTemplate增改删查Ҏhttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291932.htmlDavid1228David1228Thu, 20 Aug 2009 05:22:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291932.htmlhttp://www.aygfsteel.com/ldwblog/comments/291932.htmlhttp://www.aygfsteel.com/ldwblog/archive/2009/08/20/291932.html#Feedback0http://www.aygfsteel.com/ldwblog/comments/commentRss/291932.htmlhttp://www.aygfsteel.com/ldwblog/services/trackbacks/291932.htmlpackage com.cns.certservice.dao.impl;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.cns.certservice.exception.DAOException;


public class HibernateTemplate {

    private HibernateTemplate() {

    }

    /**
     * static final session factory
     */
    private static SessionFactory sessionFactory = null;

    /**
     * local thread variable used for storing share session instance
     */
    private static final ThreadLocal localSession = new ThreadLocal();

    /**
     * log4j logger
     */
    private static final Logger logger = Logger
            .getLogger(HibernateTemplate.class);
    /**
     * use JTA transaction
     */
    /**
     * 该工具唯一实例?br />      */
    private static HibernateTemplate instance = null;
    private static Transaction tx = null;
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final Configuration cfg = new Configuration();
    /** Holds a single instance of Session */
    private static final ThreadLocal threadLocal = new ThreadLocal();

    /**
     * 获取持久工具的唯一实例Q以后不是用单实例模式Q而不是采用对象池支持?br />      * @return PersistentTool
     * @throws BaseException
     */
    public synchronized static HibernateTemplate getInstance() {
        if (instance == null) {
            instance = new HibernateTemplate();
            instance.initHibernate();
        }
        return instance;
    }

    /**
     * 实现Hibernate的初始化配置环境?br />      */
    public void initHibernate() {
        try {
            //此处从系l\径中获取配置文g
            cfg.configure(CONFIG_FILE_LOCATION);
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }
        try {
            // 装蝲配置Q构造SessionFactory对象
            sessionFactory = cfg.buildSessionFactory();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    /**
     * Get the share session
     * @
     * @return Session share session
     */
    public  Session getSession() {
        logger.debug("Now enter into getSession method of DaoUtil");
        //obtain share session
        Session session = (Session) localSession.get();
        try {
            if (session == null||!session.isOpen()) {
                //get session by session factory
                session = sessionFactory.openSession();
                localSession.set(session);
            }
        } catch (HibernateException ex) {
            ex.printStackTrace();

        }
        return session;
    }

    /**
     * Close share session
     * @
     */
    public  void close() {
        logger.debug("Now enter into closeSessionl");
        //obtain share session
        Session session = (Session) localSession.get();
        localSession.set(null);
        if (session != null) {
            try {
                session.flush();
                session.close();
            } catch (HibernateException ex) {
                ex.printStackTrace();

            }
        }
    }

    /**
     * Begin JTA transaction
     * @
     */
    public  void beginTransaction() {
        logger.debug("Now enter into beginTransaction");
        try {
            Session session = (Session) localSession.get();
            tx = session.beginTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

    /**
     * Commit transaction
     * @
     */
    public  void commitTransaction() {
        try {
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

    /**
     * Rollback transaction when breaching ACID operation
     * @
     */
    public  void rollbackTransaction() {
        try {
            tx.rollback();
        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

    /**
     * Insert a record into table
     * @param obj Object
     * @throws DAOException
     * @
     */
    public  int insertObject(Object obj) throws DAOException {
     int res = 0;
        logger.debug("Now enter into insertObject");
        //obtain current share session
        try {
            Session session = HibernateTemplate.getInstance().getSession();
            beginTransaction();
            Object robj = session.save(obj);
            if (robj instanceof Integer) {
    res = (Integer) robj;
   }
            if (robj instanceof String) {
    res =1;
   }
            session.flush();
        } catch (HibernateException ex) {
            rollbackTransaction();
            logger.error("insertObject error:", ex);
            throw new DAOException(ex);
        } finally {
            commitTransaction();
            close();
        }
        return res;
    }


    /**
     * Delete a record of database table by Hibernate po object
     * @param obj Object
     * @throws DAOException
     * @
     */
    public  boolean deleteObject(Object obj) throws DAOException {
     boolean res = false;
        logger.debug("Now enter into deleteObject method");
        //obtain current share session
        try {
            Session session = HibernateTemplate.getInstance().getSession();
            beginTransaction();
            session.delete(obj);
            session.flush();
            res = true;
        } catch (HibernateException ex) {
            rollbackTransaction();
            logger.error("deleteObject error:", ex);
            throw new DAOException(ex);
        } finally {
            commitTransaction();
            close();
        }
        return res;
    }


    /**
     * Update a record of database table
     * @param ob Object
     * @throws DAOException
     * @
     */
    public  boolean updateObject(Object ob) throws DAOException {
     boolean res = false;
        logger.debug("Now enter into updateObject");
        //obtain current share session
        try {
            Session session = HibernateTemplate.getInstance().getSession();
            beginTransaction();
            session.update(ob);
            session.flush();
            res= true;
        } catch (HibernateException ex) {
         rollbackTransaction();
          logger.error("updateObject error:", ex);
          throw new DAOException(ex);
        } finally {
            commitTransaction();
            close();
        }
        return res;
    }
}



David1228 2009-08-20 13:22 发表评论
]]>
struts1+hibernate分页例子http://www.aygfsteel.com/ldwblog/archive/2009/08/17/291502.htmlDavid1228David1228Mon, 17 Aug 2009 07:15:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2009/08/17/291502.html

David1228 2009-08-17 15:15 发表评论
]]>
Hibernate创徏数据库表Q一对一and一对多Q?http://www.aygfsteel.com/ldwblog/archive/2008/12/15/246428.htmlDavid1228David1228Mon, 15 Dec 2008 07:44:00 GMThttp://www.aygfsteel.com/ldwblog/archive/2008/12/15/246428.html阅读全文

David1228 2008-12-15 15:44 发表评论
]]>
վ֩ģ壺 | | ½| | | | ƽ| ̨ʡ| | | | | | | ٲ| | | | | | | | | Ϳ| ˺| | ˹| | ٹ| | ĩ| ɽʡ| | ̩| | ͤ| | ƽ| Ϫ| ƽ| |