hibernate單列的多值查詢

好比你的表主鍵是id,你要刪除id 是 34,56,99 這樣的。。數組

uid是拼好的 好比 '34','56','99' ,之前我會這樣寫ui

String queryString = "update Mail set dm=1 where uid in("+uid+")";
            Query queryObject = sess.createSQLQuery(queryString);
            queryObject.executeUpdate();
            tran.commit();

但其實能夠這樣使用setParameterList,uid 作成一個數組就能夠了,不用拼的那麼麻煩。spa

String queryString = "update Mail set dm=1 where uid in(:ids)";
            Query queryObject = sess.createSQLQuery(queryString);
            queryObject.setParameterList("ids", uid);
            queryObject.executeUpdate();
            tran.commit();


另一個須要注意,使用這個時,uid數組的長度須大於0,不然會報異常 net.sf.hibernate.exception.SQLGrammarExceptionhibernate

參考:code

http://chedsk.blog.163.com/blog/static/23581288201132353226700/blog

http://spiritfrog.iteye.com/blog/197519get

相關文章
相關標籤/搜索