mybatis 插入資料返回主鍵id寫法
1、batis檔案中寫法:
<!--插入資料,返回主鍵的insert寫法 -->
<insert id="insert_A" parameterType="Map" useGeneratedKeys="true" keyProperty="id">
insert into project_A
(pid,sid,row,nm,create_date,create_uid,create_uname,mod_date,mod_uid,mod_uname)
values
(#{pid},#{sid},#{row},#{nm},#{create_date},#{create_uid},#{create_uname},#{mod_date},#{mod_uid},#{mod_uname})
</insert>
<!--插入多欄位,不許返回主鍵的寫法 -->
<insert id="insert_B" parameterType="Map">
insert into project_B
<foreach collection="params.keys" item="key" open="(" close=")" separator=",">
${key}
</foreach>
values
<foreach collection="params.keys" item="key" open="(" close=")" separator=",">
#{params[${key}]}
</foreach>
</insert>
2、dao檔案中寫法:
Integer insert_B(@Param("params") Map param);
3、獲得主鍵id:
param.put("is_hot", b010002Bean.getIs_hot());
b010002Service.insert_A(param);
String x = param.get("id").toString();
System.out.println("返回的id:"+x);