关于mybaits的注解@Param

一、含义

  @param作为dao层的注解,为了解决多个参数时,参数类型不一致的问题。

 <select id="findEmpById" resultType="com.tsinkai.ettp.model.Emp" 
                                         parameterType="java.lang.Integer" >
        select 
            *
        from 
            emp
        where 
            id = #{id,jdbcType=INTEGER}
    </select>

当只有一个参数时,parameterType可以确定类型,但当有两个不同类型的参数时,就无法满足条件。

public Emp find(@Param("userName")String name,@Param("userAge")int age);
<select id="find" resultMap="BaseResultMap" >
        select 
            *
        from 
            emp
        where 
            name=#{userName}
            and
            age=#{userAge}
    </select>

使用@param后,就不需要指定parameterType了。

同时,可以使用@Param同时指定两个Pojo类。

public Emp find(@Param("s")Student student,@Param("t")Teacher teacher);
<select id="find" resultMap="BaseResultMap" >
        select 
            *
        from 
            emp
        where 
            name=#{t.name}
            and
            age=#{s.age}
    </select>
就算这个世道烂成一堆粪坑,那也不是你吃屎的理由
原文地址:https://www.cnblogs.com/whalesea/p/10931645.html