关闭→
当前位置:科普经验站>综合知识>hibernate查询语句怎么写

hibernate查询语句怎么写

科普经验站 人气:2.51W
1. hibernate 查询语句

因为hibernate是封装了JDBC的框架。

hibernate查询语句怎么写

文中第一句:String queryString ="from *****";我想楼主应该知道这是HQL语句,TbArea 是你在hibertate 配置文件中定义的名称.propertyName + "= ?";是属性名

这句话可以看做正常的sql语句是

select * from TbArea as a where a.属性名 = 你在程序中传过来的参数;

第二句:

Query queryObject = getSession().createQuery(queryString);

是hibernate里面封装好的方法,取得你session对象,并创建Query对象。。该对象在Hibernate中你可以了解为jdbc的statement对象.

queryString就是你执行的sql语句.

第三句:

queryObject.setParameter(0, value);

queryObject我想楼主不是很熟练JDBC吧。这个对象基本和statement相似.这种写法是最常用的,优点我不太记得。

setParameter也就是设置你第一句HQL语句中的问号。设置参数.value为你方法中传进来的参数

return queryObject.list();返回查询到的多行数据,做为列表形式返回。

2. hibernate添加语句怎么写

不是有映射文件吗?

比如你的数据库表为student

那么的你就应该有hibernate.cfg.xml配置文件和他的映射文件hibernate.hbm.xml,最后还应该有一个student表的映射类student.java

这3个文件.

然后你建一个实现类studentInsert

实现是

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

student st=new student();

st.setId("0001");

st.setUserName("Wang");

st.setpassWord("123");

session.save(st);

tx.commit();

session.close();

就是这样.

3. java连接mysql 用hibernate怎么写查询语句

第一种方式:HQL(Hibernate Query Languge,Hibernate 查询语言)查询是一种面向对象的查询语言,其中没有表和字段的概念,只有类、对象和属性的概念,HQL 是应用较为广泛的方式语法:[select/update/delete……] from Entity [where……] [group by……] [having……] [order by……](1).没使用Spring框架的写法:使用HQL需要四步得到Session、编写HQL语句、创建Query对象(Query接口是HQL 查询接口。

它提供了各种的查询功能)、执行查询,得到结果 sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); String hql = “from Street”; Query query = session.createQuery(hql); List list = query.list();(2).使用Spring框架的写法:String queryString = "select form entity 。.";List list=getHibernateTemplate().find(queryString);第二种方式:Criteria 查询Criteria 查询采用面向对象方式封装查询条件,又称为对象查询;就是对SQL 语句进行封装,采用对象的方式来组合各种查询条件由Hibernate 自动产生SQL 查询语句(1).没使用Spring框架的写法;Criteria由Hibernate Session进行创建SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(User.class); List result = criteria.list(); Iterator it = result.iterator(); (2)使用Spring框架的写法:import org.hibernate.criterion.DetachedCriteria;DetachedCriteria criteria=DetachedCriteria.forClass(ObjectEntity.class);criteria.add(Restrictions.eq("propertyName", propertyValue));List result=getHibernateTemplate().findByCriteria(criteria);第三种方式:使用SQL语句查询(以下都是使用了Spring框架的写法)1).这是把执行结果放到了一个类里:(这个类通常使用VO实体,VO实体一般就只用来接收查询结果)List list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { StringBuffer hqlBuffer = new StringBuffer(""); hqlBuffer.append("select column_Name from 。

");//里面是SQL语句 SQLQuery sqlQuery = session.createSQLQuery(hqlBuffer.toString()); sqlQuery.addScalar("propertyName",Hibernate.STRING);//该propertyName是 ObjectVO实体的一个属性 sqlQuery.setResultTransformer(Transformers.aliasToBean(ObjectVO.class)); List list = sqlQuery.list(); return list;//此处list集合中存放的是ObjectVO对象 } });2).返回结果放到list中的: final String queryString = "";//sql语句 List resultList=getHibernateTemplate().executeFind(new HibernateCallback() { public List doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery sqlQuery = session.createSQLQuery(queryString); List list=sqlQuery.executeUpdate(); return list; } });3).无返回结果: final String queryString = "";//SQL语句 getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { SQLQuery sqlQuery = session.createSQLQuery(queryString); sqlQuery.executeUpdate(); return null; } });。

4. hibernate查询语言

1 .from 1.1单表查询 from eg.cat as cat.其中,cat只是一个别名,为了用其他子语句的时候书写简单 1.2多表查询 from eg.Cat,eg.Dog from eg.Cat as cat,eg.Dog as dog 2 join相关 (inner) join left (outer) join right (outer) join full join HQL同样对SQL中的这些特性支持 下面插播一个小话题,关于上边的那些特性,我一直都没怎么用,今天既然说到这里,就想 把上边的几个特性的用法说一下,也算对自己的一个补充: 假设有两个表:部门、员工,下面列举一些数据: 员工(Employee): ID Name DepNo 001 Jplateau 01 002 Jony 01 003 Camel 02 部门(Department): ID Name 01 研发部 02 营销部 在Hibernate中我们操纵的都是对象,所以我们操纵的是部门类和员工类 1).(inner) join select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee join Department as department on employee.DepNo= department.ID (注意到条件语句我用on 没有用where) 那么执行结果是什么呢? id1 name1 id2 name2 ++++++++++++++++++++++++++++++++++++++ 001 Jplateau 01 研发部 002 Jony 01 研发部 2).left (outer) join select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee left join Department as department on employee.DepNo= department.ID 那么执行结果又该是什么呢? id1 name1 id2 name2 ++++++++++++++++++++++++++++++++++++++ 001 Jplateau 01 研发部 002 Jony 01 研发部 003 Camel null null {就是说此时我要已第一个表的记录多少为准,第二个表中没有相应纪录的时候填充null} 3). right (outer) join select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee right join Department as department on employee.DepNo= department.ID 那么执行结果又该是什么呢? id1 name1 id2 name2 ++++++++++++++++++++++++++++++++++++++ 001 Jplateau 01 研发部 002 Jony 01 研发部 null null 02 营销部 {就是说此时我要已第二个表的记录多少为准,第一个表中没有相应纪录的时候填充null} 3 大小写敏感 4。

