工具:mybatis+pgsql
注意:
以下三种判断查询的结果是不一样的
<if test = "column != null">
<if test = "column != null and column != '' ">
<if test = "column != '' ">
数据库数据如下:
id username sex address depId status
1 张三 男 上海 001 1
2 李四 女 上海 1
3 王五 女 上海 002 1
案例1
dao层
User select(String username,String depId);
sql
<select id = "select" resultType = "com.test.dto.User">
select username,address,sex
from user
where status = 1
and username = #{username}
<if test = "depId != null">
and depId = #{depId}
</if>
</select>
案例2
dao层
List<User> list(String address);
sql
<select id = "list" resultType = "com.test.dto.User">
select username,address,sex
from user
where sex = '女'
<if test = "address != null and address != '' ">
and address like '%' || #{address} || '%'
</if>
</select>
案例3
dao层
List<User> list(String address);
sql
<select id = "list" resultType = "com.test.dto.User">
select username,address,sex
from user
where sex = '女'
<if test = "address != '' ">
and address like '%' || #{address} || '%'
</if>
</select>