mybatis优化写法


mapper.xml 简化技巧
insert 可复用

"setField">     <set>         "taskVersionId != null">task_version_id = #{taskVersionId},         "uid != null">uid = #{uid},         "userName != null">user_name = #{userName},         "greyDate != null">grey_date = #{greyDate},     set>   <insert id="insert" parameterType="com.**.**.app.grey.core.bean.GreyRecordDO">     insert into h_grey_record     "setField"/> insert>

update复用

"setUpdateField">     <set>         "pushDevice.appId != null">app_id = #{pushDevice.appId},         "pushDevice.deviceId != null">device_id = #{pushDevice.deviceId},         "pushDevice.registrationId != null">registration_id = #{pushDevice.registrationId},         "pushDevice.os != null">os = #{pushDevice.os},         "pushDevice.osVersion != null">os_version = #{pushDevice.osVersion},         "pushDevice.appVersion != null">app_version = #{pushDevice.appVersion},         "pushDevice.mobileBrands != null">mobile_brand = #{pushDevice.mobileBrands},         "pushDevice.mobileModel != null">mobile_model = #{pushDevice.mobileModel},         "pushDevice.pushSwitchStatus != null">push_switch_status = #{pushDevice.pushSwitchStatus},         "pushDevice.regIdType != null">reg_id_type = #{pushDevice.regIdType},         "pushDevice.bundleId != null">bundle_id = #{pushDevice.bundleId},     set>

query复用

"query_user_where">     <where>         "id!=null and id!=''"and id=#{id}         "username!=null and username!=''"and username like '%${username}%'      where>   //使用include引用sql片段 <select id="findUserList" parameterType="user" resultType="user">     select from user      "query_user_where"/> select>