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 id="insert" parameterType="com.**.**.app.grey.core.bean.GreyRecordDO">     insert into h_grey_record     <include refid="setField"/> </insert>

update复用

<sql id="setUpdateField">     <set>         <if test="pushDevice.appId != null">app_id = #{pushDevice.appId},</if>         <if test="pushDevice.deviceId != null">device_id = #{pushDevice.deviceId},</if>         <if test="pushDevice.registrationId != null">registration_id = #{pushDevice.registrationId},</if>         <if test="pushDevice.os != null">os = #{pushDevice.os},</if>         <if test="pushDevice.osVersion != null">os_version = #{pushDevice.osVersion},</if>         <if test="pushDevice.appVersion != null">app_version = #{pushDevice.appVersion},</if>         <if test="pushDevice.mobileBrands != null">mobile_brand = #{pushDevice.mobileBrands},</if>         <if test="pushDevice.mobileModel != null">mobile_model = #{pushDevice.mobileModel},</if>         <if test="pushDevice.pushSwitchStatus != null">push_switch_status = #{pushDevice.pushSwitchStatus},</if>         <if test="pushDevice.regIdType != null">reg_id_type = #{pushDevice.regIdType},</if>         <if test="pushDevice.bundleId != null">bundle_id = #{pushDevice.bundleId},</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>

相关文章

今天小编给大家分享的是MyBatis批量查询、插入、更新、删除如...
今天小编给大家分享的是Mybatis操作多数据源实现的方法,相信...
本篇文章和大家了解一下mybatis集成到spring的方式有哪些。有...
今天小编给大家分享的是mybatis-plus分页查询的3种方法,相信...
本篇内容主要讲解“mybatis之BaseTypeHandler怎么使用”,感...
这篇文章主要介绍了mybatisforeach怎么传两个参数的相关知识...