java - mybatis如何實現(xiàn)獲取新增得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得值 一直是空。數(shù)據(jù)庫是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:@浮生百記 在其基礎(chǔ)上加上useGeneratedKeys='true'
回答2:這個方法返回的實際是影響的記錄數(shù)。你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為主鍵 你看你的數(shù)據(jù)數(shù)是不是設(shè)id為主鍵并設(shè)置期為自增,如果設(shè)置執(zhí)行完insert后,主鍵的值就會反射到你實體類的主鍵中
回答5:<insert parameterType='atyy.model.ArticleCategoryPO' useGeneratedKeys='true'></insert>加入一個屬性就行了useGeneratedKeys='true'
回答6:1.數(shù)據(jù)庫id必須是auto_increment2.配置useGeneratedKeys='true'以及keyProoerty3.你調(diào)用mapper接口的方法得到的數(shù)值,也就是總拿到的1是影響的記錄數(shù),要想拿到對象的id,請點用對應(yīng)的getter方法
相關(guān)文章:
1. docker-compose中volumes的問題2. mysql 一個sql 返回多個總數(shù)3. CSS3 畫如下圖形4. 在mac下出現(xiàn)了兩個docker環(huán)境5. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器6. angular.js - ionic2 瀏覽器跨域問題7. javascript - 螞蟻金服里的react Modal方法,是怎么把元素插入到頁面最后的8. android - rxjava merge 返回Object對象數(shù)據(jù)如何緩存9. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????10. python - Scrapy存在內(nèi)存泄漏的問題。
