StringBuffer sql = new StringBuffer("select * from users where 1=1 "); String keyWord="aaa' or '1=1"; if(!keyWord.isEmpty()){ sql.append(" and username like '%" + StringEscapeUtils.escapeSql(keyWord) + "%'"); } System.out.println(sql);
输出结果 select * from users where 1=1 and username like '%aaa'' or ''1=1%'