java - mybatis如何實(shí)現(xiàn)獲取新增得id
問(wèn)題描述
<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);這個(gè)方法返回得一直是1。 對(duì)象.id得值 一直是空。數(shù)據(jù)庫(kù)是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());
問(wèn)題解答
回答1:@浮生百記 在其基礎(chǔ)上加上useGeneratedKeys='true'
回答2:這個(gè)方法返回的實(shí)際是影響的記錄數(shù)。你insert之后直接去取實(shí)體類的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:
實(shí)體類可以看下么
回答4:useGeneratedKeys='true' keyProperty='id' xml配置中keyProperty為主鍵 你看你的數(shù)據(jù)數(shù)是不是設(shè)id為主鍵并設(shè)置期為自增,如果設(shè)置執(zhí)行完insert后,主鍵的值就會(huì)反射到你實(shí)體類的主鍵中
回答5:<insert parameterType='atyy.model.ArticleCategoryPO' useGeneratedKeys='true'></insert>加入一個(gè)屬性就行了useGeneratedKeys='true'
回答6:1.數(shù)據(jù)庫(kù)id必須是auto_increment2.配置useGeneratedKeys='true'以及keyProoerty3.你調(diào)用mapper接口的方法得到的數(shù)值,也就是總拿到的1是影響的記錄數(shù),要想拿到對(duì)象的id,請(qǐng)點(diǎn)用對(duì)應(yīng)的getter方法
相關(guān)文章:
1. css - 對(duì)于類選擇器使用的問(wèn)題2. angular.js - angular ng-class里面的引號(hào)問(wèn)題3. python - 為什么在main()函數(shù)里result 會(huì)變成那樣?4. javascript - Web微信聊天輸入框解決方案5. docker - 如何修改運(yùn)行中容器的配置6. javascript - 有沒(méi)有什么好的圖片懶加載的插件,需要包含監(jiān)聽頁(yè)面滾動(dòng)高度,然后再加載的功能7. 網(wǎng)頁(yè)爬蟲 - 用Python3的requests庫(kù)模擬登陸B(tài)ilibili總是提示驗(yàn)證碼錯(cuò)誤怎么辦?8. mysql無(wú)法添加外鍵9. javascript - es6將類數(shù)組轉(zhuǎn)化成數(shù)組的問(wèn)題10. html - vue項(xiàng)目中用到了elementUI問(wèn)題
