當前位置:首頁 » 編程語言 » hibernate的sql條件查詢
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

hibernate的sql條件查詢

發布時間: 2022-08-05 10:19:38

Ⅰ 關於Hibernate的三種查詢方式。

hql提供了更接近於傳統sql語句的查詢語法,容易過度吧,官方推薦使用,它提供強大的查詢功能。一般使用這種查詢。
Criteria查詢,可移植性好,用起來也很方便,當你的查詢條件很多時,可用Example創建一查詢依據,查出屬性與之類似的對象,代碼簡潔運用很方便。
Native SQL一般在遇到很復雜的查詢條件時使用。
說白了,靈活運用就行,在不同情況下使用不同的解決方案,建議你多看些實例,見得多了用起來就比較容易判斷該用哪一個。
如果問題解決,請點贊採納,謝謝!!!

Ⅱ Hibernate的幾種查詢方式

HQL 通過Hibernate提供的查詢語言進行查詢。Hibernate Query lanague EJBQL(JPQL 1.0) 是EJB提供的查詢語言 QBC(query by cretira)通過Cretira介面進行查詢 QBE(query by Example) 通過Example編程介面進行查詢 從功能強弱上排序:NativeSQL > HQL > EJBQL(JPQL 1.0) >QBC(query by cretira) >QBE(query by Example) 1: QBE (Query By Example) QBC查詢方式 QBC(Query By Criteria)查詢方式是 Hibernate 提供的「 更加面向對象」的一種檢索方式。 QBC 在條件查詢上比 HQL 查詢更為靈活,而且支持運行時動態天生查詢語句。 在Hibernate 應用中使用 QBC 查詢通常經過 3 個步驟 (1)使用 Session 實例的 createCriteria() 方法創建 Criteria 對象 (2)使用工具類 Restrictions 的相關方法為 Criteria 對象設置查詢對象 (3)使用 Criteria 對象的 list() 方法執行查詢,返回查詢結果QBE查詢QBE查詢就是檢索與指定樣本對象具有相同屬性值的對象。因此QBE 查詢的關鍵就是樣本對象的創建,樣本對象中的所有非空屬性均將作為查詢條件。 QBE 查詢的功能子集,固然 QBE 沒有 QBC 功能大,但是有些場合 QBE 使用起來更為方便。 工具類Example 為 Criteria 對象指定樣本對象作為查詢條件Java代碼1 Session session = HibernateSessionFactory.getSessionFactory().openSe ssion(); 2 Transaction ts = session.beginTransaction(); 3 Customer c = new Customer(); 4 c.setCname("Hibernate"); 5 Criteria criteria = session.createCriteria(Customer. class ); 6 Criteria.add(Example.create(c)); 7 Iterator it = criteria.list().iterator(); 8 ts.commit(); 9 HibernateSessionFactory.closeSession();

Ⅲ 如何用hibernate直接進行SQL語句查詢

如何用hibernate直接進行SQL語句查詢
public int getLogin(String username,String password)
{ String sql="select * from user_table where username=? and password=?";
SQLQuery query=getSession().createSQLQuery(sql).addEntity(UserTable.class);
query.setString(0,username);
query.setString(1,password);
return Integer.parseInt(query.uniqueResult().toString());

}
public static void main(String[] args) {
new UserTableDAO().getLogin("zhang","yuan");
}

Ⅳ 關於hibernate多條件查詢

你這典型的將hibernate+標准sql混合使用

xsxk."+propertyNameO+"="+valueO+"andxsxk."+propertyNameT+"="+valueT

xsxk.屬性名="+valueO+"

Ⅳ 在hibernate運用sql查詢

