Collection(集合)
列出所有有学生参加的项目:(项目的学生的数量大于0,则表示有学生参加)
public void testCollection() throws Exception { Session session = HibernateUtil.getInstance().getSession(); //使用size属性或者SIZE函数来计算一个集合的元素数量放在where条件中是没有区别的 // 都是一个子查询完成 //String hql = "SELECT proj FROM Project proj WHERE proj.students.size > 0"; String hql = "SELECT proj FROM Project proj WHERE SIZE(proj.students) > 0"; Query query = session.createQuery(hql); List<Project> list = query.list(); for (Project object : list) { System.out.println(object); } session.close(); }
---------------------------------------------------------------------------------------------------------------------------------------------------
public void testCollection1() throws Exception { Session session = HibernateUtil.getInstance().getSession(); // 判断集合是不是空 // IS NOT EMPTY: 不是空, // IS EMPTY:是空,
String hql = "SELECT proj FROM Project proj WHERE proj.students IS NOT EMPTY"; Query query = session.createQuery(hql); @SuppressWarnings("unchecked") List<Project> list = query.list(); for (Project object : list) { System.out.println(object); } session.close(); }
列出每个班级中学生的平均龄和最小年龄:(AVG(age),MIN(age))
①:先把学生按班级分组把学生的年龄查出来
select AVG(stu.age),MIN(stu.age) from Student stu group by stu.clazz;
public void testFunction() throws Exception { Session session = HibernateUtil.getInstance().getSession(); String hql = "SELECT stu.id, AVG(stu.age), MIN(stu.age) FROM Student stu GROUP BY stu.clazz"; Query query = session.createQuery(hql); @SuppressWarnings("unchecked") List<Object[]> list = query.list(); for (Object[] object : list) { System.out.println(Arrays.toString(object)); } session.close(); } }
join(多表联合查询):
列出所有学生的名字对应的班级的名字:
select stu.name,clz.name From Student stu,Clazz clz Where stu.clz=clz (当学生的班级等于班级的时候)
①:隐式连接
public void Jointest() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT stu.name,clz.name FROM Student stu,Clazz clz WHERE stu.clz=clz"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<Object[]> list = createQuery.list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
②:显示连接 join
select stu.name,clz.name From Student stu join Clazz clz on stu.clz=clz (当学生的班级等于班级的时候) 不能这么写
HQL语句按照上面那么写会报错:org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: on near line 1, column 68 [select stu.name,clz.name From daomian.Student stu join Clazz clz on stu.clz=clz]
SELECT stu.name,clz.name FROM Student stu JOIN stu.clz clz
public void Jointest1() { Session session = BuildSesssionFactory.getSession(); //通过Student 对象中Clazz 连接,Student中的外键找Clazz中的主键 String sql = "SELECT stu.name,clz.name FROM Student stu JOIN stu.clz clz"; Query createQuery = session.createQuery(sql); List<Object[]> list = createQuery.list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
limit(分页):
select stu from Student stu LIMIT 0,2; 不能这么写 HQL中不支持LIMIT关键字
HQL语句按照上面那么写会报错org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: LIMIT near line 1, column 38 [select stu from daomian.Student stu LIMIT 0,2]
public void testLimit() throws Exception { Integer pageNo = 2; Integer pageSize = 10; // HQL中不支持LIMIT关键字 Session session = HibernateUtil.getInstance().getSession(); String hql = "SELECT stu FROM Student stu"; Query query = session.createQuery(hql); // firstResult: 从数据库中的第几条数据开始查询 query.setFirstResult((pageNo - 1) * pageSize); // maxResults: 表示本次分页的最大条数 query.setMaxResults(pageSize); List<Student> list = query.list(); for (Student object : list) { System.out.println(object); } session.close(); }
Namedquery:
// NamedQuery:主要是为了一些经常使用,但是又不愿意每次都写时用
// 在类的映射文件中添加一个标签,在class标签外添加一个query
// 在属性name中来指定这个NamedQuery指定属性,
// 在XML的innerTest中编写HQL语句
public void namedQuerytest() { Session session = BuildSesssionFactory.getSession(); Query namedQuery = session.getNamedQuery("QUERY_ALL_STUDENT"); @SuppressWarnings("unchecked") List<Student> list = namedQuery.list(); for (Student student : list) { System.out.println(student); } // NamedQuery:主要是为了一些经常使用,但是又不愿意每次都写时用 // 在类的映射文件中添加一个标签,在class标签外添加一个query // 在属性name中来指定这个NamedQuery指定属性, // 在XML的innerTest中编写HQL语句 }
<query name="QUERY_ALL_STUDENT">SELECT stu FROM Student stu</query> //在Student对象对应的xml中配置
这里的query标签中的name属性和java代码中session.getNamedQuery中的值是一样的
paramenter:(查询参数设置)
①:位置占位符
public class ParamenterTest { // 查询参数设置: // 1、位置占位符:就是使用`?`号来代表查询参数, // 通过setParameter(index,object)来根据?的位置来设置参数的; @Test public void positionTest() { Session session = BuildSesssionFactory.getSession(); String sql = "select stu from Student stu Where stu.age between ? and ?"; Query createQuery = session.createQuery(sql); createQuery.setParameter(0, 18); createQuery.setParameter(1, 20); @SuppressWarnings("unchecked") List<Student> list = createQuery.list(); for (Student student : list) { System.out.println(student); } session.close(); }
②:名称占位符
// 2、名称占位符:就是使用`:paramName`号来代表查询参数, // 通过setParamter(String name,object)这个方法为名称给占位符添加参数; @Test public void nameTest() { Session session = BuildSesssionFactory.getSession(); String sql = "select stu from Student stu Where stu.age between :small and :big"; Query createQuery = session.createQuery(sql); createQuery.setParameter("small", 18); createQuery.setParameter("big", 20); @SuppressWarnings("unchecked") List<Student> list = createQuery.list(); for (Student student : list) { System.out.println(student); } session.close(); }
用名称占位符的时候,要注意查询语句里边应该是(: 别名)
设置值的时候为 createQuery.setParameter("别名",值);
Result:(返回结果)
①:查询学生的总数:
Select COUNT(stu) FROM Student stu;
public class ResultTest { @Test @SuppressWarnings("unchecked") public void ResulTest() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT COUNT(stu.id) FROM Student stu"; Query createQuery = session.createQuery(sql); List<Long> list = createQuery.list(); for (Long long1 : list) { System.out.println(long1); } }
--------------------------------------------------------------------------------------------------------------------------------------------------------
//当查询结果为单条记录的时候,不需要调用list()方法,只需要调用uniqueResult()方法即可 @Test public void ResulTest1() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT COUNT(stu.id) FROM Student stu"; Query createQuery = session.createQuery(sql); Long long1 = (Long) createQuery.uniqueResult(); System.out.println(long1); }
②:返回一个Object[]:
Select stu.id,stu.name,stu.age FROM Student stu;
public void ResulTest2() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT stu.id,stu.name,stu.age FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<Object[]> list = createQuery.list(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
③:返回一个list集合
Select new LIST(stu.id,stu.name,stu.age) FROM Student stu;
public void ResulTest3() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT NEW LIST(stu.id,stu.name,stu.age) FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<List<Student>> list = createQuery.list(); for (List<Student> list2 : list) { System.out.println(list2); } }
②:返回一个Map集合
Select new MAP(stu.id,stu.name,stu.age) FROM Student stu;
public void ResulTest4() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT NEW MAP(stu.id ,stu.name,stu.age) FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<Map<String,Student>> list = createQuery.list(); for (Map<String, Student> map : list) { System.out.println(map); } }
⑤: new Map 给KEY值起一个别名
Select new MAP(stu.id AS ID,stu.name AS NAME,stu.age AS AGE) FROM Student stu; AS不能省略
public void ResulTest5() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT NEW MAP(stu.id as ID ,stu.name AS NAME,stu.age AS AGE) FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<Map<String,Student>> list = createQuery.list(); for (Map<String, Student> map : list) { System.out.println(map); } }
⑥: new 一个对象 Class 没有配置写全限定名称 VO对象 ValueObject
SELECT NEW StudentVO类的全限定名称(id,name,age) FROM Student stu
public class StudentVO { private Long id; private String name; private Integer age; //此处省略getter/setter方法 }
public void ResulTest6() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT NEW hibernate.query.result.StudentVO(id,name,age) FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<StudentVO> list = createQuery.list(); for (StudentVO student : list) { System.out.println(student); } }
7: new一个对象 在映射文件中配置Class(在FROM的那个对象的映射文件中配置)
SELECT NEW StudentVO(id,name,age) FROM Student stu
<import class="hibernate.query.result.StudentVO" rename="StudentVO"/> //在class标签外 在hibernate-mapping标签内写
public class StudentVO { private Long id; private String name; private Integer age; //此处省略getter/setter方法 }
public void ResulTest7() { Session session = BuildSesssionFactory.getSession(); String sql = "SELECT NEW StudentVO(id,name,age) FROM Student stu"; Query createQuery = session.createQuery(sql); @SuppressWarnings("unchecked") List<StudentVO> list = createQuery.list(); for (StudentVO student : list) { System.out.println(student); } }