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

标签 java mybatis

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

相关文章:

java - MyBatis 中的映射组合

mysql存储过程从基于数据库的队列中弹出

java - 日期比较问题

java - 构造函数 vs 方法 vs 工厂

java - 创建 android studio 库后,我无法添加许多 comman 依赖项

mysql - 配置 MyBatis 使用轻量级 MySQL ping/* ping */

java - 两个 jtextfield 之间的加法

javax.sound.sampled 多线程安全

java - MyBatis,如何获取插入的自动生成 key ? [MySql]

database - 在 MyBatis 中,如何为 <sql> 元素设置范围以供重用