mybatis 插入数据

mysql

反显主键 id

xml 配置文件
接口定义
     /**
      * 新增序列号信息
      * */
    int addSerialNumber(SerialNumberEx serialNumberEx);
mapper 定义
    <insert id="addSerialNumber" parameterType="com.jsh.erp.datasource.entities.SerialNumberEx"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
       insert into jsh_serial_number
        (material_Id, serial_Number, is_Sell, remark,delete_Flag,
        create_Time, creator,update_Time, updater,depothead_Id)
        values
        (#{materialId},#{serialNumber},#{isSell},#{remark},#{deleteFlag},
        #{createTime},#{creator},#{updateTime},#{updater},#{depotheadId}
        )
    </insert>
去 xml
@Insert("insert into devops_rules (std_type,std_mode,rule_type,catalog_id,rules_name,rules_agency,update_time,create_time) " +  
 "values (#{stdType},#{stdMode},#{ruleType},#{catalogId},#{rulesName},#{rulesAgency},#{updateTime},#{createTime})")  
@SelectKey(statement="select last_insert_id()",keyProperty="id", resultType=int.class, before=false)  
int addStandardRule(Rules roles);  

批量插入

xml 配置文件
接口定义

int saveByFormalImsiList(@Param("formalImsiList")List<FormalImsi> formalImsiList);

mapper 定义
    <insert id="saveByFormalImsiList" parameterType="list">
        INSERT INTO formal_imsi(formal_imsi, user_id,
        create_time, use_time, use_by_imsi)
        VALUES
        <foreach collection="formalImsiList" item="item" separator=",">
            (
            #{item.formalImsi}, #{item.userId},
            #{item.createTime}, #{item.useTime}, #{item.useByImsi}
            )
        </foreach>
    </insert>
去 xml
@Insert({"<script>",  
 "insert into devops_rules_detail (detail_name,detail_code,rules_id,std_id,modify_user,update_time,create_time) values " ,  
 "<foreach collection='list' item='each' separator=','>" ,  
 "(#{each.detailName},#{each.detailCode},#{each.rulesId},#{each.stdId},#{each.modifyUser},#{each.updateTime},#{each.createTime})",  
 "</foreach>" ,  
 "</script>"})  
int addStandardRuleDetailFromList(@Param("list") List<RulesDetail> list);  

sqlserver

接口定义

int insert(SghnRemoteTeachReply record);

mapper 定义

  <insert id="insert" parameterType="com.sinldo.po.share.SghnRemoteTeachReply" >
    <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
      SELECT IDENT_CURRENT('sghn_remote_teach_reply') AS id
    </selectKey>
    insert into sghn_remote_teach_reply ( commentId, replyVoip,
      reply, beReplyedVoip, beReplyedId, 
      replyTime, operation, createTime, 
      updateTime)
    values ( #{commentId,jdbcType=INTEGER}, #{replyVoip,jdbcType=VARCHAR},
      #{reply,jdbcType=VARCHAR}, #{beReplyedVoip,jdbcType=VARCHAR}, #{beReplyedId,jdbcType=INTEGER}, 
      #{replyTime,jdbcType=TIMESTAMP}, #{operation,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>

oracle

sequence 定义

create sequence SEQ_USER_ID
start with 1 
increment by 1 
minvalue 1 
nomaxvalue
nocycle

mapper 定义

<insert id="createUser" parameterType="pojo.User">
	<selectKey keyProperty="userId" resultType="int" order="BEFORE">  
    	select SEQ_USER_ID.NEXTVAL from dual  
	</selectKey>
    insert into "USER" (USER_ID, USER_NAME, PASSWORD)
    values (#{userId,jdbcType=DECIMAL}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  </insert>

返回 uuid

<insert id="aa">
   <selectKey resultType="java.lang.String" keyProperty="user.id" order="BEFORE">
      SELECT sys_guid() FROM DUAL </selectKey>
   insert into user(id,name) values(#{user.id},#{user.name}) 
</insert>