詳解Mybatis多參數(shù)傳遞入?yún)⑺姆N處理方式
1.利用參數(shù)出現(xiàn)的順序
利用mapper.xml
<select id="MutiParameter" resultType="com.jt.mybatis.entity.User">
select * from user where id = #{param1} and username = #{param2}
</select>
利用mybatis注解方式(sql語句比較簡單時推薦此方式)
@Select("select * from user where id = #{arg0} and username = #{arg1}")
User MutiParameter(int id,String username);
參數(shù)出現(xiàn)順序可以用arg0…到argn也可以用param1到param n設置表示第一個參數(shù)到第n個參數(shù)的位置
例子中arg0和param1表示第一個參數(shù)id,arg1和param2表示第二個參數(shù)
2.使用注解需要使用到mybatis @Param注解
利用mapper.xml
<select id="MutiParameter" resultType="com.jt.mybatis.entity.User">
select * from user where id = #{id} and username = #{username}
</select>
利用mybatis注解方式(sql語句比較簡單時推薦此方式)
@Select("select * from user where id = #{id} and username = #{username}")
User MutiParameter(@Param("id")int id,@Param("username")String username);
@Param注解放在接口方法的前面@Param(“內(nèi)容”)里面的內(nèi)容和我們sql語句里面#{內(nèi)容}里面的內(nèi)容需要一致才能查找到
3.使用map 需要map的鍵和#{內(nèi)容}里面的內(nèi)容一致
利用mapper.xml
<select id="MutiParameter" resultType="com.jt.mybatis.entity.User">
select * from user where id = #{id} and username = #{username}
</select>
利用mybatis注解方式(sql語句比較簡單時推薦此方式)
@Select("select * from user where id = #{id} and username = #{username}")
User MutiParameter(Map<String, Object> params);
測試方法
@Test
public void testMutiParameter(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", 2);
params.put("username", "admin");
mapper.MutiParameter(params);
}
4.把參數(shù)封裝在Javabean中
利用mapper.xml
<select id="MutiParameter" resultType="com.jt.mybatis.entity.User">
select * from user where id = #{id} and username = #{username}
</select>
利用mybatis注解方式(sql語句比較簡單時推薦此方式)
@Select("select * from user where id = #{id} and username = #{username}")
User MutiParameter(User user);
測試方法
@Test
public void testMutiParameter(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
User user = new User();
user.setId(2);
user.setUsername("admin");
mapper.MutiParameter(user );
}
需要User的字段跟查詢的#{內(nèi)容}里面的內(nèi)容一致
到此這篇關(guān)于詳解Mybatis多參數(shù)傳遞入?yún)⑺姆N處理方式的文章就介紹到這了,更多相關(guān)Mybatis多參數(shù)傳遞入?yún)?nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java httpClient連接池支持多線程高并發(fā)的實現(xiàn)
本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法
在java中經(jīng)常會遇到需要對數(shù)據(jù)進行類型轉(zhuǎn)換的場景,這篇文章主要給大家介紹了關(guān)于Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
spring?boot只需兩步優(yōu)雅整合activiti示例解析
這篇文章主要主要來教大家spring?boot優(yōu)雅整合activiti只需兩步就可完成測操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步2022-03-03

