mybatis insert 插入语句返回主键

虾米姐 阅读:665 2021-03-31 21:42:17 评论:0

数据库:MySQL 5.0 以上版本

建表脚本:

DROP TABLE IF EXISTS `sms_record`; 
CREATE TABLE `sms_record` ( 
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID', 
  `message_id` varchar(255) DEFAULT NULL COMMENT '消息编号', 
  `push_type` varchar(255) DEFAULT NULL COMMENT '推送类型', 
  `telephone` varchar(255) DEFAULT NULL COMMENT '电话号码', 
  `jim` varchar(255) DEFAULT NULL COMMENT '极光推送账号', 
  `entrance_type` int(11) DEFAULT NULL COMMENT '0:用户  1:保安   3:其他', 
  `identification` int(11) DEFAULT '0' COMMENT '是否发送成功(0:未发送成功,1:发送成功)', 
  `create_time` datetime DEFAULT NULL COMMENT '创建日期', 
  `up_time` datetime DEFAULT NULL COMMENT '修改日期', 
  `app_type` varchar(255) DEFAULT NULL COMMENT 'app类型', 
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=239 DEFAULT CHARSET=utf8;


mybatis.xml 文件配置,以下两种方式都可以。

方式一:

 <insert id="insert" parameterType="com.common.common.entity.SmsRecord" useGeneratedKeys="true" keyProperty="id" > 
    insert into wlsq_data.sms_record (id, message_id, push_type,  
      telephone, jim, entrance_type,  
      identification, create_time, up_time,  
      app_type) 
    values (#{id,jdbcType=INTEGER}, #{messageId,jdbcType=VARCHAR}, #{pushType,jdbcType=VARCHAR},  
      #{telephone,jdbcType=VARCHAR}, #{jim,jdbcType=VARCHAR}, #{entranceType,jdbcType=INTEGER},  
      #{identification,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{upTime,jdbcType=TIMESTAMP},  
      #{appType,jdbcType=VARCHAR}) 
  </insert>

方式二:

  <insert id="insert" parameterType="com.common.common.entity.SmsRecord"> 
   	<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" > 
    select @@IDENTITY 
  	</selectKey> 
    insert into wlsq_data.sms_record (id, message_id, push_type,  
      telephone, jim, entrance_type,  
      identification, create_time, up_time,  
      app_type) 
    values (#{id,jdbcType=INTEGER}, #{messageId,jdbcType=VARCHAR}, #{pushType,jdbcType=VARCHAR},  
      #{telephone,jdbcType=VARCHAR}, #{jim,jdbcType=VARCHAR}, #{entranceType,jdbcType=INTEGER},  
      #{identification,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{upTime,jdbcType=TIMESTAMP},  
      #{appType,jdbcType=VARCHAR}) 
  </insert>


我选用方式一,来实现mybatis insert插入语句返回主键ID。

业务代码:

//添加过滤极光推送功能 
						SmsRecord  androidRecord = new SmsRecord(); 
						androidRecord.setMessageId(message_id); 
						androidRecord.setPushType("push"); 
						androidRecord.setJim(openid); 
						androidRecord.setEntranceType(1); 
						 
						List<SmsRecord> androidList = smsRecordService.selectByObject(androidRecord); 
						if(androidList !=null && androidList.size()>0){ 
							System.out.println("用户指定极光推送id("+openid+")已经发送过"); 
						}else{ 
							androidRecord.setIdentification(0); 
							androidRecord.setCreateTime(new Date()); 
							androidRecord.setAppType("gridAndroid"); 
							int num = smsRecordService.insert(androidRecord); 
							if(num > 0){ 
								gridAndroidPushService.AndroidSendPush("网格报警",alarmDataObj.getAlarm_content(),extras,openid,1,androidRecord.getId()); 
							} 
						}



标签:MyBatis
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号