Mybatis学习(三)——动态sql

在mapper.xml文件中使用<where> <if test> <foreach>标签实现动态sql

 

  <where>和<if>标签

    <where>

      <if test="(对象值)!=null">

        ....

      </if>

    <where>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="xyz.javaswing.mapper.UserMapper">
    <select id="findUserById" parameterType="int" resultType="user">
        select * from user where id = #{id}
    </select>

    <select id="findUser" parameterType="user" resultType="user">
        select * from user
        <where>
            <if test="id!=0">
                id = #{id}
            </if>
            <if test="username!=null">
                username = #{username}
            </if>
            <if test="password!=null">
                password = #{password}
            </if>
        </where>
    </select>
</mapper>

 

posted @ 2022-03-11 19:16  remix_alone  阅读(27)  评论(0)    收藏  举报