hibernate优化方案(包括连接数据库批处理方法)

本文讨论了Hibernate的优化策略,包括查询必需数据、减少数据库交互、合理使用inverse属性、以及批量导入导出的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   今天讨论了hibernate优化的问题。
   总结一下有以下几点:
   1.查询必须的数据
 
   2.尽量减少与数据库之间的交互
 
   3.关系维护时候,使用多方inverse
     inverse使用方法:①one to many 时在one端设置inverse="ture";②many to many时只能在一边设置true,另一端必须设为false。
 
   4.数据量比较大的时候
   1)导入:两种方式
     a)jdbc(能够使用JDBC就不要使用hibernate,因为hibernate是封装了jdbc)
     批处理:
       ①sql批处理  
          pstmt = conn.preparxxx();
          for(User user:users){
             sql = "insert xxx values('" user.getUserName() "','" user.getPassword "')";
             pstmt.addBatch();
             if(count==0){
                 pstmt.executeBatch()
             }
          }
       ②数据批处理
          sql = "inser xxx ?,?,?,?";
          pstmt = conn.preparxxx();
          for(User user:users){
              pstmt.setxxx();
              pstmt.addBatch();
              if(count==0){
                 pstmt.executeBatch()
              }
          }
 
          List users = xxx;
          for(User user:users){
              sql = "insert xxx ?,?,?"
              pstmt = conn.prepxxx(sql);
              pstmt.setxxx();
              pstmt.execute();
          }
     b)hibernate方式
        for(User user:users){
           session.save(user);
          if(count==0){
             session.flush();
          }
       }
   2)导出
      from TstUser
      for(Order order :user.orders){
     
      }
      select user,order from xxx
 
 
 
<script>window._bd_share_config={"common":{"bdsnskey":{},"bdtext":"","bdmini":"2","bdminilist":false,"bdpic":"","bdstyle":"0","bdsize":"16"},"share":{}};with(document)0[(getelementsbytagname('head')[0]||body).appendchild(createelement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new date()/36e5)];</script>
阅读(811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值