帮忙... 我想使用mybatis3.2.3按多个参数进行分组,但它不起作用...请帮助我。我怎样才能得到我的答案。谢谢。
这是映射器。
<select id="selectByGroup" resultMap="chartMap">
select * from chart group by
<foreach collection="list" item="item" index="index" separator=",">
#{item}
</foreach>
</select>
Controller :
String dims[] = dimension.split(",");
List<String> paraList = new ArrayList<String>();
for(String s : dims){
if(!s.equals(""))
paraList.add(s);
}
List<Chart> charts = chartDao.selectByMap(paraList);
相关dao方法:
public List<Chart> selectByMap(Object obj) {
List<Chart> chartList = null;
System.out.println(obj);
chartList = sqlSession.selectList("draw.selectByGroup",obj);
return chartList;
}
最佳答案
尝试将 #{item} 替换为 ${item}。
来自官方文档(http://mybatis.github.io/mybatis-3/sqlmap-xml.html):
String Substitution
By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:
ORDER BY ${columnName} Here MyBatis won't modify or escape the string.
NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.
关于java - 如何使用mybatis按参数分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832190/