当前位置 : 主页 > 手机开发 > ROM >

HQL实现模糊查询

来源:互联网 收集:自由互联 发布时间:2021-06-10
hibernate 实现模糊查询两种传参方式,其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别 public ListUser getUsers(String id){ List list=new ArrayListUser(); String hql="from User as user
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;
    	}
    
     
网友评论