mybatis获取insert数据的主键
//假定params是接收到的Map<String,Object>的参数集合 mapper.saveTest(users); params.get("testId")即可拿到新增的主键id
<insert id="saveTest" parameterType="map" keyProperty="testId" useGeneratedKeys="true"> insert into Test(name) values(#{name}) </insert>
parameterType也可以用实体类对象
public static class Test{ private int testId; private String name; getter和setter }
//假定params是接收到的Map<String,Object>的参数集合 Test user=new Test(); BeanUtils.copyProperties(user, params);//将属性复制到类对象中 mapper.saveTest(users); user.getTestId()即可拿到新增的主键id
本文来自博客园,作者:每天都要学一点,欢迎讨论和转载,转载请注明原文链接:https://www.cnblogs.com/yanan7890/p/12909677.html