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;
    	}
    

      

     
posted @ 2019-04-19 10:42  剧毒术士  阅读(2000)  评论(0编辑  收藏  举报