Hibernate 批量删除 批量 插入 关联查询
来源:互联网 发布:网络推广部门架构 编辑:程序博客网 时间:2024/06/08 17:18
@Componentpublic class StudentDao extends HibernateDao<Student, Integer> { public void deleteByIds(List<?> ids) { String hql = "delete from Student where id in (:ids)"; createQuery(hql).setParameterList("ids", ids).executeUpdate(); } public void saveBatch(List<Student> stuList) { if (null != stuList && stuList.size() > 0) { Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { for (int i = 0; i < stuList.size(); i++) { session.save(stuList.get(i)); if (i % 100 == 0) { // 每一百条刷新并写入数据库 session.flush(); session.clear(); } } } catch (HibernateException e) { e.printStackTrace(); }finally{ tx.commit(); session.close(); } } }}//批量查询,当然hibernate也提供了使用某一个属性查询出集合 String hql = "from Goods where merchandiseId in (:ids)"; List<Goods> goodsList = goodsDao.createQuery(hql).setParameterList("ids", idList).list();
2 批量插入采用sql时如下:
public void setMappingGoods(Integer merchandiseId,List<Integer> ids) { StringBuilder sb = new StringBuilder(); sb.append("insert into mapping_goods(kid,oid) values "); for(Integer id:ids){ sb.append("("+merchandiseId+","+id+")"+","); } goodsmapDao.getSession().createSQLQuery(sb.substring(0, sb.length()-1)).executeUpdate(); }
3 hql 关联查询:当没有在实体中设置关联关系,而是自己建立的中间表时,可以用hql关联查询数据。
public List<Goods> getMappingGoods(String kuuid) { if(StringUtils.empty(kuuid)){ return new ArrayList<Goods>(); } String hql = " from Goods g where g.merchandiseUuid!=? and ( (g.merchandiseUuid in (select mp.kuuid from Goodsmap mp where mp.kuuid=? or mp.ouuid=?)) or (g.merchandiseUuid in (select mp.ouuid from Goodsmap mp where mp.kuuid=? or mp.ouuid=?)))"; return goodsDao.find(hql,kuuid, kuuid,kuuid, kuuid,kuuid); }
0 0
- Hibernate 批量删除 批量 插入 关联查询
- hibernate3 的常用操作(批量删除,批量插入,关联查询)
- hibernate3 的常用操作(批量删除,批量插入,关联查询) (未测试)
- mybatis 操作(批量插入,批量修改,批量删除, 分页查询)
- Hibernate批量处理操作(批量查询 批量删除)
- Hibernate 批量插入、更新与删除
- Hibernate 批量插入、更新与删除
- Hibernate 批量插入、更新与删除
- hibernate批量插入,更新和删除!
- Hibernate 批量插入、更新与删除
- mybatis批量插入、批量删除
- Mybatis-批量查询&&批量插入
- jdbc批量插入、批量删除、批量更新
- JDBC批量插入、批量删除、批量更新
- JDBC批量插入、批量删除、批量更新
- jdbc批量插入、批量删除、批量更新
- jdbc批量插入、批量删除、批量更新
- jdbc批量插入、批量删除、批量更新
- 选手及评委素材信息规范处理说明
- httpclient SocketTimeout ConnectTimeout ConnectionRequestTimeout
- 项目中对质量的思考
- 视觉与应用
- tomcat 项目部署方法
- Hibernate 批量删除 批量 插入 关联查询
- Android Studio 获取最新版本的依赖库
- Ubuntu 使用总结
- SpringBoot配置属性之MQ
- POJ 1486 Sorting Slides 已翻译
- Settings.System.putXXX数据变化,ContentObserver监听
- ubuntu16.04+cuda8.0+caffe安装
- [转载]关于定义hashcode时要使用31这个数的解释
- Linux-查看cpu型号