HQL實現模糊查詢


hibernate 實現模糊查詢兩種傳參方式,其實各個方法的實質都是一樣的,只不過傳遞參數的方法稍微有點區別 

  1.   

        public List<User> getUsers(String id){
            List list=new ArrayList<User>();
            String hql="from User as user where user.id like '%"+id+"%'";
            factory=DBHelper.getSessionFactory();
            Session session=factory.openSession();
            Transaction transaction=session.beginTransaction();
            Query query=session.createQuery(hql);
            list=query.list();
            transaction.commit();
            session.close();
            return list;
        }
    

      

  2.  

    public List<User> getUsers(String id){
    		List list=new ArrayList<User>();
    		String hql="from User as user where user.id like :id";
    		factory=DBHelper.getSessionFactory();
    		Session session=factory.openSession();
    		Transaction transaction=session.beginTransaction();
    		Query query=session.createQuery(hql);
    		 query.setString("id", "%"+id+"%");    
    		list=query.list();
    		transaction.commit();
    		session.close();
    		return list;
    	}
    

      

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM