Mybatis入门之动态sql
动态拼接sql语句,在我的理解就是相当于Java中的逻辑控制语句(if,,switch。。。)
- 我们要根据id,username,sex。。。做多条件查询
if语句:可以判断参数是否有值
<select id="getList" resultType="cn.zw.pojo.User">
SELECT
id,
username,
sex,
birthday,
address
FROM
user
WHERE
<if test="id!=0">
id > 0
</if>
</select>
where语句:去掉多余的and
<select id="getList" resultType="cn.zw.pojo.User">
SELECT
id,
username,
sex,
birthday,
address
FROM
user
WHERE 1=1
<where>
<if test="id!=0">
and id > 0
</if>
<if test="sex != null">
and sex = '男'
</if>
</where>
</select>