給你一個我寫的例子:
public List<Statistics> statistics(){
String sql="select "+
"sum(case when (t.type_code=3) then 1 else 0 end),"+
"sum(case when (t.type_code=5) then 1 else 0 end),"+
"sum(case when (t.type_code=8) then 1 else 0 end),"+
"sum(case when (t.type_code=0) then 1 else 0 end),"+
"sum(case when (t.type_code=6) then 1 else 0 end),"+
"sum(case when (t.type_code=9) then 1 else 0 end),"+
"count(*)"+
"from cs_cust_complaint_data t where t.from_flag='2'";
List<Statistics> stal = new ArrayList<Statistics>();
SQLQuery query = getSession().createSQLQuery(sql);
Object[] obj = (Object[])query.uniqueResult();
BigDecimal b0 = (BigDecimal)obj[0];
BigDecimal b1 = (BigDecimal)obj[1];
BigDecimal b2 = (BigDecimal)obj[2];
BigDecimal b3 = (BigDecimal)obj[3];
BigDecimal b4 = (BigDecimal)obj[4];
BigDecimal b5 = (BigDecimal)obj[5];
BigDecimal b6 = (BigDecimal)obj[6];
Double zs = b6.doubleValue();
DecimalFormat df=new DecimalFormat("#.00");
stal.add(new Statistics("卷煙營銷", b0.longValue(),Double.parseDouble(df.format(b0.doubleValue()/zs*100))));
stal.add(new Statistics("專賣法規", b1.longValue(),Double.parseDouble(df.format(b1.doubleValue()/zs*100))));
stal.add(new Statistics("煙葉生產", b2.longValue(),Double.parseDouble(df.format(b2.doubleValue()/zs*100))));
stal.add(new Statistics("政風行風", b3.longValue(),Double.parseDouble(df.format(b3.doubleValue()/zs*100))));
stal.add(new Statistics("人事勞資", b4.longValue(),Double.parseDouble(df.format(b4.doubleValue()/zs*100))));
stal.add(new Statistics("其他咨詢", b5.longValue(),Double.parseDouble(df.format(b5.doubleValue()/zs*100))));
return stal;
}

Ⅵ hibernate怎麼多個條件查詢

1. Hibernate多條件查詢通用方法
//value[i]為第i個查詢條件propertyName[i]的值 (本方法已通過測試) /*多條件查詢,查詢條件的值為空時自動除去該條件 * rigor為true時採用精確查詢 */ public List searchByPropertys(String model,String[]propertyName,Object[] value,int page,boolean rigor){ StringBuffer sqlBuffer = new StringBuffer(); String ralation=" like "; if(rigor){ ralation=" = "; } sqlBuffer.append("from "+model+" as model\n"); int len=propertyName.length; List list=new ArrayList(); boolean first=true; for(int i=0;i< len;i++){ if(value[i]!=null){ if(first){ sqlBuffer.append(" where "+ "model."+ propertyName[i] + ralation+" ?\n"); list.add(value[i]); first=false; }else{ sqlBuffer.append(" and "+ "model."+ propertyName[i] +ralation+ " ?\n"); list.add(value[i]); } } } try { Session session=getSession(); Query queryObject = session.createQuery(sqlBuffer.toString()); for(int i=0;i< list.size();i++){ if(rigor){ queryObject.setParameter(i, list.get(i)); }else{ queryObject.setParameter(i, "%"+list.get(i)+"%"); } } list=queryObject.list(); closeSession(session); return list; } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } }

