mybatis多个参数时传参方式

第一种方案

DAO层的函数方法 

Lecture getLecture(Integer id, Integer parentId);

对应的mapper.xml

  <select id="getLecture" resultMap="BaseResultMap">
    select  * from lecture
    where id = #{0} and parent_id = #{1}
  </select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

这种方案没有证实过

第二种方案

通过map传多参数

DAO层的函数方法 

Lecture getLecture(Map paramMap);

对应的mapper.xml

  <select id="getLecture" resultMap="BaseResultMap">
    select  * from lecture
    where id = #{id,jdbcType=INTEGER} and parent_id = #{parentId,jdbcType=INTEGER}
  </select>

Service层调用

Map paramMap=new hashMap();
paramMap.put(“id”,100);
paramMap.put(“parentId”,10);

这种方案每次调用都太麻烦,而且方法参数不直观

第三种方案

DAO层的函数方法 

Lecture getLecture(@Param("id") Integer id, @Param("parentId") Integer parentId);

这里得注意,使用@Param时,要引入的package路径为:

import org.apache.ibatis.annotations.Param;

对应的mapper.xml

<select id="getLecture" resultMap="BaseResultMap">
    select  * from lecture
    where id = #{id,jdbcType=INTEGER} and parent_id = #{parentId,jdbcType=INTEGER}
  </select>

猜你喜欢

转载自www.cnblogs.com/MTRD/p/12090966.html