java - mybatis如何实现获取新增得id

查看:128
本文介绍了java - mybatis如何实现获取新增得id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<insert id="saveCustomer" 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.数据库id必须是auto_increment
2.配置useGeneratedKeys="true"以及keyProoerty
3.你调用mapper接口的方法得到的数值,也就是总拿到的1是影响的记录数,要想拿到对象的id,请点用对应的getter方法

这篇关于java - mybatis如何实现获取新增得id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