5
【笔记】MyBatis参数处理方式
source link: https://feiju12138.github.io/2022/09/28/MyBatis%E5%8F%82%E6%95%B0%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F/
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参数处理方式
多个参数的处理方式
- 当Java向MyBatis传递了一组参数时,根据参数的传递顺序,MyBatis会自动创建一个Map集合来存储参数
- 每个参数会向Map中创建2个键值对,第一个参数的键名分别是
arg0
和param1
,值为参数值,以此类推
src/main/java/com/mapper/UserMapper.java
void test(int id, String name)
src/main/resources/com/mapper/UserMapper.xml
<select id="test">
SELECT * FROM user
WHERE id = #{arg0}
AND name = #{arg1};
</select>
<select id="test">
SELECT * FROM user
WHERE id = #{param1}
AND name = #{param2};
</select>
- 可以通过
@Param()
注解修改Map中以arg
开头的键名
src/main/java/com/mapper/UserMapper.java
void test(@Param("id") int id, @Param("name") String name)
src/main/resources/com/mapper/UserMapper.xml
<select id="test">
SELECT * FROM user
WHERE id = #{id}
AND name = #{name};
</select>
<select id="test">
SELECT * FROM user
WHERE id = #{param1}
AND name = #{param2};
</select>
单个数据的处理方式
- 无论什么类型的数据传递到MyBatis事时,都会被MyBatis封装为Map集合
- 如果是以下类型,默认会自动封装Map集合
- POJO类型
- Map集合
Collection的封装方式
- 键为
arg0
,值为集合 - 键为
collection
,值为集合
List的封装方式
- 键为
arg0
,值为集合 - 键为
collection
,值为集合 - 键为
list
,值为集合
数组的封装方式
- 键为
arg0
,值为数组 - 键为
array
,值为数组
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK