通过mybatis提供的各种标签方法实现动态拼接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>
原文:https://www.cnblogs.com/deepSleeping/p/10455193.html