hibernate手动写sql语句

不带参数:

String sql="select * from t_department where status=1";
return (List) this.getSession().createSQLQuery(sql).addEntity(Department.class).list();


带参数(String id,String name):

String sql="INSERT INTO t_department (id,name,status) VALUES (?,?,1)";
Query query= this.getSession().createSQLQuery(sql);  
query.setString(0, id);  
query.setString(1, name);  
query.executeUpdate();

或者(String id):

String sql="select id from t_department where id=:id";
Query query= this.getSession().createSQLQuery(sql).setString("id", id);
return (String) query.uniqueResult();



你可能感兴趣的:(hibernate手动写sql语句)