2:hibernate多條件組合查詢 之 sql 拼接
這個方法與上面第一節中的相同,只不過上面的方法是將搜索的多個條件在外部(即調用方)封裝在了數組中。
public static void main(String[] args) { Session session = null; Transaction tx = null; List list = null; Criteria criteria = null; try { session = HibernateSessionFactory.getSession(); tx = session.beginTransaction(); DetachedCriteria detachedCriteria = DetachedCriteria .forClass(InfoTab.class); String sql=" 1=1 "; Integer pareaId = 0; // 父地區; Integer careaId = 0; // 子地區; Integer categoryId = 0; // 類別; String infoPrivider = "中介"; // 來源; String houseType= "地下室"; // 房屋類型; Integer hxBedRoom=0; // 室; Integer hxLivingRoom=0; // 廳; String hzHouseStatus="有房出租"; // 合租類型; String hzRequestSex="男"; // 性別要求; String fixUp="尚未"; // 裝修程度; Integer lcHeightMolecuse=0; // 樓層; String orientation="東南"; // 朝向要求; Integer buildArea=2000; // 建築面積; Integer useArea=80; // 使用面積; Integer rentalDigit=2000; // 租金/價格; String title= "出租"; // 標題; if(pareaId!=0) { sql+="pareaId=" + pareaId; } if(careaId!=0) { sql+=" and careaId=" + careaId; } if(categoryId!=0) { sql+=" and categoryId=" + categoryId; } if(!infoPrivider.equals("")) { sql+=" and infoPrivider='" + infoPrivider + "'"; } if(!houseType.equals("")) { sql+=" and houseType='" + houseType +"'"; } if(hxBedRoom!=0) { sql+=" and hxBedRoom=" + hxBedRoom; } if(hxLivingRoom!=0) { sql+=" and hxLivingRoom=" + hxLivingRoom; } if(!hzHouseStatus.equals("")) { sql+=" and hzHouseStatus='" + hzHouseStatus + "'"; } if(!hzRequestSex.equals("")) { sql+=" and hzRequestSex='" + hzRequestSex +"'"; } if(!fixUp.equals("")) { sql+=" and fixUp='" + fixUp + "'"; } if(lcHeightMolecuse!=0) { sql+=" and lcHeightMolecuse=" + lcHeightMolecuse; } if(!orientation.equals("")) { sql+=" and orientation='" + orientation + "'"; } if(buildArea!=0) { sql+=" and buildArea=" + buildArea; } if(useArea!=0) { sql+=" and useArea=" + useArea; } if(rentalDigit!=0) { sql+=" and rentalDigit=" + rentalDigit; } if(!title.equals("")) { sql+=" and title like '%" + title + "%'"; } sql+=" order by id desc"; System.out.println(sql); detachedCriteria.add(Restrictions.sqlRestriction(sql)); criteria = detachedCriteria.getExecutableCriteria(session); list = criteria.list(); for(int i=0;i< list.size();i++) { InfoTab infoTab = (InfoTab)list.get(i); System.out.println(infoTab.getTitle() +" "+ infoTab.getCategoryId() +" "+ infoTab.getPareaName() +" "+ infoTab.getCareaName() +" " + infoTab.getHouseType() +" " + infoTab.getInfoPrivider()); } tx.commit(); } catch (HibernateException he) { he.printStackTrace(); } }

Ⅶ hibernate sql 條件查詢問題。

可以如下進行查詢:
Criteria criteria = session.createCriteria(User.class);
List users = criteria.list();
for(Iterator it = users.iterator(); it.hasNext(); ) {
User user = (User) it.next();
System.out.println(user.getId() +
" /t " + user.getName() +
"/" + user.getAge());
}

Ⅷ hibernate使用sql查詢

看了下你的 SQL ,裡面打的是中文的問號,這個應該是英文的咧

Ⅸ hibernate怎麼用查詢

1:hibernate數據查詢方式:有HQL方式,QBC方式,原生SQL方式。HQL適合靜態查詢,QBC則適合較多的動態查詢。
A:HQL方式,支持條件查詢,連接查詢,分頁查詢,分組查詢,內置函數和自定義函數查詢(SUN(),MIN(),MAX()),子查詢,動態綁定參數查。
HQL語句定義如下:
String hql="from book";
Query query=session.createQuery(hql);
B:QBC方式,也就是QBC檢索方式。QBC通過Session類創建Criteria實例,通過不同方法進行檢索,實際上Criteria是用來裝載查詢條件的容器。QBC有很多條件函數,如:Resstictions.eq(),Resstictions.gt(),Resstictions.ge(),
Resstictions.le(),Resstictions.and(),Resstictions.or()等。
Criteria容器使用方法如下:
Criteria criteria=session.createCriteria(book.class);
criteria.add(Restrications.It("id",new Integer(4)));
List list=criteria.list();
C:原生SQL方式。不管是HQL還是QBC最終都要通過Hibernate來解析,把他們轉換成SQL語句進行對資料庫的操作。因為我們知道SQL可以在多平台之間使用。
使用原生SQL方式如下:
String sql="select {b.*} from book b"
SQLQuery squery=session.createSQLQuery(sql);
squery.addEntity("b",book.class);
List list=squery.list();
2:hibernate的關聯查詢
A:一對一關聯:
B:一對多,多對一關聯
C:多對多關聯
最後,要學hibernate,平時要多動手,慢慢積累經驗,成就感就會也大,這樣才能學而不厭。祝你學習進步。

Ⅹ Hibernate SQL查詢

如果你的hbm.xml配置文件配置恰當,可以用HQL:
Query query=....."from MessagePO mpo where mpo.clent=?";
ClentPO cpo=session.get(ClentPO.class,主鍵值);
query.setParameter(1,cpo);
query.list();