我使用Spring mvc,Hibernate和Mysql,在我的DAOImplement中我将sql文件称为outsite。
这是我的DAO:
public List<RenterDto> searchByNamePersonalId(String name, String personalId) {
Session session = sessionFactory.openSession();
String sqlString = GetSqlUtils.getSqlQueryString(RenterDaoImpl.class, SQL_DIR + SEARCH_BY_NAME_PERSON);
List<RenterDto> list = new ArrayList<RenterDto>(0);
try {
Query query = session.createSQLQuery(sqlString);
query.setParameter(0, name);
query.setParameter(1, personalId);
list = query.setResultTransformer(new AliasToBeanResultTransformer(RenterDto.class)).list();
} catch (HibernateException e) {
logger.error("error at RenterDaoImpl.searchByNameAddress: " + e.getMessage());
} finally {
session.close();
}
return list;
}
我的SQL调用(SEARCH_BY_NAME_PERSON),用于搜索name和personalId:
SELECT
R.ID,
R.NAME,
R.ACCOUNT_ID,
R.OTP_CODE,
R.OS_TYPE,
R.MANCHINID,
R.TYPE,
R.PERSONAL_ID,
R.PHONENUMBER,
R.EMAIL,
R.ADDRESS,
R.DISTRICT_ID,
D.NAME AS DISTRICT_NAME,
R.CITY_ID,
C.NAME AS CITY_NAME
FROM
RENTER R
INNER JOIN
DISTRICT D
ON
R.DISTRICT_ID = D.ID
INNER JOIN
CITY C
ON
R.CITY_ID = C.ID
WHERE
LOWER(R.NAME)
LIKE
('%'||'?')
OR
R.PERSONAL_ID
LIKE
'%'||'%'
当我使用关键字搜索时:&#39; name&#39;或者&#39; personalId&#39;,我收到了以下错误结果:
SqlExceptionHelper - SQL Error: 0, SQLState: S1009
SqlExceptionHelper - Parameter index out of range (1 > number of parameters, which is 0).
RenterDaoImpl - error at RenterDaoImpl.searchByNameAddress: could not execute query
如何解决这个问题?非常感谢你!
答案 0 :(得分:1)
你有这个表达:
LIKE ('%'||'?')
我猜你希望'?'
成为一个参数。但它只是一个低字符串,只有一个问号。
相反:
LIKE CONCAT('%', ?)
通常,在MySQL中,您希望使用CONCAT()
进行字符串连接。