詳解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語句比較簡單時(shí)推薦此方式)
@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è)置表示第一個(gè)參數(shù)到第n個(gè)參數(shù)的位置
例子中arg0和param1表示第一個(gè)參數(shù)id,arg1和param2表示第二個(gè)參數(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語句比較簡單時(shí)推薦此方式)
@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語句比較簡單時(shí)推薦此方式)
@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語句比較簡單時(shí)推薦此方式)
@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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用注解解決ShardingJdbc不支持復(fù)雜SQL方法
這篇文章主要為大家介紹了使用注解解決ShardingJdbc不支持復(fù)雜SQL方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞
這篇文章主要介紹了深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞,其中重點(diǎn)講解了ViewGroup的事件傳遞流程,需要的朋友可以參考下2016-02-02
Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn)
本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法
在java中經(jīng)常會(huì)遇到需要對(duì)數(shù)據(jù)進(jìn)行類型轉(zhuǎn)換的場景,這篇文章主要給大家介紹了關(guān)于Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
詳解Java并發(fā)編程之內(nèi)置鎖(synchronized)
這篇文章主要介紹了Java并發(fā)編程之內(nèi)置鎖(synchronized)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
java阻塞隊(duì)列BlockingQueue詳細(xì)解讀
這篇文章主要介紹了java阻塞隊(duì)列BlockingQueue詳細(xì)解讀,在新增的Concurrent包中,BlockingQueue很好的解決了多線程中,如何高效安全“傳輸”數(shù)據(jù)的問題,通過這些高效并且線程安全的隊(duì)列類,為我們快速搭建高質(zhì)量的多線程程序帶來極大的便利,需要的朋友可以參考下2023-10-10
spring?boot只需兩步優(yōu)雅整合activiti示例解析
這篇文章主要主要來教大家spring?boot優(yōu)雅整合activiti只需兩步就可完成測操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-03-03

