Hibernate检索(上)
2021-06-27 09:04
                         标签:username   coding   result   数据   检索   引用   自定义函数   port   images    什么是HQL检索  HQL(Hibernate Query Language)是面向对象的查询语言,具有丰富灵活的特性  在Hibernate提供的各种检索方式中,HQL是使用最为广泛的一种检索方式,也是官方推荐的查询模式 Hibernate检索方式的功能  可以在查询语句中设定查询条件,动态绑定参数  支持投影查询  支持分页查询  支持链接查询  支持分组查询,可以使用having和group by关键字  内置聚集函数,如sum()、min()、max()等  可调用用户自定义函数  支持子查询 HQL检索步骤     HQL查询语法结构 【select/update/delete…】 【 from …】 【 where …】 【 group by…】 【 having…】 【 order by…】 HQL语法规则 HQL语句的关键字不区分大小写,但推荐小写。  HQL中出现的类名,属性名严格区分大小写。  可以为类设置别名,以供其他地方引用,例如 as t。  as 关键字是可选的,一般别名推荐小写。  from前也可以加select 但必须配合别名使用。 1.实体类 2.配置文件 3.测试类   Hibernate检索(上) 标签:username   coding   result   数据   检索   引用   自定义函数   port   images    原文地址:http://www.cnblogs.com/liuyingke/p/7147564.html


 1 package cn.yunhe.entity;
 2 
 3 import javax.persistence.*;
 4 
 5 /**
 6  * Created by Administrator on 2017/7/10.
 7  */
 8 @Entity
 9 @Table(name = "qx")
10 public class Qx {
11     private int qxId;
12     private String qxName;
13 
14     public Qx() {
15     }
16 
17     public Qx(int qxId, String qxName) {
18         this.qxId = qxId;
19         this.qxName = qxName;
20     }
21 
22     @Id
23     @GeneratedValue
24     @Column(name = "qxid")
25     public int getQxId() {
26         return qxId;
27     }
28 
29     public void setQxId(int qxId) {
30         this.qxId = qxId;
31     }
32 
33     @Column(name = "qxname")
34     public String getQxName() {
35         return qxName;
36     }
37 
38     public void setQxName(String qxName) {
39         this.qxName = qxName;
40     }
41 }


 1 package cn.yunhe.entity;
 2 
 3 import javax.persistence.*;
 4 import java.util.Date;
 5 
 6 /**
 7  * Created by Administrator on 2017/7/10.
 8  */
 9 @Entity
10 @Table(name = "t_user")
11 public class User {
12     private int userId;
13     private String uName;
14     private String uPwd;
15     private int age;
16     private Date hiredate;
17 
18     @Id
19     @GeneratedValue
20     @Column(name = "userid")
21     public int getUserId() {
22         return userId;
23     }
24 
25     public void setUserId(int userId) {
26         this.userId = userId;
27     }
28 
29     @Column(name = "uname")
30     public String getuName() {
31         return uName;
32     }
33 
34     public void setuName(String uName) {
35         this.uName = uName;
36     }
37 
38     @Column(name = "upwd")
39     public String getuPwd() {
40         return uPwd;
41     }
42 
43     public void setuPwd(String uPwd) {
44         this.uPwd = uPwd;
45     }
46 
47     @Column(name = "age")
48     public int getAge() {
49         return age;
50     }
51 
52     public void setAge(int age) {
53         this.age = age;
54     }
55 
56     @Column(name = "heredate")
57     public Date getHiredate() {
58         return hiredate;
59     }
60 
61     public void setHiredate(Date hiredate) {
62         this.hiredate = hiredate;
63     }
64 }


 1 xml version="1.0" encoding="UTF-8"?>
 2 DOCTYPE hibernate-configuration
 3         PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 hibernate-configuration>
 6     session-factory>
 7         
 8         property name="hibernate.hbm2ddl.auto">updateproperty>
 9         
10         property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialectproperty>
11         property name="connection.url">jdbc:mysql://localhost:3306/hibernateproperty>
12         property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
13         property name="connection.username">rootproperty>
14         property name="connection.password">1234property>
15         
16         property name="show_sql">trueproperty>
17 
18         mapping class="cn.yunhe.entity.Qx"/>
19         mapping class="cn.yunhe.entity.User"/>
20     session-factory>
21 hibernate-configuration>


  1 package cn.yunhe.demo;
  2 
  3 import cn.yunhe.entity.Qx;
  4 import cn.yunhe.entity.User;
  5 import org.hibernate.Query;
  6 import org.hibernate.Session;
  7 import org.hibernate.Transaction;
  8 import org.hibernate.cfg.AnnotationConfiguration;
  9 import org.hibernate.cfg.Configuration;
 10 import org.junit.After;
 11 import org.junit.Before;
 12 import org.junit.Test;
 13 
 14 import java.util.List;
 15 
 16 /**
 17  * Created by Administrator on 2017/7/10.
 18  */
 19 public class Demo {
 20     Session session=null;
 21     Transaction tr=null;
 22     @Before
 23     public void init(){
 24         Configuration config=new AnnotationConfiguration().configure();
 25         session=config.buildSessionFactory().openSession();
 26         tr=session.beginTransaction();
 27     }
 28 
 29     @Test//查询所有的区县
 30     public void test1(){
 31         String hql="from Qx";
 32        Query query= session.createQuery(hql);
 33         List
下一篇:什么是好的电子商务网站用户体验