mybits怎么传递参数
时间: 2024-05-09 12:15:54 浏览: 15
MyBatis可以通过Mapper接口的方法参数来传递参数。传递参数的方式有以下几种:
1. 单个参数:可以是任意类型的单个参数,如基本类型、对象、Map等。在Mapper接口方法中,使用@Param注解指定参数名称,如:
```
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") Integer id);
```
2. 多个参数:可以使用@Param注解指定多个参数的名称,或者使用Map来封装多个参数。如:
```
@Update("update user set name = #{name}, age = #{age} where id = #{id}")
int updateUser(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age);
@Update("update user set name = #{param1.name}, age = #{param1.age} where id = #{param2}")
int updateUser(Map<String, Object> map);
```
3. POJO参数:将POJO作为参数传递,MyBatis会自动将POJO的属性映射到SQL语句中的参数。如:
```
@Insert("insert into user(name, age) values(#{name}, #{age})")
int addUser(User user);
```
除了以上几种方式,还可以通过动态SQL等方式来传递参数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)