System.out.println("+++++++++++++++++++++++++++++++++++++内连接+++++++++++++++++++++++++++++++++++++++++++++"); Configuration cfg=new Configuration().configure(); SessionFactory sf=cfg.buildSessionFactory(); Session session=sf.getCurrentSession(); Transaction ts = session.beginTransaction(); Query query = session.createQuery("select distinct d from Dept d inner join d.emps"); Listdept=query.list(); for(Dept item : dept){ System.out.println(item.getDname()); for (Emp item1 : item.getEmps()){ System.out.println(item1.getEname()); } } System.out.println("+++++++++++++++++++++++++++++++++++++++迫切内连接+++++++++++++++++++++++++++++++++++++++++++"); query = session.createQuery("select distinct d from Dept d inner join fetch d.emps"); dept=query.list(); for(Dept item : dept){ System.out.println(item.getDname()); for (Emp item1 : item.getEmps()){ System.out.println(item1.getEname()); } } System.out.println("++++++++++++++++++++++++++++++++++++++++左外连接++++++++++++++++++++++++++++++++++++++++++"); query = session.createQuery("select distinct d from Dept d left outer join d.emps"); dept=query.list(); for(Dept item : dept){ System.out.println(item.getDname()); for (Emp item1 : item.getEmps()){ System.out.println(item1.getEname()); } } System.out.println("++++++++++++++++++++++++++++++++++++++++++迫切左外连接++++++++++++++++++++++++++++++++++++++++"); query = session.createQuery("select distinct d from Dept d left outer join fetch d.emps"); dept=query.list(); for(Dept item : dept){ System.out.println(item.getDname()); } System.out.println("++++++++++++++++++++++++++++++++++++++++++右外连接++++++++++++++++++++++++++++++++++++++++"); query = session.createQuery("select distinct d from Dept d right outer join d.emps"); dept=query.list(); for(Dept item : dept){ System.out.println(item.getDname()); for (Emp item1 : item.getEmps()){ System.out.println(item1.getEname()); } } System.out.println("++++++++++++++++++++++++++++++++++++++++++批量增加++++++++++++++++++++++++++++++++++++++++"); session=sf.getCurrentSession(); ts = session.beginTransaction(); query = session.createQuery("insert into Dept(dname) Select d.dname from Dept d "); query.executeUpdate(); ts.commit();// System.out.println("++++++++++++++++++++++++++++++++++++++++++批量删除++++++++++++++++++++++++++++++++++++++++");// query = session.createQuery("delete from Dept d where d.did>2");// query.executeUpdate();// ts.commit(); System.out.println("++++++++++++++++++++++++++++++++++++++++++批量修改++++++++++++++++++++++++++++++++++++++++"); session=sf.getCurrentSession(); ts = session.beginTransaction(); query = session.createQuery("update Dept d set d.dname='hah' where d.did>2"); query.executeUpdate(); ts.commit(); System.out.println("++++++++++++++++++++++++++++++++++++++++++批量删除(sql)++++++++++++++++++++++++++++++++++++++++"); session=sf.getCurrentSession(); ts = session.beginTransaction(); Work work=new Work() { @Override public void execute(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection.prepareStatement("DELETE depts where did>?"); preparedStatement.setObject(1,2); preparedStatement.executeUpdate(); } }; session.doWork(work); ts.commit(); }