1. 程式人生 > 實用技巧 >mybatis中mapper傳多個入參

mybatis中mapper傳多個入參

有三種方式

1、使用佔位符#{0},#{1}....對應順序就是引數的順序

#方法簽名
List<TbItem> selectByPage(int page, int rows);

#sql語句
<select id="selectByPage" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    from tb_item LIMIT #{0} , #{1}
  </select>

2、使用map封裝入參

#生成map入參
public List<TbItem> getItemByPage(int page , int rows){
        Map paramMap = new HashMap();
        paramMap.put("page",page);
        paramMap.put("rows" , rows);
        List<TbItem> tbItems = tbItemMapper.selectByPage(paramMap);
        return tbItems;
    }

#sql
<select id="selectByPage" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    from tb_item LIMIT #{page} , #{rows}
  </select>

3、使用@Param

#mapper中介面的簽名
List<TbItem> selectByPage(@Param("page") int page , @Param("rows") int rows);

#sql
<select id="selectByPage" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    from tb_item LIMIT #{page} , #{rows}
  </select>