1. 程式人生 > >MyBatis-Oracle-selectKey返回主鍵

MyBatis-Oracle-selectKey返回主鍵

插入類目記錄,並返回category_id,其中category_id是根據自定義規則生成的

<insert id="insertSelective2" parameterType="com.imooc.model.ProductCategory">
    insert into product_category(<include refid="Base_Save_List"/>)
    values (#{productCategory.categoryName,jdbcType=VARCHAR},#{productCategory.categoryType,jdbcType=INTEGER})

    <selectKey keyProperty='productCategory.categoryId' resultType='int' order='AFTER' >
        select MAX(category_id) from product_category
    </selectKey>
</insert>

使用方法

public Integer save(ProductCategory productCategory) {
    System.out.println(categoryMapper.insertSelective2(productCategory));
int categoryId = productCategory.getCategoryId;
    return categoryId;
}

上述程式碼實現了將MAX(category_id)存入'productCategory.categoryId,供大家參考

MyBatis-Oracle-selectKey返回主鍵

https://my.oschina.net/merryyou/blog/1358372