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. angular.js - angularjs的自定义过滤器如何给文字加颜色?2. debian - docker依赖的aufs-tools源码哪里可以找到啊?3. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””4. boot2docker无法启动5. docker容器呢SSH为什么连不通呢?6. angular.js - angular内容过长展开收起效果7. golang - 用IDE看docker源码时的小问题8. macos - mac下docker如何设置代理9. docker api 开发的端口怎么获取?10. mac里的docker如何命令行开启呢?

网公网安备