MyBatis DAO层传递参数到mapping.xml 几种方式
Dao层传递参数到mapping.xml文件的几种方式:(Mybatis传值总结)
第一种:传递单个参数
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}等对应实体类中属性。
MyBatis DAO层传递参数到mapping.xml 几种方式的更多相关文章
- mybatis传递参数到mapping.xml
第一种方案 ,通过序号传递 DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- MyBatis Dao层的编写
传统的dao层编写 以前编写dao层,先新建一个包com.chy.dao,再写接口StudentDao: public interface StudentDao { public void inser ...
- MyBatis dao层 方法传参
MyBatis dao层 方法传参有三种方法. 1. 以下标的方法获取参数. <update id="insertSuccessKilled"> INSER ...
- js setTimeout 传递带参数的函数的2种方式
js setTimeout 传递带参数的函数的2种方式 Created by Marydon on 2018年9月14日 1.准备工作 function sayYourName(param) { ...
- mybatis3.1-[topic-18-20]-_映射文件_参数处理_单个参数&多个参数&命名参数 _POJO&Map&TO 三种方式及举例
笔记要点出错分析与总结 /**MyBatis_映射文件_参数处理_单个参数&多个参数&命名参数 * _POJO&Map&TO 三种方式及举例 _ * 单个参数 : #{ ...
- Mybatis Dao层注解及XML组合Dao的开发方式
mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法.显然 ,后者更加简单. 实体类Student package com.zhao. ...
- [MyBatis]DAO层只写接口,不用写实现类
团队开发一个项目,由老大架了一个框架,遇到了DAO层不用写接口了,我也是用了2次才记住这个事的,因为自己一直都是习惯于写DAO层的实现类,所以,习惯性的还是写了个实现类.于是遇到错误了. 找不到那个方 ...
- 基于dbunit进行mybatis DAO层Excel单元测试
DAO层测试难点 可重复性,每次运行单元测试,得到的数据是重复的 独立性,测试数据与实际数据相互独立 数据库中脏数据预处理 不能给数据库中数据带来变化 DAO层测试方法 使用内存数据库,如H2.优点: ...
随机推荐
- 关于Cocos2d-x中自己定义的类的名字和Cocos2d-x引擎库中的类的名字重复的解决方法
方法一: 修改自己定义的类的名字,VS2013中可以用Ctrl+H来替换某个特定的单词,Ctrl+F是用来查询某个单词所在的位置或者有没有存在. 方法二: 1.给自己定义的类的.h和.cpp文件的整体 ...
- python print 不换行
#!/usr/bin/python # -*- coding: UTF- -*- ,): ,i+): print "%d * %d = %2d\t" % (j, i, i*j), ...
- 第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型
第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型 Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value)name ...
- QHeaderView的点击和双击事件
QTablewidget的horizontalHeader() 和 verticalHeader() 得到的表头:QHeaderView 点击事件的触发函数: sectionClicked(int) ...
- IOS URL scheme
常用URL scheme查询 http://handleopenurl.com/scheme QQ: mqq://新浪微博: weibo:// (sinaweibo://)腾讯微博: tencentw ...
- c++ word类型
word就是16位的数据 随着机器的发展,C++语言本身并没有规定short的位数,不一定是十六位的(随着计算机的发展,可能改变).但word将永远是16位的--机器发展后只需要修改,typedef ...
- oracle中LAG()和LEAD()以及over (PARTITION BY)
LAG()和LEAD()统计函数可以在一次查询中取出同一字段的前N行的数据和后N行的值.这种操作可以使用对相同表的表连接来实现,不过使用LAG和 LEAD有更高的效率.以下整理的LAG()和LEAD( ...
- Loadrunner进行md5加密方法
本文主要介绍使用Loadrunner进行字符串md5加密的方法. 使用Loadrunner进行md5比较简单,首先是加载md5.h头文件,后使用头文件中的加密函数即可. 1. md5.h头文件内容如下 ...
- RAC:Oracle11gR2:群集的起、停、状态查询
一:查看群集的状态 1.0.1 使用crsctl status resource [-t] 1.0.2 使用crs_stat [-t] 1.0.1 使用srvctl status <obj> ...
- TFS对签入文件忽略设置,解决pdb弹出警告
我们在使用TFS项目老是出现冲突,要么编译的时候 提示PDB被签出这类的大量弹出,很烦人. 在群友的指点下,对签入文件进行限制.对PDB禁止签入以后,整个世界安静了.非常感谢 TFS=>服务器管 ...