mybatis 插入返回自增后的id】的更多相关文章

//serviceImpl int customerId = customerDao.insertDynamic(customer1); System.out.println("id===================="+customer1.getId()); useGeneratedKeys:开启获取主键 keyProperty:那个字段是主键 <insert id="insertDynamic" parameterType="com.wftd…
1.场景介绍: ​ 开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后返回的是插入成功的条数,那么这个时候我们想要得到相应的这条新增数据的ID,该怎么办呢? 2.插入数据返回自增主键ID方法(一) 在映射器中配置获取记录主键值 xml映射: 在xml中定义useGeneratedKeys为true,返回主键id的值,keyProperty和keyColumn分别代表数…
公司决定新项目用mybatis,虽然这个以前学过但是一直没用过都忘得差不多了,而且项目比较紧,也没时间去系统点的学一学,只好很粗略的百度达到能用的程度就行了. 其中涉及到插入实体要求返回主键id的问题,以前用ssh配合oracle很容易实现,因为有sequence,而且mysql也基本没用过,所以只好去百度了. 这个内容还是比较好查的,如下: <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapp…
我们都知道Mybatis在插入单条数据的时候有两种方式返回自增主键: 1.对于支持生成自增主键的数据库:useGenerateKeys和keyProperty. 2.不支持生成自增主键的数据库:<selectKey>. 但是怎对批量插入数据返回自增主键的解决方式网上看到的还是比较少,至少百度的结果比较少. Mybatis官网资料提供如下: First, if your database supports auto-generated key fields (e.g. MySQL and SQL…
<insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole" useGeneratedKeys="true" keyProperty="id"> insert into app_cms_role <trim prefix="(" suffix=")" suffixOve…
mybaits两种获取自增主键ID的方法:一种是使用useGeneratedKeys,第二种是selectKey方法获取. useGeneratedKeys <insert id="insert" parameterType="com.github.chengbin.auth.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into sys…
一.场景: 插入数据库的值需要立即得到返回的主键id进行下一步程序操作 二.解决方法: 第一种:使用通用mapper的插入方法 Mapper.insertSelective(record): 此方法:插入一条数据,只插入不为null的字段,不会影响有默认值的字段支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)优先使用传入的参数值,参数值空时,才会使用序列.UUID,自动增长 controller的实际应用:使用方法id会直接将映射到参数的实体上使用时直接使用…
mapper.xml 代码 <insert id="insert" parameterType="com.Student" > <selectKey keyProperty="id" resultType="long" order="AFTER"> select last_insert_id(); </selectKey> insert into student(id,,…
话不多说,直接说方法. 1.在insert语句中加入如下的代码. <insert id="insertSelective" parameterType="com.qgranite.entity.TCategory"> insert into t_category <trim prefix="(" suffix=")" suffixOverrides=","> <if test=…
<insert id="insertTable" parameterType="com.infohold.city.map.model.CheckTemplateManagement" useGeneratedKeys="true" keyProperty="id">…