通过 mybatis 提供的各种标签方法实现动态拼接 sql.
1,if,where,sql,include 标签 (条件, sql 片段)
- <sql id="selector">
- select * from account
- </sql>
- <!-- 根据性别和名称查询用户 where 标签可以去掉第一个前 and -->
- <select id="selectUserBySexAndUsername" parameterType="deep.pojo.Account"
- resultType="deep.pojo.Account">
- <include refid="selector"/>
- <where>
- <if test="sex != null and sex !='' ">
- sex = #{sex}
- </if>
- <if test="username != null and username !=''">
- and username = #{username}
- </if>
- </where>
- </select>
来源: http://www.bubuko.com/infodetail-2971841.html