MyBatis-Oracle-selectKey返回主键

2021-02-17 17:20:55 浏览数 (1)

SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式。

Mapper(Oracle)

代码语言:javascript复制
<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**

0 人点赞