MyBatis系列学习---《Mapper.xml 解析 二 - if标签》

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标签。

发布了63 篇原创文章 · 获赞 1 · 访问量 2780

猜你喜欢

转载自blog.csdn.net/A_bad_horse/article/details/104965777
今日推荐