Dao层传递参数到mapping.xml文件的几种方式:(Mybatis传值总结)

第一种:传递单个参数

Dao层Code片段:

  1. /**
  2. * 根据articleId查询XXXX详情.
  3. *
  4. * @param articleId
  5. * @return {@link CmsProductArticle}
  6. */
  7. public CmsProductArticle getCmsProductArticleByArticleId(Long articleId);

Mapping片段:

  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>

传递单个参数时直接将parameterType设置成你传入的参数类型(Long),直接用“#{}”获得参数,参数名必须与Dao层参数名一致。

resultType是SQL查询结果返回的类型,Dao层接口返回是实体类,所以这里的resultType是实体类的路径(按住ctrl键,鼠标点击路径时可以直接进入实体类时路径正确)

第二种:传递多个参数

1,以注解标记

Dao层Code片段:

  1. /**
  2. * 查询companyId是否存在.
  3. *
  4. * @param companyId
  5. * @param imgId
  6. * @return int
  7. */
  8. public int queryCompanyIdAndImgIdIsExist(@Param("companyid") Long companyId, @Param("id") Long imgId);

Mapping片段:

  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>

此时不需要写parameterType,但是注意“#{}”内的参数名必须跟你在Dao层中注解@Param("")内定义的名称一致。

2,直接传递参数

Dao层Code片段:

  1. /**
  2. * 查询companyId是否存在.
  3. *
  4. * @param companyId
  5. * @param imgId
  6. * @return int
  7. */
  8. public int queryCompanyIdAndImgIdIsExist( Long companyId,  Long imgId);

Mapping片段:

  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}与#{1}是你在Dao里的参数顺序

3,以Map传递参数

实现类Code片段:

  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层Code片段:

  1. /**
  2. * 根据搜索条件查询产品模板集.
  3. *
  4. * @param searchCondition
  5. * @return List<CmsProductArticle>
  6. */
  7. public List<CmsProductArticle> getCmsProductArticles(Map<String, Object> searchCondition);

Mapping片段:

  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}对应你在Map里面的Key

第三种:以实体类传递

Dao层Code片段:

  1. /**
  2. * 更新.
  3. *
  4. * @param cmsProductArticle
  5. * @return
  6. */
  7. public void updateCmsProductArticle(CmsProductArticle cmsProductArticle);

Mapping片段:

  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 = #{status}
  5. WHERE
  6. article_id = #{articleId}
  7. and del_flag != 2
  8. </update>

#{categoryId}等对应实体类中属性。

MyBatis DAO层传递参数到mapping.xml 几种方式的更多相关文章

  1. mybatis传递参数到mapping.xml

    第一种方案 ,通过序号传递 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id ...

  2. SpringMVC 页面传递参数到controller的五种方式

    一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public  String login (String username,String password)   : 解 ...

  3. MyBatis Dao层的编写

    传统的dao层编写 以前编写dao层,先新建一个包com.chy.dao,再写接口StudentDao: public interface StudentDao { public void inser ...

  4. MyBatis dao层 方法传参

    MyBatis dao层 方法传参有三种方法. 1. 以下标的方法获取参数. <update id="insertSuccessKilled">       INSER ...

  5. js setTimeout 传递带参数的函数的2种方式

      js setTimeout 传递带参数的函数的2种方式 Created by Marydon on 2018年9月14日 1.准备工作 function sayYourName(param) { ...

  6. mybatis3.1-[topic-18-20]-_映射文件_参数处理_单个参数&多个参数&命名参数 _POJO&Map&TO 三种方式及举例

    笔记要点出错分析与总结 /**MyBatis_映射文件_参数处理_单个参数&多个参数&命名参数 * _POJO&Map&TO 三种方式及举例 _ * 单个参数 : #{ ...

  7. Mybatis Dao层注解及XML组合Dao的开发方式

    mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法.显然 ,后者更加简单. 实体类Student   package com.zhao. ...

  8. [MyBatis]DAO层只写接口,不用写实现类

    团队开发一个项目,由老大架了一个框架,遇到了DAO层不用写接口了,我也是用了2次才记住这个事的,因为自己一直都是习惯于写DAO层的实现类,所以,习惯性的还是写了个实现类.于是遇到错误了. 找不到那个方 ...

  9. 基于dbunit进行mybatis DAO层Excel单元测试

    DAO层测试难点 可重复性,每次运行单元测试,得到的数据是重复的 独立性,测试数据与实际数据相互独立 数据库中脏数据预处理 不能给数据库中数据带来变化 DAO层测试方法 使用内存数据库,如H2.优点: ...

随机推荐

  1. 关于Cocos Studio制作游戏资源

    没想到,Cocos Studio居然是做游戏资源的,而且可以做骨骼动画,虽然我还不会做,只能自己一个人慢慢研究了.学长以前说,Coocs Studio只是用来打包项目成Apk的,没有什么卵用,刚开始我 ...

  2. selenium测试(Java)-- 一组元素操作(十一)

    利用下面的例子来编写测试脚本 页面代码: <!DOCTYPE html> <html> <head> <meta http-equiv="conte ...

  3. x264参数

    参数及结构 typedef struct{    int     i_csp;       //色彩空间参数 ,X264只支持I420    int     i_stride[4]; //对应于各个色 ...

  4. jQuery异步请求模拟IE登录网站

    具体请求的登录验证页面后台逻辑处理,这里我们忽略,不在我们的学习范围内:关键的是使用jQuery异步请求方法,如下例子: <%@ Page Language="C#" Aut ...

  5. 使用Visual Studio将C#生成DLL文件的方法

    1.命令方式 打开Visual Studio安装目录下的开发人员命令提示 译 File.cs 以产生 File.exe csc File.cs 编译 File.cs 以产生 File.dll csc ...

  6. mysql数据库,查看数据存放目录datadir

    需求描述: 在使用数据库,或者刚接手一个数据库时,可以查看该数据库的数据文件存放在什么位置. 操作过程: 1.通过查看datadir系统变量来查看数据目录 [mysql@redhat6 mysql-b ...

  7. 非常不错的前端框架Kendo-ui

    近期公司在做重构,准备换前端框架由Extjs换kendo-ui,问什么要换框架呢?主要有以下几个原因: Extjs太重,偏向后端语言,前端写起来费劲 Extjs执行太慢(这是主要原因),因为Extjs ...

  8. oracle如何将am,pm时间字符串改为时间格式

    问题: 解决办法: 1.param["OPT_DATE"] = DateTime.Parse(dt.Rows[0]["CREATED_ON"].ToString ...

  9. img标签-srcset属性

    今天看前辈的代码时,发现img标签有个陌生的srcset属性,如下: 1 <img class="Avatar" src="https://pic3.zhimg.c ...

  10. 计算从ios照片库中选取的图片文件大小

    本文转载至:http://blog.csdn.net/longzs/article/details/8373586 从 iphone 的 照片库中选取的图片,由于 系统不能返回其文件的具体路径,所以这 ...