方法一:
方法二:
方法三:
注意其中中文的编码设置
List questionList = (List)this.getHibernateTemplate().find(
"from question where name='" + name + "' and password='" + password +"'"
);
方法二:
List questionList = (List)this.getHibernateTemplate().find(
"from Question q where q.name=? and q.password=?",new Object[]{name, password}
);
方法三:
注意其中中文的编码设置
List questionList = (List) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) {
String hql = "from Question q where q.questionBank.id="
+ question.getQuestionBank().getId()
+ " and q.name like ? and q.isSingleChoice=? and q.isMultipleChoice= ?"
+ " or q.name like ? order by q.id desc";
//是否单选
q.setParameter(1, question.getIsSingleChoice());
//是否多选
q.setParameter(2, question.getIsMultipleChoice());
//关键字匹配
String s = "";
try {
s = new String(keys.getBytes("iso-8859-1"), "UTF-8");
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
q.setParameter(i, "%" + s + "%");
}
List result = q.list();
return result;
}
}