https://www.cnblo在 mapper.xml 中對於的數據庫的表中,由於表設計的是主鍵自增,所以添加的時候不用給主鍵 id 賦值,數據庫會自動的給 id 賦值,而怎么拿到賦值后的 id 呢,需要在 mapper.xml 中的 insert 中加入:
useGeneratedKeys="true" keyProperty="id" //得到 id
如:
<insert id="insertSelective" parameterType="com.miaoshaproject.dataproject.UserDo" useGeneratedKeys="true" keyProperty="id">
Insert into … //SQL語句
</insert>
這種辦法,會對注入的 實體類,自動加入 id 的值。