gpt4 book ai didi

java - 如何使用mybatis按参数分组

转载 作者:行者123 更新时间:2023-11-30 08:58:06 25 4
gpt4 key购买 nike

帮助...我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com