hibernate模糊查詢

來源:互聯網
上載者:User

標籤:

hibernate模糊查詢-Restrictions.ilike & Expression.like

 Criteria criteria = session.createCriteria(TaDiagnoseSystem.class);
   //增加查詢條件
   if(StringUtils.isNotEmpty(systemName)){
//    criteria.createCriteria("taDiagnoseSystemSubs").add(Restrictions.ilike("systemName", systemName, MatchMode.ANYWHERE));
    criteria.createCriteria("taDiagnoseSystemSubs").add(Expression.like("systemName", "%"+systemName+"%"));
   }

 

 

Hibernate3.0中實現模糊查詢【轉總結】今天的項目中在用Hibernate3.0進行模糊查詢的時候,出現中文亂碼,在網上查了好久資料,終於解決了,現在把整理後的貼出來。
Hibernate中實現模糊查詢,可有以下三種方式: 

第一種方式:QBC查詢 
String name = "", info = ""; 
if (sub != null && sub.getSubname() != null) { 
name = sub.getSubname(); 
} 
if (sub != null && sub.getSubinfo() != null) { 
info = sub.getSubinfo(); 
} 
Criteria cr = session.createCriteria(Subject.class); 
cr.add(Expression.like("subname","%"+name+"%")); 
cr.add(Expression.like("subinfo","%"+info+"%")); 

第二種方式:HQL查詢語句 
String hql = "from Subject as s where s.subname like :name and s.subinfo like :info"; 
// 調用session的獲得資料列表方法,傳遞HQL查詢語句 
String  subnamevalue="要查詢的中文值";
String   sql="";
sql   = "from Pfapp  where name like :subname "; 
Query query = session.createQuery(sql); 
query.setString("subname","%"+subnamevalue+"%");
System.out.println("*********"+sql); 

註:Subject為資料庫表subject映射的類;它有相應的屬性subname、subinfo及相應的get和set方法;sub為Subject的一個執行個體化對象。
另外說明的是:如果你不寫對象名稱,直接寫欄位,那麼hibernate就按照資料庫欄位進行尋找 
如果你寫   (對象名.對象屬性),那麼hibernate就按照ormapping中的映射欄位進行尋找

hibernate模糊查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.