转自:https://blog.youkuaiyun.com/qq_20867981/article/details/80422824
<insert id="updateOrInsertPhone2Email" useGeneratedKeys="true" keyProperty="id" parameterType="com.sa.pojo.Phone2Email" >
<!-- 查看是否存在memberid,如果存在及更新,否则插入 -->
<selectKey keyProperty="count" order="BEFORE" resultType="int">
select count(*) as count from phone2email where phone = #{phone,jdbcType=VARCHAR}
</selectKey>
<!-- 如果大于0则更新 -->
<if test="count>0">
update phone2email set email=#{email,jdbcType=VARCHAR} where phone = #{phone,jdbcType=VARCHAR}
</if>
<!-- 如果等于0则保存 -->
<if test="count==0">
insert into phone2email(phone,email)
values(#{phone,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR})
</if>
</insert>
这里的count要有setter方法,也就是要作为Phone2Email(对象)的属性:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Phone2Email {
private Integer id;
private String phone;
private String email;
private int count;
}
如果没有则会报错:
Caused by: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'count' in com.sa.pojo.Phone2Email.
本文详细介绍了在MyBatis中如何实现基于手机号码的邮箱信息更新或插入操作,通过使用<selectKey>标签获取记录数,再根据记录数决定进行更新还是插入,确保了数据的一致性和完整性。
1175

被折叠的 条评论
为什么被折叠?



