在sql優化中,一般不允許直接使用select*from ,如果是全字段查詢就需要寫上所有的字段,這時候就可以
查詢條件優化
<sql id="Base_Column_List">
userMainId, userPassword, authtype, modifyPwdTime, modifyPwdFlag, OpenId, VerifyCode
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from bug_LoginUser
where userMainId = #{userMainId,jdbcType=VARCHAR}
</select>
在使用sql片段時使用include标簽通過sql片段的id進行引用,sql片段的ID在目前空間必須為唯一的
where條件
當然,sql片段中也可以寫其他的内容,隻要符合文法規範都是可以的。如下:
<sql id="sql_where">
<trim prefix="WHERE" prefixoverride="AND | OR">
<if test="id != null">AND id=#{id}</if>
<if test="name != null and name.length()>0">AND name=#{name}</if>
<if test="gender != null and gender.length()>0">AND gender=#{gender}</if>
</trim>
</sql>
<select id="updateByKey" parameterType="Map" resultType="List">
select * from user
<include refid="sql_where">
</select>
總結
利用sql标簽就可以将一些常用的sql進行複用