Hibernate 更新數據庫 參數綁定總結:數據庫
一.query.setParameter(屬性名,真實值,類型);對象
String hql="update User u set u.userName=:userName where u.userId=:userId";get
Query query = getSession.createQuery(hql); io
query.setParameter("userName", userName(對應:後面的值), Hibernate.STRING);date
query.setParameter("userId", userId(對應:後面的值), Hibernate.INTEGER);
總結
query.executeUpdate();數據
二.query.setXXXX(屬性值,真實值);query
String hql="update User u set u.userName=:userName where u.userId=:userId";
new
query.setString("userName",userName);參數
query.setInteger("userId",userId);
query.executeUpdate();
三.query.setString(問號位置,真實值);
String hql="update User u set u.userName=? where u.userId=?";
Query query = getSession.createQuery(hql);
query.setString(0,userName);
query.setInteger(1,userId);
query.executeUpdate();
四.query.setProperties(對象);
String hql="update User u set u.userName=:userName where u.userId=:userId";
Query query = getSession.createQuery(hql);
User user = new User();
user.serUserName("張三");
user.setUserId(2);
query.setProperties(user);
query.executeUpdate();