MyBatis返回主键

mapper文件中返回主键:

  <insert id="insert" parameterType="com.za.cls.model.CompanyLeaseInfo" >
    <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into company_lease_info (name, code, business_licence, 
      oper_name, oper_phone, card_no, 
      province_code, province_name, city_code, 
      city_name, area_code, area_name, 
      addr_detail, founded_time, contact_realname, 
      contact_phone, contact_email, manager_id, 
      open_account_status, status, create_time, 
      modify_time)
    values (#{name,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, #{businessLicence,jdbcType=VARCHAR}, 
      #{operName,jdbcType=VARCHAR}, #{operPhone,jdbcType=VARCHAR}, #{cardNo,jdbcType=VARCHAR}, 
      #{provinceCode,jdbcType=VARCHAR}, #{provinceName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}, 
      #{cityName,jdbcType=VARCHAR}, #{areaCode,jdbcType=VARCHAR}, #{areaName,jdbcType=VARCHAR}, 
      #{addrDetail,jdbcType=VARCHAR}, #{foundedTime,jdbcType=VARCHAR}, #{contactRealname,jdbcType=VARCHAR}, 
      #{contactPhone,jdbcType=VARCHAR}, #{contactEmail,jdbcType=VARCHAR}, #{managerId,jdbcType=INTEGER}, 
      #{openAccountStatus,jdbcType=BIT}, #{status,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{modifyTime,jdbcType=TIMESTAMP})
  </insert>

  在service层,调用这个方法后,根据getId方法可以获取主键。但在web层,controller的方法里,却获取的是null。

原因:web层和service层是通过dubbo调用的,其对象不是同一个,故获取不到主键。

解决方法:在service层返回主键,web层直接获取主键,而不是通过对象的getId方法获取。

原文地址:https://www.cnblogs.com/esther-qing/p/10100867.html