MyBatis的parameterType传入参数类型

 

        在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的输入参数类型即可。parameterType有基本数据类型和复杂的数据类型配置。

 

1. MyBatis的传入参数parameterType类型分两种

   1. 1. 基本数据类型:int、string、long、Date;

   1. 2. 复杂数据类型:类(JavaBean、Integer等)和Map

 

2. 如何获取参数中的值:

   2.1  基本数据类型:#{参数} 获取参数中的值

   2.2  复杂数据类型:#{属性名}  ,map中则是#{key}

 

3.案例:

 3.1 传入Long型

mapper接口代码:

public User findUserById(Long id);  

xml代码:

  1. <select id="findUserById" parameterType="java.lang.Long" resultType="User">

  2. select * from user where id = #{id};

  3. </select>

 

 3.2  传入List

mapper接口代码:

public List<User> findUserListByIdList(List<Long> idList);    

xml代码:

  1. <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">

  2. select * from user user

  3. <where>

  4. user.ID in (

  5. <foreach collection="list" item="id" index="index" separator=",">

  6. #{id}

  7. </foreach>

  8. )

  9. </where>

  10. </select>

在使用foreach的时候最关键的也是最容易出错的就是collection属性。

该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

    1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
    2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
    3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可

 

3.3 传入数组

mapper接口代码:

public List<User> findUserListByIdList(int[] ids);  

xml代码:

  1. <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">

  2. select * from user user

  3. <where>

  4. user.ID in (

  5. <foreach collection="array" item="id" index="index" separator=",">

  6. #{id}

  7. </foreach>

  8. )

  9. </where>

  10. </select>

 

3.4 传入map

mapper接口代码:

public boolean exists(Map<String, Object> map);  

xml代码:

  1. <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">

  2. SELECT COUNT(*) FROM USER user

  3. <where>

  4. <if test="code != null">

  5. and user.CODE = #{code}

  6. </if>

  7. <if test="id != null">

  8. and user.ID = #{id}

  9. </if>

  10. <if test="idList !=null ">

  11. and user.ID in (

  12. <foreach collection="idList" item="id" index="index" separator=",">

  13. #{id}

  14. </foreach>

  15. )

  16. </if>

  17. </where>

  18. </select>

MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。

比如这里MAP含有一个名为idList的list,所以MAP中用idList取值,这点和单独传list或array时不太一样。

 

3.5传入JAVA对象

mapper接口代码:

public int findUserList(User user);   

xml代码:

  1. <select id="findUserList" parameterType="User" resultType="java.lang.Integer">

  2. SELECT COUNT(*) FROM USER user

  3. <where>

  4. <if test="code != null">

  5. and user.CODE = #{code}

  6. </if>

  7. <if test="id != null">

  8. and user.ID = #{id}

  9. </if>

  10. <if test="idList !=null ">

  11. and user.ID in (

  12. <foreach collection="idList" item="id" index="index" separator=",">

  13. #{id}

  14. </foreach>

  15. )

  16. </if>

  17. </where>

  18. </select>

JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。

比如这里User含有一个名为idList的list,所以User中用idList取值,这点和单独传list或array时不太一样。

 

3.6注解@Param

例子1:

注解单一属性;这个类似于将参数重命名了一次

mapper接口代码:

List<User> selectUserByTime(@Param(value="startdate")String startDate);  

xml代码:

  1. <select id="selectUserByTime" resultType="User" parameterType="java.lang.String">

  2. select * from t_user

  3. where create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')

  4. </select>

例子2:

注解javaBean

mapper接口代码:

List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);  

xml代码:

  1. <select id="selectUserByTime" resultType="User" parameterType="DateVO">

  2. select *

  3. from t_user

  4. where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')

  5. and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')

  6. </select>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值