XCMercy 2020-06-25
mybatis插值,数据提交事务回滚数据库值为空
通过sql日志查看sql为:INSERT INTO `quanxian`.`user` ( phone, email, password, times, name, age, address, sex, hobby, work ) VALUES (phone, email, password, times, name, age, address, sex, hobby, work );
1.0
初步怀疑是入参类型不对,于是检查类型:<insert id="addUser" parameterType="com.ry.project.vo.User">
入参类型没问题
2.0
继续查看Mapper接口:int addUser(User user);
接口没问题
3.0
于是控制台打印入参看看是否为空:{"address":"长江以北暂不繁华之处","age":20,"email":"","hobby":"电鱼","id":91,"name":"汤姆猫","password":"abc53814efg","phone":"18856519837","sex":"女","times":"2019-11-05 13:51:17","work":"电工"}
入参也没问题
4.0
回头继续检查mapper.xml文件
<trim prefixOverrides=","> <if test="phone !=null">, `phone`=#{phone} </if> <if test="email !=null">, `email`=#{email} </if> <if test="password!=null">, `password`=#{password}</if> <if test="times!=null">,`times`=#{times} </if> <if test="name!=null">,`name`=#{name} </if> <if test="age!=null">,`age`=#{age} </if> <if test="address!=null">,`address`=#{address} </if> <if test="sex!=null">,`sex`=#{sex}</if> <if test="hobby!=null">,`hobby`=#{hobby} </if> <if test="work!=null">, `work`=#{work}</if></trim>发现入参为实体时添加插值没有指定jdbc类型,于是添加类型<if test="phone != null">#{phone,jdbcType=VARCHAR},</if><if test="email != null">#{email,jdbcType=VARCHAR},</if><if test="password != null">#{password,jdbcType=VARCHAR},</if><if test="times != null">#{times,jdbcType=VARCHAR},</if><if test="name != null">#{name,jdbcType=VARCHAR},</if><if test="age != null">#{age,jdbcType=INTEGER},</if><if test="address != null">#{address,jdbcType=VARCHAR},</if><if test="sex != null">#{sex,jdbcType=VARCHAR},</if><if test="hobby != null">#{hobby,jdbcType=VARCHAR},</if><if test="work != null">#{work,jdbcType=VARCHAR},</if>至此问解决