详解Mybatis多参数传递入参四种处理方式
作者:禹奕晗 时间:2022-12-27 15:18:20
1.利用参数出现的顺序
利用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);
参数出现顺序可以用arg0…到argn也可以用param1到param n设置表示第一个参数到第n个参数的位置
例子中arg0和param1表示第一个参数id,arg1和param2表示第二个参数
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(“内容”)里面的内容和我们sql语句里面#{内容}里面的内容需要一致才能查找到
3.使用map 需要map的键和#{内容}里面的内容一致
利用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.把参数封装在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的字段跟查询的#{内容}里面的内容一致
来源:https://blog.csdn.net/yu2875592987/article/details/106043970
标签:Mybatis,多参数,传递
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
.NET/C#实现识别用户访问设备的方法
2021-12-20 06:30:58
Java实现雪花算法(snowflake)
2022-10-27 03:43:23
![](https://img.aspxhome.com/file/2023/0/81300_0s.png)
EventBus与Spring Event区别详解(EventBus 事件机制,Spring Event事件机制)
2023-12-06 00:23:05
android动态加载布局文件示例
2023-12-10 20:42:46
Unity实现旋转扭曲图像特效
2023-12-16 08:35:17
![](https://img.aspxhome.com/file/2023/2/111272_0s.png)
Android的Service应用程序组件基本编写方法
2023-12-17 04:22:00
浅谈Spring自定义注解从入门到精通
2023-11-25 03:59:12
C/C++中typedef的用法大全
2023-02-11 08:57:47
Android应用程序转到后台并回到前台判断方法
2022-11-12 19:49:35
Java线程安全中的原子性浅析
2021-09-06 16:08:13
![](https://img.aspxhome.com/file/2023/0/67960_0s.png)
Java开发之Lombok指南
2022-11-19 21:49:28
![](https://img.aspxhome.com/file/2023/2/64202_0s.jpg)
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
java中SynchronizedList和Vector的区别详解
2023-08-23 10:13:12
Android可筛选的弹窗控件CustomFiltControl
2023-01-21 09:01:46
![](https://img.aspxhome.com/file/2023/8/94248_0s.jpg)
基于JavaMail API收发邮件的方法
2022-03-10 09:34:24
java对ArrayList排序代码示例
2023-11-24 20:14:21
深入了解Spring中最常用的11个扩展点
2023-07-05 17:46:40
![](https://img.aspxhome.com/file/2023/1/57531_0s.png)
Android实现计时与倒计时的常用方法小结
2023-10-28 07:18:02
Java maven三种仓库,本地仓库,私服,中央仓库的配置
2023-04-13 12:05:17
![](https://img.aspxhome.com/file/2023/2/62132_0s.png)
Android Support Palette使用详解
2023-04-05 15:47:24
![](https://img.aspxhome.com/file/2023/4/108264_0s.jpg)