1.实体查询
String sqla = "from TEST where id=? order by sortnum desc";
List list = sess.createQuery(sql).setString(0, id).list();
Test test= (Test)list.get(0);
List list= sess.createSQLQuery("select * from TEST where id =?").addEntity(TEST.class).setString(0, id).list();
Test test= (Test)list.get(0);
2.通用查询,删除,新增,修改
List<HashMap<String,Object>> list= (List<HashMap<String,Object>>)sess.createSQLQuery("select * from test where id=?").setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).setString(0,id).list();
List<Object[]> list= (List<HashMap<String,Object>>)sess.createSQLQuery("select id,sortnum,remark from test where id=:id").setString("id",id).list();
sess.createSQLQuery("delete from test where id=?").setString(0, id).executeUpdate();
sess.createSQLQuery("insert into test(id,sortnum,remark) values (?,?,?)").setString(0, id).setString(1, sortnum).setString(2, remark).executeUpdate();
sess.createSQLQuery("update test set remark=? where id=?").setString(0, remark).setString(1, id).executeUpdate();
注意:有变量参数的sql语句,应该使用占位符(?)或者绑定变量(:id) 的形式执行,可防止数据库并发过高时,硬解析语句过多导致查询卡慢。