传统JDBC的参数绑定:
传统JDBC的参数绑定:
PrepareStatement pre=connection prepare(“select * from User where user name=?”);
pre.etString(0,”luoye”);
ResultSet rs=pre.executeQuery();
按参数名称绑定:
Query query=session.createQuery("from User u where u.username=:username and u.age=:userage ");
query.setString("username",name);
query.setInteger("userage",age);
按参数位置邦定:
Query query=session.createQuery("from User user where user.name=? and user.age =? ");
query setString(0,name);
query setInteger(1,age);
setParameter()方法:
String name="张三";
Integer age =10;
String hql="from User user where user.name=:customername and user.age=:userage";
Query query=session.createQuery(hql);
query.setParameter("customername",name);
query.setParameter("userage",age);
setProperties()方法:
Customer customer=new Customer();
customer.setName("customerl");
customer.setAge(80);
Query query=session.createQuery("from Customer c where c.name=:name and c.age=:age ");
query.setProperties(customer);//命名参数名称必须要与实体对象相应的属性同名
setEntity()方法: Customer customer=(Customer)session.get(Customer.class,"1");
Query query=session.createQuery("from Order order where order.customer=:customer ");
query setEntity("customer",customer);
List.list=query.list();
// 上面的代码会生成类似如下的SQL语句:Select * from order where customer.id=’1’;