使用java在sql中的PreparedStatementCallback

时间:2014-02-14 20:05:11

标签: java sql list map

List<Integer> roles = jdbcTemplate.queryForList("select role_id from user_roles where user_id = ?" , new Object[]{id});

    String roleIds= "";

    for (int i= 0; i < roles.size(); i++)
    {

           }            if (roles.get(i) != null) {
            if(i!=0){
                roleIds += "," + m.get(i) ;
            }else{
                roleIds += m.toString() ;
            }
        }
    }

    String queryString= "select u.id,u.user_id,count(distinct pf.id),IfNULL(upm.grievance_autoassign_enable,true) "+
            " from users u "+  
            " inner join user_roles ur on ur.user_id=u.id " +
            " left outer join user_preference_management upm on u.id = upm.id "+
            " inner  join management_hierarchy mh on mh.child_id = u.id or mh.role_id =ur.role_id "+  
            " left outer join ( jbpm4_task  jt "+ 
            " inner join process_flow pf on pf.pid = jt.execution_id_ and pf.grievance_mgmt_id is not null)  on jt.assignee_ = u.user_id "+
            " where mh.parent_id = ? "+ (roleIds != null ? " or mh.role_id in (" + roleIds + ")" : "") +
            " group by u.id,u.user_id  order by count(pf.id) ";

    return executeDBSummaryWithAutoAssign(queryString,new Object[] {id});
    }

我的错误日志:

  

PreparedStatementCallback;   来自用户的错误SQL语法[select u.id,u.user_id,count(distinct pf.id),IfNULL(upm.grievance_autoassign_enable,true)   内部联接user_roles ur在ur.user_id = u.id左外部联接   user.preference_management upm on u.id = upm.id inner join   m_.child_id = u.id或mh.role_id上​​的management_hierarchy mh   = ur.role_id左外连接(jfpm4_task jt内连接process_flow pf on pf.pid = jt.execution_id_和pf.grievance_mgmt_id不为空)   在jt.assignee_ = u.user_id,其中mh.parent_id =?或者mh.role_id in   ([{role_id = 2},{role_id = 4},{role_id = 1},{role_id = 8},{role_id = 38},   {role_id = 22},{role_id = 69}],[{role_id = 2},{role_id = 4},{role_id = 1},   {role_id = 8},{role_id = 38},{role_id = 22},{role_id = 69}],[{role_id = 2},   {role_id = 4},{role_id = 1},{role_id = 8},{role_id = 38},{role_id = 22},   {role_id = 69}],[{role_id = 2},{role_id = 4},{role_id = 1},{role_id = 8},   {role_id = 38},{role_id = 22},{role_id = 69}],[{role_id = 2},{role_id = 4},   {role_id = 1},{role_id = 8},{role_id = 38},{role_id = 22},   {role_id = 69}],[{role_id = 2},{role_id = 4},{role_id = 1},{role_id = 8},   {role_id = 38},{role_id = 22},{role_id = 69}],[{role_id = 2},{role_id = 4},   {role_id = 1},{role_id = 8},{role_id = 38},{role_id = 22},{role_id = 69}])   u.id分组,u.user_id按count(pf.id)顺序排序];       嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,在'[{role_id = 2}附近使用正确的语法,   {role_id = 4},{role_id = 1},{role_id = 8},{role_id = 38},{role_id = 22},'at   第1行

1 个答案:

答案 0 :(得分:1)

逻辑中存在输出地图值列表的错误。您正在输出整个地图,而不仅仅是您想要的一个值。

更改此行:

roleIds += m.toString();

要:

roleIds += m.get(i);