1.xml文件编写useGeneratedKeys和keyProperty
<!-- 插入一条信息 -->
<insert id="addCarOne" useGeneratedKeys="true" keyProperty="id">
INSERT INTO car
VALUES
(
NULL,
#{car.carNumber},
#{car.driver},
#{car.phone},
#{car.color},
);
</insert>
2.mapper.java类
/**
* 添加一条数据
* @param car
* @return
*/
int addCarOne(@Param("car") Car car);
3.测试类
@Test
public void Text01() {
Car car=new Car();
car.setCarNumber("广202233");
car.setDriver("张珊");
car.setPhone("111");
int addCarOne = carmapper.addCarOne(car);
System.out.println("addCarOne "+addCarOne);
System.out.println("getId "+car.getId());
}
4.打印的结果
addCarOne 1
getId 30