Mybatisplus插入后返回元素id的问题怎么解决

这篇文章主要讲解了“Mybatisplus插入后返回元素id的问题怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Mybatisplus插入后返回元素id的问题怎么解决”吧!

    mybatis-plus插入后返回插入元素的id

    有三种方法,第三种最简单。

    不想麻烦的直接看第三种

    1.mybatis原生

    mybaits-plus要使用mybatis原生需要一下配置,指定下mapper文件的位置就好

    mybatis-plus:
      mapper-locations: classpath*:mapperxml/*Mapper.xml

    直接先看mapper.xml文件,这个insert语句实际上就是插入MouldMessage这个我定义的实体类。

    <mapper namespace="com.hwz.MessageMouldMapper">
        <insert id="testInsert" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO t_XXXX
            XXXXXX,XXXX,XXXXX
            VALUES XXXX,XXXX,XXXX
        </insert>
    </mapper>

    userGenerateKeys告诉mybatis使用自增主键,keyProperty指定这个主键名称叫id。

    然后再mapper接口定义这个方法

    Long testInsert(MessageMould messageMould);

    调用这个插入语句,information这个实例时没有定义id,创建时间这些字段的,输出结果是数据表修改条数,这里插入一条,所以返回1。

    System.out.println(messageMouldMapper.testInsert(information)+“----”);

    然后这时候我们输出下information的一些属性,发现本来作为参数的information被mybatis自动填充上了id和创建时间

    System.out.println(information.getId()+“*******”+information.getCreateTime());

    所以结论就是,插入之后找传入的参数,就能找到新增加元素的id

    2.使用mybatis-plus注解

    其实跟原生mybatis一样,插入后元素的id会直接映射到参数中,只不过用注解代替了mapper.xml文件

    @Insert(value = "INSERT INTO t_XXXX" +
            "XXX,XXX,XXX " +
            "VALUES (XXX,XXX,XXX)")
    @SelectKey(statement="select LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Long.class)
    Integer testInsert1(MessageMould messageMould);

    执行完这条insert操作后,直接拿形参messageMould的id,就能拿到id

    3.使用mybatis-plus提供的insert

    mybatis只要extends BaseMapper就可以调用他的insert方法。其实也就跟上面2个一样。i调用insert(MessageMould messageMould)后,id会映射到形参messageMould中,直接拿形参messageMould的id,就能拿到id

    Mybatis-plus设置id自增,插入数据

    没修改前

    这是我的实体类。

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class Company {
      private Integer id;
      private String cid;
      private String cname;
      private String address;
      private String representation;
      private String phone;
      private String email;
      private String weburl;
      private String introductory;
    }

    我的数据库设置的是id自增。 添加数据时没有加上id的数据。

    然后报错

    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@59238b99]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87] was not registered for synchronization because synchronization is not active
    Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3cc8aa87]
    2021-11-07 14:28:06.789 ERROR 5620 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.dk.pojo.Company' with value '1457233381002637313' Cause: java.lang.IllegalArgumentException: argument type mismatch] with root cause

    查询得知,当时实体中,plus主键生成方式不设置生成方式时,默认的是自增。而且生成的值就如报错中的&lsquo;1457233381002637313&rsquo;很长的一个值。

    而主键的设置类型有:

    AUTO(0, “数据库ID自增”),
    INPUT(1, “用户输入ID”),
    ID_WORKER(2, “全局唯一ID”),
    UUID(3, “全局唯一ID”),
    NONE(4, “该类型为未设置主键类型”),
    ID_WORKER_STR(5, “字符串全局唯一ID”);

    所以修改后

    第一次:

    @TableId( type = IdType.AUTO)
    private Integer id;

    这样的修改跟修改前一样。我们需要的是12、13这样的序列,所以需要设置id生成方式,就需要在注解设置value的值。

    @TableId(value = “id”, type = IdType.AUTO)
    private Integer id;

    这样指定id的值,我们在用plus插件insert时就不用插入id的值。生成的id值跟数据库对应。

    感谢各位的阅读,以上就是“Mybatisplus插入后返回元素id的问题怎么解决”的内容了,经过本文的学习后,相信大家对Mybatisplus插入后返回元素id的问题怎么解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是蜗牛博客,小编将为大家推送更多相关知识点的文章,欢迎关注!

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    评论

    有免费节点资源,我们会通知你!加入纸飞机订阅群

    ×
    天气预报查看日历分享网页手机扫码留言评论电报频道链接