最近作工程时遇到这种状况,刚向数据库插入一条记录,立刻须要获得该记录的主键数据库
网上搜索得出以下解决办法app
<insert id="insert" parameterType="userInfo" useGeneratedKeys="true" keyProperty="id"> insert into t_user(user_name,password) values(#{name},#{pswd}) </insert>
实现层代码以下code
//先添加宠物信息 CustomerPets customerPets = new CustomerPets(); //customerPets设值 customerPets.setCustomerId(userId); customerPets.setPetName(petName); customerPets.setSex(sex); customerPets.setPetCategoryId(petCategoryId); customerPets.setPetTypeId(petTypeId); customerPets.setBirthday(brithday); customerPets.setWeight(weight); customerPets.setUpdPid(PID); customerPets.setUpdUid(userId); customerPets.setUseFlag(Constant.UseFlag.USE.getFlag()); customerPets.setAddUid(userId); customerPets.setAddPid(PID); int success = customerPetsMapper.insertSelective(customerPets); if (success == 1) { //注意这里取值 String tmpPetId = customerPets.getPetId(); }
备注:keyProperty后面的自增加主键,应该与t_user表中主键名称保持一致。get