select * from user where sex = 1 and username = "Jack";
<!-- 对应Mapper.xml SQL语句 -->
<select id="selectUserBySexAndUserName" resultMap="UserExample" parameterType="com.huarenwenyu.client.dao.UserExample">
select * from user
where
<if test="sex != null and sex != ''">
sex = #{sex}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
</select>
<!-- 测试代码 -->
User user = new User();
user.setSex("1");
user.setUsername("Jack");
List<User> users = userMapper.selectUserBySexAndUserName(user);
for (User user2 : users) {
System.out.println(user2);
}
<!-- 运行报错 -->
user.setSex("1"); // 运行后报错如下
###SQL: select * from user where and username = ?
使用where标签可以解决这个问题,请继续阅读where标签。