本文共 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/