标签:hibernate
在web环境下,hibernate配置文件需要配置,数据库驱动,方言
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
hibernate的查询方式
1、hql(hibernate query language) query
2、Criteria query
3、Native query
总结:
1、如果页面上要显示的数据和数据库中的数据相差甚远,利用带select的构造器进行查询是比较好的方案
2、如果页面上要显示的数据和数据库中的数据相差不是甚远,这个时候用迫切连接
3、如果采用迫切连接,from后面跟的那个对象就是结构主体
4、如果多张表进行查询,找核心表
public class HQLDao extends HiberanteUtils{ public static HQLDao getInstanse() { return new HQLDao(); } /** * 单表 * * 一对多和多对多 * * 多表的结合 * @author Think * */ public List<Classess> queryAllClasses(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess").list(); session.close(); return cList; } public List queryClasses_Properties(){ Session session = sessionFactory.openSession(); List cList = session.createQuery("select cid,cname from Classess").list(); session.close(); return cList; } public List<Classess> queryClasses_Constructor(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess").list(); session.close(); return cList; } public Classess queryClasses_Condition(){ Session session = sessionFactory.openSession(); Query query = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess where cid=:cid"); query.setLong("cid", 1L); Classess classes = (Classess)query.uniqueResult(); System.out.println(classes.getCname()); session.close(); return classes; } public Classess queryClasses_Condition_2(){ Session session = sessionFactory.openSession(); Query query = session.createQuery("select new cn.itcast.domain.Classess(cname,description) from Classess where cid=?"); query.setLong(0, 1L); Classess classes = (Classess)query.uniqueResult(); System.out.println(classes.getCname()); session.close(); return classes; } /** * order by,group by,sun,min,max,avg,having等都适用 * @return */ /** * 子查询 */ public void queryClasses_SubSelect(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess where cid in(select cid from Classess where cid in(1,2,3))").list(); session.close(); } public static HQLDao getInstance(){ return new HQLDao(); } /*********************************************************************************************/ /** * 一对多 * 等值连接 查询出来的机构很差 * 内连接 * 左外连接 * 迫切左外连接 */ public List<Classess> queryClasses_Student_EQ(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c,Student s where c.cid=s.classess.cid").list(); session.close(); return cList; } /** * 内连接 * @return */ public List<Classess> queryClasses_Student_INNER(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c inner join c.students").list(); session.close(); return cList; } /** * 迫切内连接 * @return */ public List<Classess> queryClasses_Student_INNER_FETCH(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c inner join fetch c.students").list(); session.close(); return cList; } /** * 左外连接 */ public List<Classess> queryClasses_Student_LeftJoin(){ Session session = sessionFactory.openSession(); List<Classess> cList = session.createQuery("from Classess c left outer join c.students").list(); session.close(); return cList; } /** * 迫切左外连接 */ public List<Classess> queryClasses_Student_LeftJoin_fetch(){ Session session = sessionFactory.openSession(); String hql = "from Classess c left outer join fetch c.students"; hql = "from Student s left outer join fetch s.classess c"; List<Classess> cList = session.createQuery(hql).list(); session.close(); return cList; } /** * 带select的查询 */ //public List<Classess> queryClasses_Student_Select(){ //Session session = sessionFactory.openSession(); //String hql = "select new cn.itcast.domain.ClassesView(c.cname,s.sname) " + // "from Student s left outer join s.classes c"; //List<Classess> cList = session.createQuery(hql).list(); //session.close(); //return cList; //} /** * 多对多 */ public void testQueryCourse_Student(){ Session session = sessionFactory.openSession(); List<Student> studentList = session.createQuery("from Student s inner join fetch s.courses c").list(); session.close(); } /** * 一对多结合多对多 */ public List<Student> queryClasses_Student_Course(){ Session session = sessionFactory.openSession(); String hql = "from Classes cs inner join fetch cs.students s inner join fetch s.courses c"; hql = "from Student s inner join fetch s.classes cs inner join fetch s.courses c"; //hql = "from Classes cs left outer join fetch cs.students s left outer join fetch s.courses c"; List<Student> cList = session.createQuery(hql).list(); //Set<Classes> cset = new HashSet<Classes>(cList); //cList = new ArrayList<Classes>(cset); System.out.println(cList.size()); //for(Classes classes:cList){ //System.out.println(classes.getCid()); //Set<Student> students = classes.getStudents(); //for(Student student:students){ //System.out.println(student.getSname()); //Set<Course> courses = student.getCourses(); //for(Course course:courses){ //System.out.println(course.getCname()); //} //} //} session.close(); return cList; } }
标签:hibernate
原文地址:http://pengya123.blog.51cto.com/8467424/1811635