首页 > 代码库 > Hql 执行CRUD
Hql 执行CRUD
//新增] @Test public void add(){ config = new Configuration(); sessionfactory = config.configure("hibernate.cfg.xml").buildSessionFactory(); session = sessionfactory.openSession(); ts = session.beginTransaction(); User user = new User(); user.setName("QQ"); user.setPwd("123"); session.save(user); ts.commit(); session.close();}
//HQL 查询----------------------------- @Test public void sel(){ config = new Configuration(); sessionfactory = config.configure("hibernate.cfg.xml").buildSessionFactory(); session = sessionfactory.openSession(); ts = session.beginTransaction(); Query query = session.createQuery("from User user where user.name=? and user.pwd=?"); query.setString(0, "www"); query.setString(1, "888"); List uu = (List) query.list(); for(int i =0;i<uu.size();i++){ User user = (User) uu.get(i); System.out.println(user.getName() +"-------"+ user.getPwd()); } session.close(); }
//HQL 删除 @Test public void del(){ config = new Configuration(); sessionfactory = config.configure("hibernate.cfg.xml").buildSessionFactory(); session = sessionfactory.openSession(); ts = session.beginTransaction(); String hql="delete from User user where user.id=?"; Query queryupdate=session.createQuery(hql); queryupdate.setInteger(0, 5); int ret=queryupdate.executeUpdate(); if(ret >0 ){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); }
ts.commit();
}
//HQL 修改 ----------------------------------------- @Test public void update(){ config = new Configuration(); sessionfactory = config.configure("hibernate.cfg.xml").buildSessionFactory(); session = sessionfactory.openSession(); ts = session.beginTransaction(); String hql = "update User user set user.name=?,user.pwd=? where user.id=?"; Query queryupdate=session.createQuery(hql); queryupdate.setString(0, "ABCDE"); queryupdate.setString(1, "AAA"); queryupdate.setInteger(2,6); int ret=queryupdate.executeUpdate(); if(ret >0 ){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } ts.commit(); }
Hql 执行CRUD
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。