Mybatis傳遞多個參數

1、使用索引?#{index}

DAO層函數方法app

Public User selectUser(String name, String area);

  Mapper.xml中SQL函數

<select id="selectUser" resultMap="BaseResultMap">
    select  
        *  
    from 
        user_user_t
    where 
        user_name = #{0} and user_area=#{1}
</select>

  

2、使用Map

DAO層函數方法code

Public User selectUser(Map<String, Object> map);

  Mapper.xml中SQLxml

<select id="selectUser" resultMap="BaseResultMap">
    select  
        *  
    from 
        user_user_t
    where 
    user_name = #{username, jdbcType=VARCHAR} 
    and user_area = #{userarea, jdbcType=VARCHAR}
</select>

  Service層函數調用blog

public User selectUser() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("username", "zhangsan");
    map.put("userarea", "beijing");
    User user = mapper.selectUser(map); 
}

  

3、使用註解

DAO層函數索引

public User selectUser(@Param("userName")String username, @Param("userArea")String userarea);

  Mapper.xml中SQLclass

<select id="selectUser" resultMap="BaseResultMap">
    select  
        *  
    from 
        user_user_t
    where 1 = 1 
    and user_name = #{userName} 
    and user_area=#{userArea}
</select>
相關文章
相關標籤/搜索