Today, it took two hours to get a very simple SQL statement. When I know the answer, I decide to write a blog post.
Requirements: According to a series of IDs query the corresponding name. That is, according to the collection of Creative ID query its corresponding creative name.
Come and see how I wrote it:
<!--query the creative name collection based on IDs--><select id= "Querycreativenamebyids" parametertype= "map" resulttype= "java.lang.String ">select creative_namefrom pub_creative pcwhere 1=1<if test=" IDs! = null and ids! = "" >and pc.creative_id in (# {IDs}) </if></select>
Seeing this SQL is not a problem at all. I think so, too. Take a look at the Java code:
/** * Search Creative name According to IDs */@Overridepublic string Querycreativenamebyids (String IDs) throws Exception {map<string, Object > queryparams = new hashmap<string, object> () queryparams.put ("IDs", IDS); list<object> creativenames = this.objectDao.getObjects (PubTargetCreativeService.class.getName () + ". Querycreativenamebyids ", queryparams); return creativenames.tostring ();}
This code is very clear, is to get back to return the Creativenames collection. The pass-through parameter is a map collection. But the problem came, and I returned to the Creativenames collection just to have no content. Snap to console code, run in MySQL, have data ah.
The following is the captured console code:
[DEBUG] 2015-08-27 11:30:09:==> preparing:select creative_name from pub_creative pc WHERE 1=1 and pc.creative_id in (?) [DEBUG] 2015-08-27 11:30:09:==> Parameters: [15131, 15123, 15124, 15040] (String)
Pondering for a long time, did not find the reason. Find a colleague to help you look at it. The original problem is the way the parameters are passed. I passed the collection of the past should be an array, I converted it to a string type directly passed to the background. The surface is not a problem, actually mybatis it as a parameter processing ( I guess). So I never get the right result.
MyBatis the Foreach Loop problem