1. 程式人生 > >mybatis 新增資料後返回主鍵的兩種寫法

mybatis 新增資料後返回主鍵的兩種寫法

第一種:加上  useGeneratedKeys="true" keyProperty="id"

<insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="id" parameterType="com.chenzhou.mybatis.User">
    insert into user(userName,password,comment)
    values(#{userName},#{password},#{comment})
</insert>

第二種: 加上 selectKey 標籤裡的語句

    <
insert id="insertProduct" parameterType="domain.model.ProductBean" > <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO t_product(productName,productDesrcible,merchantId)values(#{productName},#{productDesrcible},#{merchantId});
</insert>