时间:2021-07-01 10:21:17 帮助过:116人阅读
(2)直接封装到学生对象中,将对象存储到list集合:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); String sql="Select * from Student"; SQLQuery sqlQuery=session.createSQLQuery(sql); sqlQuery.addEntity(Student.class); List<Student>list= sqlQuery.list(); System.out.println(list); transaction.commit(); session.close(); }
2、占位符的使用:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); String sql="Select * from Student where sex=?"; SQLQuery sqlQuery=session.createSQLQuery(sql); sqlQuery.setParameter(0,"女"); sqlQuery.addEntity(Student.class); List<Student>list= sqlQuery.list(); System.out.println(list); transaction.commit(); session.close(); }
3、分页查询:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); String sql="Select * from Student limit ?,?"; SQLQuery sqlQuery=session.createSQLQuery(sql); sqlQuery.setParameter(0,0); sqlQuery.setParameter(1,1); sqlQuery.addEntity(Student.class); List<Student>list= sqlQuery.list(); System.out.println(list); transaction.commit(); session.close(); }
Hibernate的批量查询——原生sql查询
标签:占位符 tor sts objects obj 分页查询 span generator create