java - mybatis如何實(shí)現(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);這個(gè)方法返回得一直是1。 對(duì)象.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:這個(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ù)庫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. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????2. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)3. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫4. mysql取模分表與分表5. gvim - 誰有vim里CSS的Indent文件, 能縮進(jìn)@media里面的6. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號(hào)?正則如何寫?7. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯(cuò)誤8. PHP類屬性聲明?9. objective-c - ios 怎么實(shí)現(xiàn)微信聯(lián)系列表 最好是swift10. javascript - 請(qǐng)教如何獲取百度貼吧新增的兩個(gè)加密參數(shù)