select语句 就是要确定你要从查询中返回哪些对象或者哪些对象的属性。写几个例子吧: select employee form Employee as employee select employee form Employee as employee where employee.Name like 'J%' select employee.Name form Employee as employee where employee.Name like 'J%' select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 from Employee as employee right join Department as department on employee.DepNo= department.ID select elements(employee.Name) from Employee as employee (不明白elements到底是做什么用的?望给于说明) 等等 5。

数学函数 JDO目前好像还不支持此类特性。 avg(。

), sum(。), min(。

), max(。) count(*) count(。

), count(distinct 。), count(all。

) 其用法和SQL基本相同 select distinct employee.name from Employee as employee select count(distinct employee.name),count(employee) from Employee as employee 6。polymorphism (暂时不知道如何解释?) from com.test.Animal as animal 不光得到所有Animal得实例,而且可以得到所有Animal的子类(如果我们定义了一个子类Cat) 一个比较极端的例子 from java.lang.Object as o 可以得到所有持久类的实例 7。

where语句 定义查询语句的条件,举几个例子吧: from Employee as employee where employee.Name='Jplateau' from Employee as employee where employee.Name like 'J%' from Employee as employee where employee.Name like '%u' 在where语句中“=”不光可以比较对象的属性,也可以比较对象,如: select animal from com.test.Animal as animal where animal.name=dog 8。表达式 在SQL语句中大部分的表达式在HQL中都可以使用: mathematical operators +, -, *, / binary comparison operators =, >=, <=, <>, !=, like logical operations and, or, not string concatenation || SQL scalar functions like upper() and lower() Parentheses ( ) indicate grouping in, between, is null JDBC IN parameters ? named parameters :name, :start_date, :x1 (这种应该是另一种"?"的变通解决方法) SQL literals 'foo', 69, '1970-01-01 10:00:01.0' Java public static final constants eg.Color.TABBY 其他不必解释了,在这里我只想对查询中的参数问题说明一下: 大家知道在SQL中进行传递参数进行查询的时候,我们通常用PreparedStatement,在语句中写一大堆的“?”, 在hql中也可以用这种方法,如: List mates = sess.find( "select employee.name from Employee as employee " + "where employee.Name=? ", name, Hibernate.STRING ); (说明:上面利用Session里的find方法,在hibernate的api Session中重载了很多find方法,它可以满。

TAG标签:#语句 #hibernate #