oracle和mysql的主键生成策略有不同,但是最重要的selectKey 这个标签,其他只需要换select UUID()
<insert id="insertPerson" parameterType="com.beijita.dto.User">
<selectKey keyProperty="userId" order="BEFORE" resultType="java.lang.String">
select UUID()
</selectKey>
insert into user(userId,userName,birthday,sex,address) values (#{id},#{userName},#{birthday},#{sex},#{address})
</insert>
利用mysql的LAST_INSERT_ID()
方法获取插入记录的主键,select LAST_INSERT_ID()
可以在插入数据后,查询并返回刚插入数据的主键(但是单独执行这条语句只会返回0)。
例如oracle的
select SYS_GUID() as userId from DUAL
或者
select USER_ID_SEQ.NEXTVAL as userId from DUAL
3 mysql插入表时主键自动增长策略
mybatis配置文件
<insert id="insert" parameterType="com.entity.Entity" useGeneratedKeys="true" keyProperty="id" >
....
</insert>
数据库建表
CREATE TABLE `tt_charging_history` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`create_by` varchar(50) DEFAULT NULL COMMENT '创建人',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`update_by` datetime DEFAULT NULL COMMENT '修改人',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4 插入 更新时 mysql当前系统时间
<update id="updateById" parameterType="com.Entity">
update tableName
set order_no = #{orderNo,jdbcType=VARCHAR},
update_time = now(),
update_by = #{updateBy,jdbcType=VARCHAR},
where id = #{id,jdbcType=BIGINT}
</update>