一个对象进行添加之后能调用它的id吗?
作者:互联网
答案是不加东西当然不能的,返回值直接返回一个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了!
标签:PriceRule,调用,keyColumn,insert,priceRule,添加,keyProperty,id 来源: https://blog.csdn.net/qq_28241535/article/details/101361340