MyBatis的映射文件
动态sql语句
- if
当查询时,某个字段客户在查询时,没有放入值
<select id="findByCondition" parameterType="user" resultType="user">
select * from user where 1=1
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</select>
- foreach
<select id="findByIds" parameterType="list" resultType="user">
select * from user
<where>
// open 从哪里开始 从id(开始, )结束
// item中填写一个变量,负责接收每一个值
// separator 括号中用什么隔开
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
- sql片段的抽取
<!--sql语句的抽取-->
<sql id="selectUser">select * from user </sql>
<!--调用时-->
<select id="findByCondition" parameterType="user" resultType="user">
<include refid="selectUser"></include>
MyBatis映射文件配置:
- 查询 select
- 插入 insert
- 修改 update
- 删除 delete
- where 条件where
- if判断 if
- 循环 foreach
- sql片段抽取 sql