1.xml文件編寫(xiě)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類(lèi)
/**
* 添加一條數(shù)據(jù)
* @param car
* @return
*/
int addCarOne(@Param("car") Car car);
3.測(cè)試類(lèi)
@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.打印的結(jié)果
addCarOne 1
getId 30