时间的法外狂徒

导航

关于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>

 

posted on 2019-05-27 16:56  抄手砚  阅读(498)  评论(0编辑  收藏  举报