类级别查询
@Test public void func7() { //------------------------------------------------------------------------ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- //关联级别查询 调用时 才加载 注意:使用懒加载时要确保,调用属性加载数据时,session还是打开的.不然会抛出异常 Customer c = session.load(Customer.class, 2l); //无任何策略 Customer a = session.get(Customer.class, 2l); System.out.print(c); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
get方法:没有任何策略. 调用即立即查询数据库加载数据.
load方法: 应用类级别的加载策略 使用才会加载
lazy属性默认是true 查询类是返回代理对象
当lazy:false. load方法会与get方法没有任何区别.调用时即加载数据.
//lazy 和 fetch 属性 //fetch select @Test public void func9() { //关联级别 延迟加载 //lazy:true 使用时才加载集合数据 //fetch:select // //------------------------------------------------------------------------ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- Customer c = session.load(Customer.class, 2l); SetlinkMans = c.getLinkMans();//关联级别 System.out.print(linkMans); //!--------------------------------------------------------------------- tx.commit(); session.close(); //lazy:false 立即加载集合数据 //fetch:select //lazy:extra 与true 懒加载差不多 只获得集合的size 只查询集合的size //fetch:select //fetch join 多表查询 //lazy:true|false|extra 失效 没有意义 都是立即加载 } //fetch:subselect public void func10() { //子查询 //lazy:true 懒加载 使用才加载 //fetch:subselect 子查询 // //------------------------------------------------------------------------ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "from Customer"; Query query = session.createQuery(hql); List list = query.list(); //!--------------------------------------------------------------------- tx.commit(); session.close(); //lazy:false 立即加载 //fetch:subselect 子查询 立即加载以及子查询 //lazy:extra 懒加载效果一样 //fetch:subselect 子查询 立即加载以及子查询 } public void function1() { //子查询 //fatch 决定加载的sql语句 //反查询 //select 单表查询 默认 //join 多表查询 //lazy 决定加载时机 /*false 立即加载 * proxy 代理决定 默认 由一方决定 就是一放的lazy * * * proxy select 使用时加载 * * false select 全加载 * * false join 多表查询 lazy失效 * * proxy join 多表查询 lazy失效 * */ //------------------------------------------------------------------------ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- LinkMan lmLinkMan = session.get(LinkMan.class, 1l); Customer customer = lmLinkMan.getCustomer(); System.out.print(customer); //!--------------------------------------------------------------------- tx.commit(); session.close(); /* * no-session问题解决 扩大session的作用范围 * 利用filter * * 创建session * * chain.dofilter(req,res); * * 销毁session * * * */ }
结论 用默认的就好
HQL查询 hibernate Query Language
public void hql() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); /*取数据list customer 所有的*/ String hql = "from Customer"; Query query = session2.createQuery(hql);
//不止一个值使用list()方法 即可返回一个list Listcustomers = query.list(); System.out.println(customers); /*提交*/ txTransaction.commit(); session2.close(); sessionfactory.close(); }
添加where条件 使用 :变量名 来让hibernate知道这是个需要替换的变量
public void hql_rule_include_symbol() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); /*找数据*/ /*HQL语句中 不可能出现数据库名 */ String hql2 = "from Customer where cust_id = :abc"; Query query = session2.createQuery(hql2);
//替换变量 query.setParameter("abc", 1l);
//得到唯一值 使用uniqueResult方法 Customer c = (Customer)query.uniqueResult(); System.out.println(c); /*提交*/ txTransaction.commit(); session2.close(); sessionfactory.close(); }
分页
@Test public void hql_page() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); /*取数据*/ String hql = "from Customer"; Query query = session2.createQuery(hql);
//设置第几个开始
query.setFirstResult(0);
//数多少个 query.setMaxResults(100); //query.setParameter("abc", 1l); Listcustomers = query.list(); /*提交*/ txTransaction.commit(); session2.close(); sessionfactory.close(); }
跨表
public void hql_page2() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); /*取数据 customer为1的所有linkman*/ String hql = "from Customer c,LinkMan l where l.customer = c.cust_id and c.cust_id = :id"; Query query = session2.createQuery(hql); //设置第几个开始 query.setParameter("id", 1l); List
排序
public void func2() { //排序 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = " from Customer order by cust_id desc"; //查询object子类的对象 所有注册的对象 Query query = session.createQuery(hql); Listlist = query.list(); System.out.println(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
count
public void func5() { //查条数 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "select count(*) from Customer"; Query query = session.createQuery(hql); //query.setParameter("id","id"); Number count = (Number) query.uniqueResult(); System.out.println(count); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
查询所需要的值
public void func7() { //查询所需字段的值 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "select cust_name,cust_id from Customer"; Query query = session.createQuery(hql); List
封装对象
public void func8() { //封装对象 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "select new Customer(cust_name,cust_id) from Customer"; Query query = session.createQuery(hql); Listlist = query.list(); System.out.println(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
内链接
@Test public void func9() { // 内连接 //隐式连接 /* * select * from a,b where b.aid = a.id * 显示内连接 * * * 左外 * select * from A left [outer] join b on b.aid = a.id * select * from A right [outer] join b on b.aid = a.id * */ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c inner join c.linkMans "; Query query = session.createQuery(hql); List
迫切内连接
public void func10() { //HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "from Customer c inner join fetch c.linkMans"; Query query = session.createQuery(hql); Listlist = query.list(); System.out.println(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
左外以及右外
@Test public void func11() { /* * 左外 * select * from A left [outer] join b on b.aid = a.id * select * from A right [outer] join b on b.aid = a.id * */ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- String hql = "from Customer c left join c.linkMans"; String hqlString = "from Customer c right join c.linkMans"; //查询object子类的对象 所有注册的对象 Query query = session.createQuery(hqlString); //query.setParameter("id","id"); List
Criteria查询 可用于单表条件查询 Hibernate自创的无语句面向对象查询
查列表
@Test public void func1() { Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- Criteria criteria = session.createCriteria(Customer.class); Listlist = criteria.list(); System.out.print(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
条件
public void None_Sql() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); /*存数据*/ /*HQL语句中 不可能出现数据库名*/ Criteria criteria = session2.createCriteria(Customer.class); /*条件查询*/ // > gt /* >= ge * < lt * <= le * == eq * != ne * in in * between and between * like like * is not null isnotnull * is null isNull * or or * and and * * * * */ //添加条件 使用Restrictions criteria.add(Restrictions.eq("cust_id", 1l)); Customer customer =(Customer) criteria.uniqueResult(); /*提交*/ txTransaction.commit(); session2.close(); sessionfactory.close(); }
分页
@Test public void func3() { //分页 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- Criteria criteria = session.createCriteria(Customer.class); criteria.setFirstResult(0); criteria.setMaxResults(10); Listlist = criteria.list(); System.out.print(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
排序
@Test public void func4() { //排序 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- Criteria criteria = session.createCriteria(Customer.class);
//排序条件 criteria.addOrder(Order.asc("cust_id")); Listlist = criteria.list(); System.out.print(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
设置查询目标
@Test public void func5() { //设置查询目标 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!--------------------------------------------------------------------- Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.rowCount()); Listlist = criteria.list(); System.out.print(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
离线criteria
@Test public void func6() { //离线criteria DetachedCriteria dCriteria = DetachedCriteria.forClass(Customer.class); dCriteria.add(Restrictions.idEq(6l)); //------------------------------------------------------------------------ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //!---------------------------------------------------------------------
//为离线传入session Criteria criteria = dCriteria.getExecutableCriteria(session); Listlist = criteria.list(); System.out.print(list); //!--------------------------------------------------------------------- tx.commit(); session.close(); }
原生sql
public void origin_sql() { Configuration configuration = new Configuration().configure(new File("src/hibernate.cfg.xml")); SessionFactory sessionfactory = configuration.buildSessionFactory(); Session session2 = sessionfactory.openSession(); Transaction txTransaction = session2.beginTransaction(); String sql = "select * from cst_customer"; SQLQuery query = session2.createSQLQuery(sql); //制定结果集 query.addEntity(Customer.class); //多个 Listlist = query.list(); //一个 query.uniqueResult(); //条件 String sql2 = "select * from cst_customer where cust_id = ?"; SQLQuery query2 = session2.createSQLQuery(sql); //制定结果集 query2.setParameter(0, 1); //多个 //一个 Customer customer = (Customer) query.uniqueResult(); //分页 String sql3 = "select * from cst_customer limit ?,?"; SQLQuery query3 = session2.createSQLQuery(sql); //制定结果集 query3.setParameter(0, 0); query3.setParameter(1, 10); //多个 List list2 = query.list(); //一个 query.uniqueResult(); /*提交*/ txTransaction.commit(); session2.close(); sessionfactory.close(); }