. Primary data type
mapper interface
1
User selectByPrimaryKey(Integer id);
sql mapping
1
2
3
4
5
6<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
The id
in this condition can be any name, it all represent the Integer parameter.
Discuss
if Integer argument need be judge use _parameter
represent it in tag <if>
1
2
3
4
5
6
7
8<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_user
<if test="_parameter != 0">
where id = #{id,jdbcType=INTEGER}
</if>
</select>
Object data type
mapper interface
1
int insert(User user);
sql mapping
1
2
3<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into tb_user (name)
values (#{name,jdbcType=CHAR})The name in value must be the User object’s property name
- If
<if>
tag want to judge whether user is null,_parameter
variable need be used. like above discuss content.
Map data type
mapper interface
1
int updateByExample(@Param("user") User user, @Param("condition") Condition condition);
sql mapping
1
2
3
4
5
6
7
8
9
10<update id="updateByCondition" parameterType="map" >
update tb_user
set id = #{user.id,jdbcType=INTEGER},
...
<if test="condition != null" >
<include refid="Update_By_Condition_Where_Clause" />
</if>
<if test="_parameter != null" >
<include refid="Update_By_Default_Where_Clause" />
</if>use
#{keyname}
to reference target- @Param(“…”) is alias manule point the map key name
- Multi argument will auto box into a map
Collection data type
You can pass a List instance or an Array to MyBatis as a parameter object. When you do, MyBatis will automatically wrap it in a Map, and key it by name. List instances will be keyed to the name “list” and array instances will be keyed to the name “array”.
mapper interface
1
List<User> selectUserInList(List<Interger> ids);
sql mapping
1
2
3
4
5
6
7
8<select id="selectUserInList" resultType="User">
select * from user
where id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
Object’s collection
mapper interface
1
List<User> selectByCondition(Condition condition)
sql mapping
1
2
3<where >
<foreach collection="threshold" item="limit" separator="or" >
<if test="limit.value > 10" >threshold
is a collection property in conditionlimit
represent the item in collection