基于MyBatis的parameterType传入参数类型

目录
  • MyBatis的parameterType传入参数类型
    • 1. MyBatis的传入参数parameterType类型分两种
    • 2. 如何获取参数中的值
    • 3.案例
  • mybatis 之parameterType="Long"

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代码:

<select id="findUserById" parameterType="java.lang.Long" resultType="User">
        select * from user where  id = #{id};
</select>  

3.2 传入List

mapper接口代码:

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

xml代码:

<select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
    select * from user user
    <where>
        user.ID in (
          <foreach collection="list"  item="id" index="index" separator=",">
             #{id}
          </foreach>
        )
    </where>
</select>  

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

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

1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可

3.3 传入数组:

mapper接口代码:

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

xml代码:

<select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
    select * from user user
    <where>
        user.ID in (
           <foreach collection="array"  item="id" index="index"  separator=",">
                #{id}
           </foreach>
        )
    </where>
</select>

3.4 传入map

mapper接口代码:

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

xml代码:

<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM USER user
        <where>
            <if test="code != null">
                and user.CODE = #[code]
            </if>
            <if test="id != null">
                and user.ID = #{id}
            </if>
            <if test="idList !=null ">
                and user.ID in (
                   <foreach collection="idList" item="id" index="index" separator=",">
                        #{id}
                   </foreach>
                )
            </if>
        </where>
</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代码:

<select id="findUserList" parameterType="User" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM USER user
        <where>
            <if test="code != null">
                and user.CODE = #[code]
            </if>
            <if test="id != null">
                and user.ID = #{id}
            </if>
            <if test="idList !=null ">
                and user.ID in (
                    <foreach  collection="idList" item="id" index="index" separator=",">
                         #{id}
                    </foreach>
                )
            </if>
        </where>
</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代码:

<select id="selectUserByTime" resultType="User" parameterType="java.lang.String">
    select * from t_user
    where  create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')
</select>  

例子2:

注解javaBean

mapper接口代码:

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

xml代码:

<select id="selectUserByTime" resultType="User" parameterType="DateVO">
    select *
    from t_user
    where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')
          and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')
 </select>  

mybatis 之parameterType="Long"

 <select id="selectByPrimaryKeyByArrayMemberId"  resultType="memberModel" parameterType="Long">
           select
           <include refid="Base_Column_List"/>
              from member m
           where
           m.IS_DELETE = 'N'
           and m.member_id IN
           <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
             #{item,jdbcType=DECIMAL}
        </foreach>
    </select>
