標籤:user cas close 參數 import 條件查詢 rgs length minimal
hibernate.cfg.xml配置:<?xml version=‘1.0‘ encoding=‘UTF-8‘?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://127.0.0.1/java?characterEncoding=utf-8</property><property name="connection.username">root</property><property name="connection.password">root</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property><property name="show_sql">true</property><property name="format_sql">true</property><mapping resource="com/etc/entity/Person.hbm.xml" /><mapping resource="com/etc/entity/Sex.hbm.xml" /></session-factory></hibernate-configuration>=============================================2個實體:package com.etc.entity;/** * Person entity. @author MyEclipse Persistence Tools */public class Person implements java.io.Serializable {// Fieldsprivate Integer pid;private Sex sex; //n端持有1端的1個關聯對象private String pname;// Constructors/** default constructor */public Person() {}/** full constructor */public Person(Sex sex, String pname) {this.sex = sex;this.pname = pname;}// Property accessorspublic Integer getPid() {return this.pid;}public void setPid(Integer pid) {this.pid = pid;}public Sex getSex() {return this.sex;}public void setSex(Sex sex) {this.sex = sex;}public String getPname() {return this.pname;}public void setPname(String pname) {this.pname = pname;}@Overridepublic String toString() {return "Person [pid=" + pid + ", pname=" + pname + ", sex=" + sex.getSexname() + "]";}}===============package com.etc.entity;import java.util.HashSet;import java.util.Set;/** * Sex entity. @author MyEclipse Persistence Tools */public class Sex implements java.io.Serializable {// Fieldsprivate Integer sexid;private String sexname;private Set persons = new HashSet(0); //1 端持有N端的1個集合對象// Constructors/** default constructor */public Sex() {}@Overridepublic String toString() {return "Sex [persons的數量:" + persons.size() + ", sexid=" + sexid + ", sexname="+ sexname + "]";}/** minimal constructor */public Sex(String sexname) {this.sexname = sexname;}/** full constructor */public Sex(String sexname, Set persons) {this.sexname = sexname;this.persons = persons;}// Property accessorspublic Integer getSexid() {return this.sexid;}public void setSexid(Integer sexid) {this.sexid = sexid;}public String getSexname() {return this.sexname;}public void setSexname(String sexname) {this.sexname = sexname;}public Set getPersons() {return this.persons;}public void setPersons(Set persons) {this.persons = persons;}}=============Person.hbm.xml配置:<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.etc.entity.Person" table="person" catalog="java" lazy="true"> <id name="pid" type="java.lang.Integer"> <column name="pid" /> <generator class="identity" /> </id> <many-to-one name="sex" class="com.etc.entity.Sex" lazy="false" cascade="save-update"> <column name="sexid" not-null="true"/> </many-to-one> <property name="pname" type="java.lang.String"> <column name="pname" length="20" not-null="true" /> </property> </class> <query name="find_unusual_sex"> from Person where sex.sexname not in(‘男性‘,‘女性 ‘) </query> <sql-query name="find_max_count_sex"> select t1.*,sex.sexname from (select count(1) 人口,sexid from person group by sexid) t1,sex where t1.人口 = (select max(人口) from (select count(1) 人口,sexid from person group by sexid) t2 ) and t1.sexid = sex.sexid; </sql-query></hibernate-mapping>============================Sex.hbm.xml配置:<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.etc.entity.Sex" table="sex" catalog="java"> <id name="sexid" type="java.lang.Integer"> <column name="sexid" /> <generator class="identity" /> </id> <property name="sexname" type="java.lang.String"> <column name="sexname" length="20" not-null="true" unique="true" /> </property> <set name="persons" inverse="true" lazy="false" outer-join="false" cascade=""> <key> <column name="sexid" not-null="true" /> </key> <one-to-many class="com.etc.entity.Person" /> </set> </class></hibernate-mapping>==========================測試類別:hql查詢.javapackage com.etc.test;import java.util.List;import java.util.Properties;import org.hibernate.Query;import org.hibernate.SQLQuery;import org.hibernate.Session;import com.etc.dao.HibernateSessionFactory;import com.etc.entity.Person;import com.etc.entity.Sex;public class hql查詢 {static void 完整對象的查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//2 構造hql語句String hql = "from Person where pname like ‘老%‘";//3 執行查詢List<Person> list = s.createQuery(hql).list();//4 遍曆結果for(Person p:list){System.out.println(p);}//5 關閉串連HibernateSessionFactory.closeSession();}static void 部分欄位的查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//2 構造hql語句。只返回部分欄位String hql = "select pid,pname from Person where pname like ‘老%‘";//3 執行查詢List<Object[]> list = s.createQuery(hql).list();//4 遍曆for(Object[] fields:list){//把每條記錄的欄位顯示查詢for(Object field:fields){System.out.print(field+"\t");}System.out.println();}//5 關閉串連HibernateSessionFactory.closeSession();}static void 關聯條件查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//a 顯式關聯(inner join關鍵字)//2 構造hql語句String hql = "from Person as p inner join p.sex as s " +"where s.sexname like ‘男%‘";//3 執行查詢.獲得主對象和關聯對象List<Object[]> list = s.createQuery(hql).list();//4 遍曆結果for(int i=0;i<list.size();i++){Person p = (Person) list.get(i)[0];System.out.println(p);}//b 隱式關聯.推薦使用/*2 構造hql語句。使用關聯對象的屬性作為約束條件。String hql = "from Person where sex.sexname like ‘女%‘";//3 執行查詢List<Person> list = s.createQuery(hql).list();//4 遍曆結果for(Person p:list){System.out.println(p);}*///5 關閉串連HibernateSessionFactory.closeSession();}static void 彙總查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//2 構造hql語句/*String hql = "select ‘記錄的總數:‘||cast(count(pid),string) from Person";List<Object> list = s.createQuery(hql).list();System.out.println(list.get(0));*///分組查詢,統計各個性別的人數String hql = "select ‘人數:‘||cast(count(pid),string),‘性別名:‘||sex.sexname from Person " +"group by sex";List<Object[]> list = s.createQuery(hql).list();for(Object[] fields:list){//把每條記錄的欄位顯示查詢for(Object field:fields){System.out.print(field+"\t");}System.out.println();}//5 關閉串連HibernateSessionFactory.closeSession();}static void 分頁查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//2 構造hql語句String hql = "from Person where pname like ‘老%‘";//3 執行查詢.第3到6條記錄Query q = s.createQuery(hql);q.setFirstResult(2);//設定起點q.setMaxResults(4);//設定條數List<Person> list = q.list();//4 遍曆結果for(Person p:list){System.out.println(p);}//5 關閉串連HibernateSessionFactory.closeSession();}static void 命名查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//3 執行查詢List<Person> list = s.getNamedQuery("find_unusual_sex").list();//4 遍曆結果for(Person p:list){System.out.println(p);}//5 關閉串連HibernateSessionFactory.closeSession();}static void 動態參數查詢() //支援?的hql語句{//1 擷取串連Session s = HibernateSessionFactory.getSession();//a ?作為動態參數//String hql = "from Person where pname like ?";//b 取參數別名。//c 設定查詢屬性//d 關聯查詢String hql = "from Person where sex =:sex";//String hql = "from Person where pname like :name " +//"and sex.sexname =:sex";Query q = s.createQuery(hql);//a 按照位置指定//q.setString(0,"%白%"); //與jdbc不同,從0開始/*b 按照別名進行指定q.setParameter("name", "%老%");q.setParameter("sex", "女性");*//*c 採用構造"查詢屬性"的方法Properties pro = new Properties();pro.setProperty("name","%老%");pro.setProperty("sex", "女性");q.setProperties(pro);*///d 設定關聯對象作為查詢條件Sex sex = new Sex();sex.setSexid(2); //只有sex.主鍵作為查詢條件。q.setEntity("sex", sex);//將對象作為sex參數的查詢條件//3 執行查詢List<Person> list = q.list();//4 遍曆結果for(Person p:list){System.out.println(p);}//5 關閉串連HibernateSessionFactory.closeSession();}static void sql的查詢(){//1 擷取串連Session s = HibernateSessionFactory.getSession();//2 構造sql語句。找出人數最多的性別名,及它的人數 SQLQuery q = (SQLQuery) s.getNamedQuery("find_max_count_sex");//3 執行查詢List<Object[]> list = q.list();//4 遍曆for(Object[] fields:list){//把每條記錄的欄位顯示查詢for(Object field:fields){System.out.print(field+"\t");}System.out.println();}//5 關閉串連HibernateSessionFactory.closeSession();}public static void main(String[] args) {//完整對象的查詢();//部分欄位的查詢();//關聯條件查詢();//彙總查詢();//分頁查詢();//命名查詢();//動態參數查詢();sql的查詢();}}
Hibernate---Hql查詢2---