【笔记】MyBatis参数传递
source link: https://feiju12138.github.io/2022/09/26/MyBatis%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
MyBatis多个参数传递学习笔记
- 散装方式可以直接传递变量,此时接口中的变量名就是xml的参数名
- 也可以通过
@Param("")
注解指定参数名的映射关系
src/main/java/com/mapper/UserMapper.java
public interface UserMapper {
List<User> selectAll(int id, @Param("username") String name);
}
src/main/resources/com/mapper/UserMapper.xml
<select>
SELECT * FROM user
WHERE id = #{id}
AND name = #{username}
</select>
封装成对象
- xml会自动从对象中的属性映射参数
src/main/java/com/pojo/User.java
public class User {
Integer id;
String name;
}
src/main/java/com/mapper/UserMapper.java
public interface UserMapper {
List<User> selectAll(User user);
}
src/main/resources/com/mapper/UserMapper.xml
<select>
SELECT * FROM user
WHERE id = #{id}
AND name = #{name}
</select>
Map集合
- xml会自动从Map中的键值对映射参数
src/main/java/com/mapper/UserMapper.java
public interface UserMapper {
List<User> selectAll(Map map);
}
src/main/resources/com/mapper/UserMapper.xml
<select>
SELECT * FROM user
WHERE id = #{id}
AND name = #{name}
</select>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK