MyBatis DAO layer passing parameters to Mapping.xml

Source: Internet
Author: User

Summarize the several ways that I used the Mybatis,dao layer to pass parameters to the Mapping.xml file:

First: Pass a single argument

DAO Layer Code fragment:

[Java]View PlainCopy
    1. /**
    2. * Check xxxx details according to ArticleID.
    3. *
    4. * @param ArticleID
    5. * @return {@link cmsproductarticle}
    6. */
    7. Public cmsproductarticle Getcmsproductarticlebyarticleid (Long ArticleID);

Mapping Fragment:

[SQL]View PlainCopy
  1. <select id= "getcmsproductarticlebyarticleid" parametertype="Long" resulttype=" Xxx.xxxxx.xxx.xxxxx.xxx.CmsProductArticle ">
  2. SELECT
  3. *
  4. from
  5. TableA A, TableB b
  6. WHERE
  7. a.article_id = b.article_id
  8. and A.del_flag! = 2
  9. and b.article_id = #{articleid}
  10. </select>

When passing a single parameter, set parametertype directly to the parameter type (Long) you passed in, and get the parameter directly with "#{}", the parameter name must match the DAO layer parameter name.

Resulttype is the type returned by the results of the SQL query, and the DAO layer interface returns the entity class, so the resulttype here is the path to the entity class (holding down the CTRL key, the path is correct when the mouse clicks the path directly into the entity class)

Second type: Pass multiple parameters

1, marked with annotations

DAO Layer Code fragment:

[Java]View PlainCopy
    1. /**
    2. * Query whether CompanyID exists.
    3. *
    4. * @param CompanyID
    5. * @param imgid
    6. * @return int
    7. */
    8. public int Querycompanyidandimgidisexist (@Param ("CompanyID") Long CompanyID, @Param ("id")  Long Imgid);

Mapping Fragment:

[Java]View PlainCopy
    1. <select id="querycompanyidandimgidisexist" resulttype="Integer" >
    2. Select
    3. Count (1)
    4. From table_img img
    5. where img.company_id = #{companyid}
    6. and img.id = #{id}
    7. </select>

You do not need to write parametertype at this time, but note that the parameter names within "#{}" must match the names defined within the annotation @param ("") in the DAO layer.

2, direct transfer of parameters

DAO Layer Code fragment:

[Java]View PlainCopy
    1. /**
    2. * Query whether CompanyID exists.
    3. *
    4. * @param CompanyID
    5. * @param imgid
    6. * @return int
    7. */
    8. public int Querycompanyidandimgidisexist (long companyid, long imgid);

Mapping Fragment:

[Java]View PlainCopy
    1. <select id="querycompanyidandimgidisexist" resulttype="Integer" >
    2. Select
    3. Count (1)
    4. From table_img img
    5. where img.company_id = #{0}
    6. and img.id = #{1}
    7. </select>

#{0} and #{1} are the order of your arguments in DAO

3, passing parameters in map

Implement the Class code fragment:

[Java]View PlainCopy
    1. map<string,object> searchcondition = new hashmap<> ();
    2. Searchcondition.put ("CategoryId", categoryId);
    3. Searchcondition.put ("status", status);
    4. list<cmsproductarticle> cmsproductarticles = Cmsprodcutarticledao.getcmsproductarticles (searchCondition);

DAO Layer Code fragment:

[Java]View PlainCopy
    1. /**
    2. * Query product template set based on search criteria.
    3. *
    4. * @param searchcondition
    5. * @return list<cmsproductarticle>
    6. */
    7. Public list<cmsproductarticle> getcmsproductarticles (map<string, object> searchcondition);

Mapping Fragment:

[SQL]View PlainCopy
  1. <select id="getcmsproductarticles" parametertype="Java.util.Map" resulttype=" Xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle ">
  2. SELECT
  3. *
  4. from
  5. Table A, table B
  6. WHERE
  7. a.article_id = b.article_id
  8. and A.del_flag! = 2
  9. <if test="CategoryId! = null" >
  10. and a.category_id = #{categoryid}
  11. </if>
  12. <if test="Status! = NULL" >
  13. and a.status = #{status}
  14. </if>
  15. </select>

#{categoryid}, #{status} corresponds to your key in the map

The third kind: to pass the entity class

DAO Layer Code fragment:

[Java]View PlainCopy
    1. /**
    2. Update.
    3. *
    4. * @param cmsproductarticle
    5. * @return
    6. */
    7. Public void Updatecmsproductarticle (cmsproductarticle cmsproductarticle);

Mapping Fragment:

[SQL]View PlainCopy
  1. <update id="updatecmsproductarticlebase" parametertype=" Xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle ">
  2. UPDATE Table
  3. SET
  4. category_id = #{categoryid}, Supply_type = #{supplytype}, Pay_type = #{paytype}, Pay_value = #{payvalue}, status = #{statu S
  5. WHERE
  6. article_id = #{articleid}
  7. and Del_flag! = 2
  8. </update>

#{categoryid} and other properties in the corresponding entity class.

MyBatis DAO layer passing parameters to Mapping.xml

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.