总结我所用到的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}等对应实体类中属性。
相关推荐
主要介绍了Mybatis中@Param的用法和作用,在文中给大家补充了spring中@param和mybatis中@param使用区别,需要的朋友可以参考下
方法多参数@Param。
主要介绍了Mybatis使用@param注解四种情况解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了mybatis多个接口参数的注解使用方式(@Param),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了浅谈为什么要使用mybatis的@param,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
mybatis-param-clazzStudent-222mybatis-param-clazzStudent-222
我们经常会遇到表中的字段名和表对应实体类的属性名称不一定都是完全相同的情况,如何解决呢?下面脚本之家小编给大家介绍MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题,一起学习吧
mybatis各种功能实例
IDEA插件free mybatis plugin下载、 xml和mapper之间的有用导航 支持生成语句,@Param注释和xml的映射器 在xml中支持一些有用的mapper重命名 支持mapper xml中select语句的正确结果类型 支持mapper xml的正确...
(一)Mybatis plugin插件主要功能有: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联(属性A...
Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...
Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...
SpringBoot+mybatis基于注解式增删改查框架源码程序, @Insert("INSERT INTO USER(nick_name, user_code) VALUES(#{nickName}, #{userCode})") int insertParam(@Param("nickName") String nickName, @Param(...
mybatis_plugin是啥? 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联(属性A.属性B.属性C) 快捷键生成@Param注解 XML中编辑SQL时, 支持参数...
XML中编辑SQL时, 支持参数自动补全(基于@Param注解识别参数) 自动检查Mapper XML文件中ID冲突 自动检查Mapper XML文件中错误的属性值 支持Find Usage 支持重构从命名 支持别名 自动生成ResultMap属性 快捷键:...
mybatis-async mybatis-async,是封装了mybatis的异步框架。 逻辑图如下: 工程依赖 Jdk 1.8 Maven 3.x mybits 3.x 调用示例 实现规范 AsyncMethod 注解, 标记Mapper的某个方法是异步调用的 AsyncType 注解, 异步的...
直接运行 generator.sh... * @param hid 医院id */ public void setHid(Integer hid) { this.hid = hid; } /** * 医生id * @return DOCTOR_ID 医生id */ public String getDoctorId() { return doctorId; }
搭建mybatis所需包org.apache.ibatis.annotations.Param @Param
4.取变量时,如果dao层接口使用的是@param("别名")注解,则根据别名取值 5.mapper.xml中$和#取值的区别 4.mybatis的xml中如何设置返回值 resultType返回的数据类型 5.$和#区别 1. #将传入的数据都当成一个字符串,会...
XML中编辑SQL时, 支持参数自动补全(基于@Param注解识别参数) 自动检查Mapper XML文件中ID冲突 自动检查Mapper XML文件中错误的属性值 支持Find Usage 支持重构从命名 支持别名 自动生成ResultMap属性 快捷键: Option...