当前位置: 首页 > news >正文

MyBatis DAO层传递参数到mapping.xml

文章来源:http://blog.csdn.net/u011663693/article/details/53334513

MyBatis DAO层传递参数到mapping.xml

总结我所用到的MyBatis,Dao层传递参数到mapping.xml文件的几种方式:

第一种:传递单个参数

Dao层Code片段:

/** 
 * 根据articleId查询XXXX详情. 
 *  
 * @param articleId 
 * @return {@link CmsProductArticle} 
 */  
public CmsProductArticle getCmsProductArticleByArticleId(Long articleId);  

Mapping片段:

<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>  

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

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

 

第二种:传递多个参数

1,以注解标记

Dao层Code片段:

/** 
 * 查询companyId是否存在. 
 *  
 * @param companyId 
 * @param imgId 
 * @return int 
 */  
public int queryCompanyIdAndImgIdIsExist(@Param("companyid") Long companyId, @Param("id") Long imgId);  

Mapping片段:

<select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">  
    select   
        count(1)  
     from table_img img  
    where img.company_id = #{companyid}  
      and img.id = #{id}  
</select>  

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

 

 

2,直接传递参数

Dao层Code片段:

/** 
 * 查询companyId是否存在. 
 *  
 * @param companyId 
 * @param imgId 
 * @return int 
 */  
public int queryCompanyIdAndImgIdIsExist( Long companyId,  Long imgId);  

Mapping片段:

<select id="queryCompanyIdAndImgIdIsExist" resultType="Integer">  
    select   
        count(1)  
     from table_img img  
    where img.company_id = #{0}  
      and img.id = #{1}  
</select>  

#{0}与#{1}是你在Dao里的参数顺序

 

 

3,以Map传递参数

实现类Code片段:

Map<String,Object> searchCondition = new HashMap<>();  
searchCondition.put("categoryId", categoryId);  
searchCondition.put("status", status);  
List<CmsProductArticle> cmsProductArticles = cmsProdcutArticleDao.getCmsProductArticles(searchCondition);  

Dao层Code片段:

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

Mapping片段:

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

 

 

第三种:以实体类传递

Dao层Code片段:

/** 
 * 更新. 
 *  
 * @param cmsProductArticle 
 * @return  
 */  
public void updateCmsProductArticle(CmsProductArticle cmsProductArticle);  

Mapping片段:

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

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

转载于:https://www.cnblogs.com/mihun-666/p/8203327.html

相关文章:

  • 微内核与面向组件
  • 运维学python之爬虫中级篇(二)线程、协程
  • 资料推荐--Google Java编码规范
  • Python中的string模块的学习
  • bzoj千题计划205:bzoj3529: [Sdoi2014]数表
  • 关于多线程的参数问题
  • sudo、磁盘结构、echo,awk,python计算、RAID0和1的区别
  • jsp页面按时间排序
  • 18载艰苦创业,曾动念房地产转型,讯飞的江湖夜雨和桃李春风
  • UML--------------------类图
  • 如何使用MACS进行peak calling
  • 使用mysqladmin命令修改MySQL密码与忘记密码
  • text-decoration与color属性
  • reactive streams与观察者模式
  • 面试技术题笔记
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【347天】每日项目总结系列085(2018.01.18)
  • Brief introduction of how to 'Call, Apply and Bind'
  • chrome扩展demo1-小时钟
  • JavaScript-Array类型
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Redux系列x:源码分析
  • 分类模型——Logistics Regression
  • 理清楚Vue的结构
  • 判断客户端类型,Android,iOS,PC
  • 如何解决微信端直接跳WAP端
  • 使用 QuickBI 搭建酷炫可视化分析
  • 协程
  • 云大使推广中的常见热门问题
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • ​​​​​​​​​​​​​​Γ函数
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • !!Dom4j 学习笔记
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (42)STM32——LCD显示屏实验笔记
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (蓝桥杯每日一题)love
  • (利用IDEA+Maven)定制属于自己的jar包
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)使用VMware vSphere标准交换机设置网络连接
  • .bat批处理(六):替换字符串中匹配的子串
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core 连接数据库,通过数据库生成Modell
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .Net Web项目创建比较不错的参考文章
  • .NET 常见的偏门问题
  • .NET性能优化(文摘)
  • :中兴通讯为何成功
  • @KafkaListener注解详解(一)| 常用参数详解
  • @Valid和@NotNull字段校验使用