SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式。
Mapper(Oracle)
<insert id="insertWxshLogOut" parameterType="java.util.HashMap">
<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
select wxsh_seq.nextval as id from dual
</selectKey>
INSERT INTO WXSH_LOG_OUT
(ID,
OUT_NO,
USER_ID,
FILE_NAME,
CREATE_AT,
IP,
CONTENT,
OUT_NUM,
UNIT_NO,
STATUS)
VALUES
(#{id},
#{out_no},
#{user_id},
#{file_name},
SYSDATE,
#{ip},
#{content},
#{out_num},
#{unit_no},
#{status})
</insert> ### Service
public int addWxshLogOut(Map map) {
try {
DataSourceTypeManager.set(DataSources.GJT);
welfareConfirmMapper.insertWxshLogOut(map);
return Integer.parseInt(String.valueOf(map.get("id")));
} catch (Exception e) {
logger.error(e.getMessage());
}
return 0;
} **map中会增加返回的主键id**