public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(
            List<Long> memberIds)
    {
        try
        {
            if (memberIds == null || memberIds.size()==0){
                return super.returnParamsError("参数为空!");
            }
            List<Member> list = memberMapper
            .selectByPrimaryKeyByArrayMemberId(memberIds);
            return super.returnCorrectResult(list);
        }
        catch (Throwable e)
        {
            return super.returnException(e);
        }
    }

    public ServiceMessage<List<Member>> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
    List<Member> selectByPrimaryKeyByArrayMemberId(List<Long> memberIds);
    @Test
    public void testSelectByPrimaryKeyByArrayMemberId()
    {
        InternalMemberService internalMemberService = J1SOAHessianHelper.getService(url,InternalMemberService.class);
        List<Long> memberIds = new ArrayList<Long>();
        memberIds.add(1l);
        memberIds.add(2l);
        memberIds.add(1855l);
        ServiceMessage<List<Member>> sm = internalMemberService.selectByPrimaryKeyByArrayMemberId(memberIds);
        System.out.println(sm.getResult());
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • MyBatis中传入参数parameterType类型详解

    前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType属性,用于对应的mapper接口方法接受的参数类型.本文主要给大家介绍了关于MyBatis传入参数parameterType类型的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 1. MyBatis的传入参数parameterType类型分两种 1. 1. 基本数据类型:int,string,long,Date; 1. 2. 复杂数据类

  • MyBatis 参数类型为String时常见问题及解决方法

    1. 参数为String时的插值问题 假设有下面一Dao接口方法 public Account findByAccountType (String type)throws DaoException; 对应的Mapper.xml <select id="findByAccountType " parameterType="string" resultType="account"> select * form account <wh

  • Mybatis中传递多个参数的4种方法总结

    前言 现在大多项目都是使用Mybatis了,但也有些公司使用Hibernate.使用Mybatis最大的特性就是sql需要自己写,而写sql就需要传递多个参数.面对各种复杂的业务场景,传递参数也是一种学问. 下面给大家总结了以下几种多参数传递的方法. 方法1:顺序传参法 #{}里面的数字代表你传入参数的顺序. 这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错. 方法2:@Param注解传参法 #{}里面的名称对应的是注解 @Param括号里面修饰的名称. 这种方法在参数不多的情况还

  • 基于MyBatis的parameterType传入参数类型

    目录 MyBatis的parameterType传入参数类型 1. MyBatis的传入参数parameterType类型分两种 2. 如何获取参数中的值 3.案例 mybatis 之parameterType="Long" MyBatis的parameterType传入参数类型 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的输入参数

  • MyBatis传入参数的实例代码

    在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型 基本数据类型:包含int,String,Date等.基本数据类型作为传参,只能传入一个.通过#{参数名} 即可获取传入的值 复杂数据类型:包含JAVA实体类.Map.通过#{属性名}或#{map的KeyName}即可获取传入的值 基本数据类型参数示例: 根据班级ID查询教师列表 x

  • 详解Mybatis 传递参数类型为List的取值问题

    问题描述: 参数传递为List时: 当传递一个 List 实例或者数组作为参数对象传给 Mybatis.此时,Mybatis 会自动将它包装在一个 Map 中,用名称在作为键.List 实例将会以"list" 作为键,而数组实例将会以"array"作为键.所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map. DAO 层: List<User> selectUserByIDs( List ID

  • MyBatis传入参数为List对象的实现

    SSM框架是JavaWeb必学的框架,虽说基本的增删改查很简单,但是当面临一些特殊情况时,有时还是会显得手足无措,此篇用来记录一些特殊场景下Mybatis框架的应用. 传入参数为List对象 1. 场景复现 首先有如下一张表: MySQL [test]> select * from t_entry_resource; +----+-------------+------+----------+--------+--------+---------------------+ | id | reso

  • MyBatis中如何接收String类型的参数实现

    在MyBatis学习初期,当parameterType的值为String<==>也就是接收String类型的参数时,我会通过value来接,如图: 通过value接收String类型的值舒适又简单,然而,直到有一天,我发现屡试不爽的value不给力了->接收String类型值的时候出了问题. 到底是什么凶残吓到了我的value->就是<bind/ >标签,大家请看: 这是配套数据表 查出来的结果让我很费解,用value接收值的时候出了这个问题(好好的"GZ&q

  • 使用mybatis的interceptor修改执行sql以及传入参数方式

    目录 mybatis interceptor修改执行sql以及传入参数 总体思路 1.Interceptor 代码实现 2.AutoConfiguration代码实现 mybatis interceptor 处理查询参数及查询结果 拦截器:拦截update,query方法 添加xml配置 mybatis interceptor修改执行sql以及传入参数 项目中途遇到业务需求更改,在查询某张表时需要增加条件,由于涉及的sql语句多而且依赖其他服务的jar,逐个修改sql语句和接口太繁杂.项目使用m

  • mybatis参数类型不匹配错误argument type mismatch的处理方案

    目录 参数类型不匹配错误argument type mismatch 错误日志 错误描述 错误原因 mybatis时argument type mismatch的坑 错误描述:参数类型不匹配 例如 参数类型不匹配错误argument type mismatch 错误日志 java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.execu

  • 基于python 将列表作为参数传入函数时的测试与理解

    将一个列表传入函数后,会对这个列表本身产生什么改变? 这就是本文主要考察的内容. list = [1,2,3,4,5,6,7] word = list.pop(0) print(word) print(list) # 输出结果理所当然地为: # 1 # [2, 3, 4, 5, 6, 7] # def a(temp): b = temp.pop(0) print(b) print(temp) a(list) # 输出结果为: # 2 # [3, 4, 5, 6, 7] # 此处,传给temp时,

随机推荐