博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot中mybaits自动返回新增数据的主键
阅读量:4287 次
发布时间:2019-05-27

本文共 1468 字,大约阅读时间需要 4 分钟。

一 搭建项目工程

2.mapper

insert into tb_orders(order_id,order_name) values(#{orderId},#{orderName})

3. service层

package com.ljf.spring.boot.demo.service.impl;import com.ljf.spring.boot.demo.dao.OrderDao;import com.ljf.spring.boot.demo.model.Order;import com.ljf.spring.boot.demo.service.OrderService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * @ClassName: OrderServiceImpl * @Description: TODO * @Author: liujianfu * @Date: 2021/06/15 21:08:35  * @Version: V1.0 **/@Servicepublic class OrderServiceImpl implements OrderService {    @Autowired    private OrderDao orderDao;    @Override    public String addOrder() {        Order order=new Order();       // order.setOrderId();        order.setOrderName("苹果");        int k=orderDao.addOrder(order);        String r="影响的行数:"+k+" 返回的主键id值:"+order.getOrderId();        System.out.println("影响的行数:"+k+" 返回的主键id值:"+order.getOrderId());        return r;    }}

二 操作案例

总结:可以看到不填的刷新,添加之后,返回的主键id值为0

 

三 设置返回主键

 

3.1 设置

 

 

3.2 访问结果

 

 总结:

useGeneratedKeys="true" 表示给主键设置自增长keyProperty="orderId"  表示将自增长后的Id赋值给实体类中的orderId字段。parameterType="com.ljf.spring.boot.demo.model.Order" 这个属性指向传递的参数实体类这里提醒下,
中没有resultType属性,不要乱加。实体类中orderId 要有getter() and setter()方法Mybatis会自动将返回的主键设置到mapper接口传入的参数对象中去就是useGeneratedKeys和keyProperty要一块使用,如果只在insert的statement中使用keyProperty,那么传入的实体类中的orderId属性仍然为null

主键被设置到了参数对象中了,而insert方法的返回值是修改的行数

 

四 mysql-baits-plus

转载地址:http://xatgi.baihongyu.com/

你可能感兴趣的文章
论文笔记| BART:Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation
查看>>
【论文笔记】 | Learning to Retrieve Reasoning Paths over Wikipedia Graph for Question Answering
查看>>
论文笔记 | Adversarial Examples for Evaluating Reading Comprehension Systems
查看>>
2021-06-12
查看>>
论文笔记| The Emergence, Advancement and Future of Textual Answer Triggering
查看>>
论文笔记|Open Set Text Classification using Convolutional Neural Networks
查看>>
论文笔记: Hierarchical Chinese Legal event extraction via Pedal Attention Mechanism
查看>>
论文笔记 | Enhancing Pre-Trained Language Representations with Rich Knowledge for MRC
查看>>
论文笔记 | Text Summarization with Pretrained Encoders
查看>>
论文笔记:Document-level Event Extraction via Heterogeneous Graph-based Interaction Model with a Tracker
查看>>
论文笔记丨Inductive Unsupervised Domain Adaptation for Few-Shot Classification via Clustering
查看>>
论文笔记|GSum: A General Framework for Guided Neural Abstractive Summarization
查看>>
论文笔记 | Does Structure Matter? Encoding Documents for Machine Reading Comprehension
查看>>
论文笔记|Self-Supervised Test-Time Learning for Reading Comprehension
查看>>
论文笔记|Open-world Learning and Application to Product Classification
查看>>
论文笔记 _ ELECTRA_ Pre-training Text Encoders as Discriminators Rather than Generators
查看>>
【论文笔记】
查看>>
论文笔记:Exploring Pre-trained Language Models for Event Extraction and Generation
查看>>
论文解读 | QANET: COMBINING LOCAL CONVOLUTION WITH GLOBAL SELF-ATTENTION FOR READING COMPREHENSION
查看>>
论文笔记|Get To The Point: Summarization with Pointer-Generator Networks
查看>>