答案是不加东西当然不能的,返回值直接返回一个null
@Autowired
private PriceRuleMapper priceRuleMapper;
@Test
public void contextLoads() {
PriceRule priceRule=new PriceRule();
priceRule.setName("abc");
priceRuleMapper.insert(priceRule);
System.out.println(priceRule.getId());
}
}
但是,你在mapper层,在方法体加上:
@Options(useGeneratedKeys=true, keyProperty=“id”, keyColumn=“id”)
@Insert({
"insert into price_rule (id, name)",
"values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})"
})
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
int insert(PriceRule record);
就能得到新增的id了!