增删改查的比较好的写法

1、增加

<insert id="insertInfo" parameterType="drivercar">
insert into t_drivers_cars
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id !=null" >id,</if>
<if test="driverId !=null">driverId,</if>
<if test="carNum !=null" >carNum,</if>
<if test="carTypeId !=null">carTypeId,</if>
<if test="carLengthId !=null">carLengthId,</if>
<if test="loadWeight !=null">loadWeight,</if>
<if test="createTime !=null">createTime,</if>
<if test="isDefault !=null">isDefault,</if>
<if test="createDriverId !=null">createDriverId,</if>
<if test="state !=null">state,</if>
<if test="verificationErrorDesc !=null">verificationErrorDesc,</if>
<if test="travelLicenseId !=null">travelLicenseId,</if>
<if test="travelLicenseId_fan !=null">travelLicenseId_fan,</if>
<if test="postType !=null">postType</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides="">
<if test="id !=null">#{id},</if>
<if test="driverId !=null">#{driverId},</if>
<if test="carNum !=null">#{carNum},</if>
<if test="carTypeId !=null">#{carTypeId},</if>
<if test="carLengthId !=null">#{carLengthId},</if>
<if test="loadWeight !=null">#{loadWeight},</if>
<if test="createTime !=null">#{createTime},</if>
<if test="isDefault !=null">#{isDefault},</if>
<if test="createDriverId !=null">#{createDriverId},</if>
<if test="state !=null">#{state},</if>
<if test="verificationErrorDesc !=null">#{verificationErrorDesc},</if>
<if test="travelLicenseId !=null">#{travelLicenseId},</if>
<if test="travelLicenseId_fan !=null">#{travelLicenseId_fan},</if>
<if test="postType !=null">#{postType}</if>
</trim>
</insert>

2、删除

<delete id="deleteInfo" parameterType="drivercar">
delete from t_drivers_cars where id=#{id}
</delete>

3、修改

<update id="updateInfo" parameterType="drivercar">
update t_drivers_cars
<set>
<if test="driverId !=null">driverId = #{driverId},</if>
<if test="carNum!=null">carNum = #{carNum},</if>
<if test="carTypeId!=null">carTypeId = #{carTypeId},</if>
<if test="carLengthId!=null">carLengthId = #{carLengthId},</if>
<if test="loadWeight!=null">loadWeight = #{loadWeight},</if>
<if test="createTime!=null">createTime = #{createTime},</if>
<if test="isDefault!=null">isDefault = #{isDefault},</if>
<if test="verificationErrorDesc !=null">verificationErrorDesc = #{verificationErrorDesc},</if>
<if test="createDriverId !=null">createDriverId = #{createDriverId},</if>
<if test="travelLicenseId !=null">travelLicenseId = #{travelLicenseId},</if>
<if test="travelLicenseId_fan !=null">travelLicenseId_fan = #{travelLicenseId_fan},</if>
<if test="postType !=null">postType = #{postType},</if>
<if test="state !=null">state = #{state},</if>
<if test="isDel !=null">isDel = #{isDel}</if>
</set>
where id = #{id}
</update>

4、查询

<select id="getCount" parameterType="java.util.HashMap" resultType="Integer">
select count(1) from t_drivers_cars
<where>
<if test="states != null">
<foreach collection="states" item="state" open="(" close=")" separator="or">
state=#{state}
</foreach>
</if>
<if test="carNum != null">and carNum like '%${carNum}%'</if>
<if test="userName != null">and userName like '%${userName}%'</if>
</where>
</select>

以上基本的增删改查这样写的话是比较严谨的

原文地址:https://www.cnblogs.com/haoxiu1004/p/9680292.html