在 mapper 中如何傳遞多個參數?


1、第一種:

DAO 層的函數

public UserselectUser(String name,String area);

對應的 xml,#{0}代表接收的是 dao 層中的第一個參數,#{1}代表 dao 層中第二

參數,更多參數一致往后加即可。

<select id="selectUser"resultMap="BaseResultMap">

select * fromuser_user_t

whereuser_name = #{0}

anduser_area=#{1}

</select>

2、第二種: 使用 @param 注解:

public interface usermapper {

user selectuser(@param(“username”) string

username,@param(“hashedpassword”) string hashedpassword);

}

然后,就可以在 xml 像下面這樣使用(推薦封裝為一個 map,作為單個參數傳遞給

mapper):

<select id=”selectuser” resulttype=”user”>

select id, username, hashedpassword

from some_table

where username = #{username}

and hashedpassword = #{hashedpassword}

</select>

3、第三種:多個參數封裝成 maptry {

//映射文件的命名空間.SQL 片段的 ID,就可以調用對應的映射文件中的

SQL

//由於我們的參數超過了兩個,而方法中只有一個 Object 參數收集,因此

我們使用 Map 集合來裝載我們的參數

Map < String, Object > map = new HashMap();

map.put("start", start);

map.put("end", end);

return sqlSession.selectList("StudentID.pagination", map);

} catch (Exception e) {

e.printStackTrace();

sqlSession.rollback();

throw e;

} finally {

MybatisUtil.closeSqlSession();

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM