后端学习笔记(七)--MyBatis参数传递

发布于:2024-08-19 ⋅ 阅读:(139) ⋅ 点赞:(0)

5.MyBatis参数传递

​ *MyBatis接口方法中可以接收各种各样的参数,MyBatis底层对于这些参数进行不同的封装处理方式

​ *单个参数:
1.POJO类型:直接使用,属性名和参数占位符名称一致
2.Map集合:直接使用,键名和参数占位符一致
3.Collection:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
​ map.put(“arg0”,collection集合)
​ map.put(“collection”,collection集合)
4.List:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
​ map.put(“arg0”,list集合);
​ map.put(“collection”,list集合);
​ map.put(“list”,list集合);
5.Array:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名
​ map.put(“arg0”,数组);
​ map.put(“array”,数组);
6.其它类型:直接使用
* *多个参数:封装为Map集合,可以使用@Param注解,替换Map集合中默认的键名
​ map.put(“arg0”,参数值1)
​ map.put(“param1”,参数值1)
​ map.put(“param2”,参数值2)
​ map.put(“arg1”,参数值2)
​ * ----------------@Param(“username”)
​ map.put(“username”,参数值1)
​ map.put(“username”,参数值1)
​ map.put(“password”,参数值2)
​ map.put(“password”,参数值2)

User select(@Param("username")String username,@Param("password")String pass word)
<select id="select" resultType="user">
    select *
    from tb_user
    where 
    	username =#{username}	
	and password =#{password};
</select>

6.注解完成增删改查

​ *使用注解开发会比配置文件开发更加方便

@Select("select * from tb_user where id = #{id}")
public User selectById(int id);

​ *查询:@Select

​ *添加:@Insert

​ *修改:@Update

​ *删除:@Delete

​ *提示:

​ 1.注解完成简单功能

​ 2.配置文件完成复杂功能


网站公告

今日签到

点亮在社区的每一天
去签到