mybatis优化写法
mapper.xml 简化技巧
insert 可复用
<sql id= "setField" > < set > <if test= "taskVersionId != null" >task_version_id = #{taskVersionId},</if> <if test= "uid != null" >uid = #{uid},</if> <if test= "userName != null" >user_name = #{userName},</if> <if test= "greyDate != null" >grey_date = #{greyDate},</if> </ set > </sql> insert into h_grey_record <include refid= "setField" /> </ insert > |
update复用
<sql id= "setUpdateField" > < set > <if test= "pushDevice.pushSwitchStatus != null" >push_switch_status = #{pushDevice.pushSwitchStatus},</if> </ set > </sql> |
query复用
<sql id= "query_user_where" > < where > <if test= "id!=null and id!=''" > and id=#{id} </if> <if test= "username!=null and username!=''" > and username like '%${username}%' </if> </ where > </sql> //使用include引用sql片段 < select id= "findUserList" parameterType= "user" resultType= "user" > select * from user <include refid= "query_user_where" /> </ select > |