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
- /**
- * Check xxxx details according to ArticleID.
- *
- * @param ArticleID
- * @return {@link cmsproductarticle}
- */
- Public cmsproductarticle Getcmsproductarticlebyarticleid (Long ArticleID);
Mapping Fragment:
[SQL]View PlainCopy
- <select id= "getcmsproductarticlebyarticleid" parametertype="Long" resulttype=" Xxx.xxxxx.xxx.xxxxx.xxx.CmsProductArticle ">
- SELECT
- *
- from
- TableA A, TableB b
- WHERE
- a.article_id = b.article_id
- and A.del_flag! = 2
- and b.article_id = #{articleid}
- </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
- /**
- * Query whether CompanyID exists.
- *
- * @param CompanyID
- * @param imgid
- * @return int
- */
- public int Querycompanyidandimgidisexist (@Param ("CompanyID") Long CompanyID, @Param ("id") Long Imgid);
Mapping Fragment:
[Java]View PlainCopy
- <select id="querycompanyidandimgidisexist" resulttype="Integer" >
- Select
- Count (1)
- From table_img img
- where img.company_id = #{companyid}
- and img.id = #{id}
- </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
- /**
- * Query whether CompanyID exists.
- *
- * @param CompanyID
- * @param imgid
- * @return int
- */
- public int Querycompanyidandimgidisexist (long companyid, long imgid);
Mapping Fragment:
[Java]View PlainCopy
- <select id="querycompanyidandimgidisexist" resulttype="Integer" >
- Select
- Count (1)
- From table_img img
- where img.company_id = #{0}
- and img.id = #{1}
- </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
- map<string,object> searchcondition = new hashmap<> ();
- Searchcondition.put ("CategoryId", categoryId);
- Searchcondition.put ("status", status);
- list<cmsproductarticle> cmsproductarticles = Cmsprodcutarticledao.getcmsproductarticles (searchCondition);
DAO Layer Code fragment:
[Java]View PlainCopy
- /**
- * Query product template set based on search criteria.
- *
- * @param searchcondition
- * @return list<cmsproductarticle>
- */
- Public list<cmsproductarticle> getcmsproductarticles (map<string, object> searchcondition);
Mapping Fragment:
[SQL]View PlainCopy
- <select id="getcmsproductarticles" parametertype="Java.util.Map" resulttype=" Xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle ">
- SELECT
- *
- from
- Table A, table B
- WHERE
- a.article_id = b.article_id
- and A.del_flag! = 2
- <if test="CategoryId! = null" >
- and a.category_id = #{categoryid}
- </if>
- <if test="Status! = NULL" >
- and a.status = #{status}
- </if>
- </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
- /**
- Update.
- *
- * @param cmsproductarticle
- * @return
- */
- Public void Updatecmsproductarticle (cmsproductarticle cmsproductarticle);
Mapping Fragment:
[SQL]View PlainCopy
- <update id="updatecmsproductarticlebase" parametertype=" Xxx.xxxxxxx.xxx.xxxxx.xxxxxxx.CmsProductArticle ">
- UPDATE Table
- SET
- category_id = #{categoryid}, Supply_type = #{supplytype}, Pay_type = #{paytype}, Pay_value = #{payvalue}, status = #{statu S
- WHERE
- article_id = #{articleid}
- and Del_flag! = 2
- </update>
#{categoryid} and other properties in the corresponding entity class.
MyBatis DAO layer passing parameters to Mapping.xml