@Select({"<script>",
"select t.resource_point_id",
"from delivery_point_task t",
"<where>",
"t.delivery_date = #{deliveryDate ,jdbcType=DATE} ",
"and t.delivery_target_status = 'YES' ",
"</where>",
"GROUP BY t.resource_point_id ",
"<choose>",
"<when test=\"deliveryTaskId != null and deliveryTaskId !=''\">",
"ORDER BY if(t.delivery_task_id = '${deliveryTaskId}',0,1)",
"</when>",
"<otherwise>",
"ORDER BY if(t.resource_point_id = '${resourcePointId}',0,1)",
"</otherwise>",
"</choose>",
"</script>"
})
会报错:Error querying database. Cause: java.lang.IllegalArgumentException: Unpermitted character (='')
打印的SQL复制出来,在SQL客户端上执行没问题。后来发现mybatis order by 后面的语句中不能有单引号,否则会报错。
解决方法,换了个思路:
@Select({"<script>",
"select t.resource_point_id",
"from delivery_point_task t",
"<where>",
"<choose>",
"<when test=\"deliveryTaskId != null and deliveryTaskId !=''\">",
"t.delivery_task_id = #{deliveryTaskId}",
"</when>",
"<otherwise>",
"t.resource_point_id = #{resourcePointId}",
"</otherwise>",
"</choose>",
"and t.delivery_target_status = 'YES' ",
"and t.delivery_date = #{deliveryDate ,jdbcType=DATE}",
"</where>",
"group by t.resource_point_id",
"UNION",
"select t.resource_point_id",
"from delivery_point_task t",
"<where>",
"t.delivery_date = #{deliveryDate ,jdbcType=DATE} ",
"and t.delivery_target_status = 'YES' ",
"</where>",
"group by t.resource_point_id",
"</script>"
})