最近做工程时遇到这种情况,刚向数据库插入一条记录,马上需要得到该记录的主键
网上搜索得出如下解决办法
insert into t_user(user_name,password) values(#{name},#{pswd})
实现层代码如下
//先添加宠物信息 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表中主键名称保持一致。