Hibernate 集中常用的查询方式
来源:互联网 发布:java flag标志位 编辑:程序博客网 时间:2024/05/18 02:11
Hibernate一共有四种查询方式:
主键查询,HQL查询(hibernate特有的查询(实体)),QBC查询(完全面向对象的查询(实体)),SQL查询(数据库表)
package cn.itcast.hello;import java.util.List;import org.hibernate.Criteria;import org.hibernate.Query;import org.hibernate.SQLQuery;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;import org.hibernate.criterion.Criterion;import org.hibernate.criterion.Restrictions;import org.junit.Test;public class AppTest3 {private static SessionFactory factory;static{factory=new Configuration().configure().buildSessionFactory();//链式调用}//主键查询@Testpublic void testQueryById(){Session session = factory.openSession();Transaction transaction = session.beginTransaction();//主键查询Employee emp = (Employee) session.get(Employee.class, 1);System.out.println(emp);transaction.commit();session.close();}//HQL查询@Testpublic void testQuery(){Session session = factory.openSession();Transaction transaction = session.beginTransaction();Query query = session.createQuery("from Employee");//这里是JavaBean中的属性不是数据库的List list = query.list();System.out.println(list);transaction.commit();session.close();}//完全面向对象的查询@Testpublic void testQBC(){Session session = factory.openSession();Transaction transaction = session.beginTransaction();Criteria criteria = session.createCriteria(Employee.class);//条件criteria.add(Restrictions.eq("empName", "韩玮"));List list = criteria.list();System.out.println(list);transaction.commit();session.close();}@Testpublic void testSQL(){Session session = factory.openSession();Transaction transaction = session.beginTransaction();//把查询的每一行记录封装成对象添加到集合中SQLQuery query = session.createSQLQuery("select * from employee").addEntity(Employee.class);List list = query.list();System.out.println(list);transaction.commit();session.close();}}运行结果一致:
Hibernate: select employee0_.id as id0_0_, employee0_.empName as empName0_0_, employee0_.workDate as workDate0_0_ from employee employee0_ where employee0_.id=?
Employee [id=1, empName=韩玮, workDate=2015-07-26 23:07:29.0]
0 0
- Hibernate 集中常用的查询方式
- Hibernate的集中查询方式 : hql查询,QBC查询和QBE查询
- Hibernate的集中查询方式 : hql查询,QBC查询和QBE查询
- hibernate 三种常用的查询方式
- Hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- Hibernate查询,常用的指定返回类型方式
- Hibernate的QBE查询方式
- 博客有感
- Thinkphp3.2 上传详解
- Java中多线程小结
- Python request第三方库的安装
- 数据表操作(一)
- Hibernate 集中常用的查询方式
- POJ 1088 滑雪(记忆化搜索+DFS)
- OC内存管理
- static_cast, dynamic_cast, reinterpret_cast, const_cast区别
- LeetCode 203 Remove Linked List Elements
- Java中集合小结
- Intent 跳转发短信、打电话、到设置界面等
- VisualSVN Server安装过程
- RecyclerView的拖动和滑动 第一部分 :基本的ItemTouchHelper示例