<insert >

insert into query_rate_config (code,partner_type,search_count, booking_count, ticket_count,rate_type)

values (#{code,jdbcType=VARCHAR},#{partnerType,jdbcType=TINYINT}, #{searchCount,jdbcType=INTEGER},
    #{bookingCount,jdbcType=INTEGER}, #{ticketCount,jdbcType=INTEGER},#{rateType,jdbcType=TINYINT})
</insert>

  

首先我们应该保证数据库的主键Id是自增的,另外需要设置的两个属性为:

keyProperty="id"

useGeneratedKeys="true"

这样的话,我们在插入数据之后,就可以得到插入数据之后的对象,然后通过该对象获取该对象的id。

案例:

1、MyBatis的配置文件如上遍所示的一段代码;

2、使用的Java代码如下:

MyBatis插入数据之后返回插入记录的id
@Override
    public int insert(CountRateConfig countRateConfig) {
        int insertNum = Integer.parseInt(countRateConfigMapper.insert(countRateConfig) + "");
        Long id = countRateConfig.getId();
        return insertNum;
    }
MyBatis插入数据之后返回插入记录的id

3、上述代码,如果插入数据成功的话,则可以找到数据库中对应的key;

结果是正确的,即可以读取正确的id。

相关文章:

  • 2021-08-27
  • 2021-08-14
  • 2021-08-23
  • 2021-05-15
  • 2021-04-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2021-12-26
  • 2021-05-18
相关资源
相似解决方案