java - mybatis如何实现获取新增得id
问题描述
<insert parameterType='com.xiaonatech.dsx.entity.CustomerEntity' useGeneratedKeys='true' keyProperty='policyID'>
insert into customer (certType,code,password,name,mobile,effDate,expDate,address,createID,createTime,updateID,updateTime) values (#{certType},#{code}, #{password}, #{name}, #{mobile}, #{effDate},#{expDate},#{address},#{createID},#{createTime} ,#{updateID},#{updateTime})</insert>
dao层public int saveCustomer(CustomerEntity cs);这个方法返回得一直是1。 对象.id得值 一直是空。数据库是mysql CustomerEntity applyRecord = new CustomerEntity();
applyRecord.setCertType('0'); applyRecord.setCode('423565462256'); applyRecord.setPassword('123456'); applyRecord.setName('sds'); applyRecord.setMobile('12345678978'); applyRecord.setCreateID('150'); applyRecord.setUpdateID('150'); applyRecord.setUpdateTime(new Date()); int i = dao.saveCustomer(cs); System.out.println('i========='+i+' id================'+applyRecord.getCarOwnerID());
问题解答
回答1:@浮生百记 在其基础上加上useGeneratedKeys='true'
回答2:这个方法返回的实际是影响的记录数。你insert之后直接去取实体类的id即可。
ApplyRecord applyRecord = new ApplyRecord();applyRecord.setAccount('1234');applyRecord.setCode('123');Timestamp now = new Timestamp(System.currentTimeMillis());applyRecord.setGmtCreate(now);applyRecord.setGmtModified(now);int i = applyRecordDao.insert(applyRecord);logger.info('{}',applyRecord.getId());回答3:
实体类可以看下么
回答4:useGeneratedKeys='true' keyProperty='id' xml配置中keyProperty为主键 你看你的数据数是不是设id为主键并设置期为自增,如果设置执行完insert后,主键的值就会反射到你实体类的主键中
回答5:<insert parameterType='atyy.model.ArticleCategoryPO' useGeneratedKeys='true'></insert>加入一个属性就行了useGeneratedKeys='true'
回答6:1.数据库id必须是auto_increment2.配置useGeneratedKeys='true'以及keyProoerty3.你调用mapper接口的方法得到的数值,也就是总拿到的1是影响的记录数,要想拿到对象的id,请点用对应的getter方法
相关文章:
1. android - 请问一下 类似QQ音乐底部播放 在每个页面都显示 是怎么做的?2. thinkPHP5中获取数据库数据后默认选中下拉框的值,传递到后台消失不见。有图有代码,希望有人帮忙3. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?4. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?5. python小白 关于类里面的方法获取变量失败的问题6. python - vscode 如何在控制台输入7. django - Python error: [Errno 99] Cannot assign requested address8. python小白,关于函数问题9. Python2中code.co_kwonlyargcount的等效写法10. [python2]local variable referenced before assignment问题
