【转载】mybatis根据条件判断来执行插入或者更新

转自:https://blog.csdn.net/qq_20867981/article/details/80422824

 
   
   
      select count(*) as count from phone2email where phone = #{phone,jdbcType=VARCHAR}
   

 
   
   
      update phone2email set email=#{email,jdbcType=VARCHAR} where phone = #{phone,jdbcType=VARCHAR}
   

 
   
   
      insert into phone2email(phone,email)
      values(#{phone,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR})
   

 
 

这里的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根据条件判断来执行插入或者更新)