MyBatis the Foreach Loop problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.