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. docker容器呢SSH为什么连不通呢?2. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””3. nignx - docker内nginx 80端口被占用4. debian - docker依赖的aufs-tools源码哪里可以找到啊?5. 前端 - ng-view不能加载进模板6. android clickablespan获取选中内容7. docker网络端口映射,没有方便点的操作方法么?8. python - from ..xxxx import xxxx到底是什么意思呢?9. javascript - iframe 为什么加载网页的时候滚动条这样显示?10. 请教各位大佬,浏览器点 提交实例为什么没有反应
