在模糊查询时,如果使用到了的条件包括"_",在查询时,需要使用到转义字符对"_"进行转义,具体详细看下列代码:
name=name.replaceAll("\\_", "\\\\\\\\_");
final String _name = name;
List<Integer> cuIdList = super.getHibernateTemplate().execute(new HibernateCallback<List<Integer>>() {
@SuppressWarnings("unchecked")
@Override
public List<Integer> doInHibernate(Session session) throws HibernateException, SQLException {
StringBuffer sql = new StringBuffer(200);
sql.append("select ")
.append("c.id")
.append(" from s c ")
.append("where c.control_unit_id in(:controlUnitId)")
.append(" and c.name like '%")
.append(_name)
.append("%' ESCAPE '\\\\'");
SQLQuery query = session.createSQLQuery(sql.toString());
query.setParameterList("controlUnitId", unitIdWithPrivilege);
return query.list();
}
});