<resultMaptype="Orders"id="orders"><resultcolumn="user_id"property="userId"/></resultMap><selectid="selectOrdersList"resultMap="orders">
SELECT id, user_id, number, createtime, note FROM orders
</select>
动态sql 通过mybatis提供的各种标签方法实现动态拼接sql。
if 标签
where 标签:可以去掉第一个前and
<selectid="queryUserByWhere"parameterType="user"resultType="user">
SELECT id, username, birthday, sex, address FROM `user`
<!-- where标签可以自动添加where,同时处理sql语句中第一个and关键字 --><where><iftest="sex != null">
AND sex = #{sex}
</if><iftest="username != null and username != ''">
AND username LIKE
'%${username}%'
</if></where></select>
sql 片段
<!-- 声明sql片段 --><sqlid="userFields">
id, username, birthday, sex, address
</sql><selectid="queryUserByWhere"parameterType="user"resultType="user"><!-- SELECT id, username, birthday, sex, address FROM `user` --><!-- 使用include标签加载sql片段;refid是sql片段id -->
SELECT <includerefid="userFields"/> FROM `user`
<!-- where标签可以自动添加where关键字,同时处理sql语句中第一个and关键字 --><where><iftest="sex != null">
AND sex = #{sex}
</if><iftest="username != null and username != ''">
AND username LIKE
'%${username}%'
</if></where></select>