使用mybatis插入数据时,mysql的字段默认值不生效的问题
解决办法:
使用动态sql(记录本人踩过的坑)
<sql id="if_insert_column">
<trim suffixOverrides=",">
<if test="zxywzj != null">ZXYWZJ,</if>
<if test="zxywnr != null">ZXYWNR,</if>
<if test="jgid != null">JGID,</if>
<if test="zxzt != null">ZXZT,</if>
<if test="dwzj != null">DWZJ,</if>
<if test="cjsj != null">CJSJ,</if>
<if test="cjr != null">CJR,</if>
<if test="xgsj != null">XGSJ,</if>
<if test="xgr != null">XGR,</if>
<if test="bz != null">BZ</if>
</trim>
</sql>
<sql id="if_insert_value">
<trim suffixOverrides=",">
<if test="zxywzj != null">#{zxywzj},</if>
<if test="zxywnr != null">#{zxywnr},</if>
<if test="jgid != null">#{jgid},</if>
<if test="zxzt != null">#{zxzt},</if>
<if test="dwzj != null">#{dwzj},</if>
<if test="cjsj != null">#{cjsj},</if>
<if test="cjr != null">#{cjr},</if>
<if test="xgsj != null">#{xgsj},</if>
<if test="xgr != null">#{xgr},</if>
<if test="bz != null">#{bz}</if>
</trim>
</sql>
<!--新增所有列 并返回主键 这种方式存在问题:主键不一致-->
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="zxywzj">
insert into tb_name
(<include refid="if_insert_column"/>)
values
(<include refid="if_insert_value"/>)
</insert>