MyBatis系列学习---《Mapper.xml 解析 三 - where标签 01》

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} and
    </if>
    <if test="username != null and username != ''">
      username = #{username}
    </if>
  </where>
</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);
}
发布了63 篇原创文章 · 获赞 1 · 访问量 2779

猜你喜欢

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