今日学习主要知识点(所借用案例都是双向多对一,一对多):
1.分页
2.多表查询
3.所需数据库代码
insert into CLASSINFO values('c111','banji1'); insert into CLASSINFO values('c222','banji2'); insert into persons values('p1','p111','c111') insert into persons values('p2','p222','c111') insert into persons values('p3','p333','c222') insert into persons values('p4','p444','c222') insert into persons values('p5','p555',null) select * from CLASSINFO select * from persons inner join CLASSINFO on persons.rci = CLASSINFO.cid select * from persons left outer join CLASSINFO on persons.rci = CLASSINFO.cid select * from CLASSINFO left outer join persons on CLASSINFO.cid = persons.rci
一、分页查询
(1)图解
(2)分页Main测试
public static void main(String[] args) { //老师,分页1,当前页下标从0开始 //pageQuery1(3,1);//第二页:序号为4,即从序号4开始,把第二页的所有数据显示出来 //my分页2,改进,当前页下标从1开始(正常的日常分页) //pageQuery2(3,2);//第一页:(3,1)每页显示3条记录,当前页为1 } /* * pageSize:表示每页显示的记录数量 * pageno:表示显示的是第几页(数字是从1开始) * * pageIndex:表示显示的是第几页(数字是从0开始) 选择 * 当pageIndex=2时,意味着当前显示的是第3页 * 所以(3,2)表示的是每条记录显示3条,第三页的第一个序号为7 * 即:从序号为7开始,把第三页的所有数据输出 * (3,2)=6--> 7 * * 0,1,2,3,4,5,6 */ private static void pageQuery1(int pageSize,int pageIndex) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p from Person p "; Query q = s.createQuery(hql); q.setMaxResults(pageSize);//这个参数设定的其实是每页记录总数 q.setFirstResult(pageSize*pageIndex);//每页的第一条记录所在序号公式 List<Person> list = q.list(); for(Person p: list){ System.out.println(p); } tx.commit(); s.close(); sf.close(); } /* * 如果选用 * pageno:表示显示的是第几页(数字是从1开始) * (3,1)每页显示3条记录,当前页为1 * pagesize(pageno-1)+1-->当前序号,需-1 * * */ private static void pageQuery2(int pageSize,int pageno) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p from Person p "; Query q = s.createQuery(hql); q.setMaxResults(pageSize);//这个参数设定的其实是每页记录总数 q.setFirstResult(pageSize*(pageno-1));//每页的第一条记录所在序号公式 List<Person> list = q.list(); for(Person p: list){ System.out.println(p); } tx.commit(); s.close(); sf.close(); } }
二、多表连接查询
(1)图解
(2)多表连接查询Main测试
package com.nf; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.query.Query; import shuang.m2oo2m.Person; import util.HibernateUtil; public class Main { public static void main(String[] args) { //多表连接 //内连接 //innerQuery(); //左外连接1:以Person为主表 //leftOuterJoin(); //左外连接2:以ClassInfo为主表 //leftOuterJoin2(); fetchJoin() ; } private static void innerQuery() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); /*inner join p.ci中的p.ci是通过实体对象Person中的属性ci映射找到对应的班级信息而来*/ String hql = "select p from Person p inner join p.ci c "; Query q = s.createQuery(hql); List<Person> list = q.list(); for(Person p: list){ System.out.println(p); } tx.commit(); s.close(); sf.close(); /*结果 Person [pid=p1, name=p111] Person [pid=p2, name=p222] Person [pid=p3, name=p333] Person [pid=p4, name=p444] * */ } private static void leftOuterJoin() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p from Person p left join p.ci c "; Query q = s.createQuery(hql); List<Person> list = q.list(); for(Person p: list){ System.out.println(p); } /*结果 Person [pid=p1, name=p111] Person [pid=p2, name=p222] Person [pid=p3, name=p333] Person [pid=p4, name=p444] Person [pid=p5, name=p555] * */ tx.commit(); s.close(); sf.close(); } /* * 此方法演示的是,链接的时候,如果select多个实体 * 返回的是对象数组,而不是Person 的list集合 * 这样导致取数据的时候麻烦 */ private static void leftOuterJoin2() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p,c from Person p left join p.ci c "; Query q = s.createQuery(hql); List<Object[]> list = q.list(); for(Object[] data: list){ System.out.println(data[0]); System.out.println(data[1]); } /*结果 Person [pid=p1, name=p111] ClassInfo [cid=c111, name=banji1] Person [pid=p2, name=p222] ClassInfo [cid=c111, name=banji1] Person [pid=p3, name=p333] ClassInfo [cid=c222, name=banji2] Person [pid=p4, name=p444] ClassInfo [cid=c222, name=banji2] Person [pid=p5, name=p555] null * */ tx.commit(); s.close(); sf.close(); } /*返回的是整个Person类里有的所有属性*/ private static void fetchJoin() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); String hql = "select p from Person p left join fetch p.ci c "; Query q = s.createQuery(hql); List<Person> list = q.list(); for(Person p: list){ System.out.println(p); System.out.println(p.getCi().getName()); } /*结果: Person [pid=p1, name=p111] banji1 Person [pid=p2, name=p222] banji1 Person [pid=p3, name=p333] banji2 Person [pid=p4, name=p444] banji2 Person [pid=p5, name=p555] * */ tx.commit(); s.close(); sf.close(); } }
/*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/