话不多说,直接说方法。
1.在insert语句中加入如下的代码。
<insert id="insertSelective" parameterType="com.qgranite.entity.TCategory">
insert into t_category
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="categoryId != null">
category_id,
</if>
<if test="categoryName != null">
category_name,
</if>
<if test="categoryRemark != null">
category_remark,
</if>
<if test="categoryType != null">
category_type,
</if>
<if test="isRoot != null">
is_root,
</if>
<if test="categoryLevel != null">
category_level,
</if>
<if test="rootCategoryId != null">
root_category_id,
</if>
<if test="parentCategoryId != null">
parent_category_id,
</if>
<if test="parentCategoryName != null">
parent_category_name,
</if>
<if test="sortNum != null">
sort_num,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
<if test="isDel != null">
is_del,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="categoryId != null">
#{categoryId,jdbcType=INTEGER},
</if>
<if test="categoryName != null">
#{categoryName,jdbcType=VARCHAR},
</if>
<if test="categoryRemark != null">
#{categoryRemark,jdbcType=VARCHAR},
</if>
<if test="categoryType != null">
#{categoryType,jdbcType=INTEGER},
</if>
<if test="isRoot != null">
#{isRoot,jdbcType=INTEGER},
</if>
<if test="categoryLevel != null">
#{categoryLevel,jdbcType=INTEGER},
</if>
<if test="rootCategoryId != null">
#{rootCategoryId,jdbcType=INTEGER},
</if>
<if test="parentCategoryId != null">
#{parentCategoryId,jdbcType=INTEGER},
</if>
<if test="parentCategoryName != null">
#{parentCategoryName,jdbcType=VARCHAR},
</if>
<if test="sortNum != null">
#{sortNum,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="isDel != null">
#{isDel,jdbcType=INTEGER},
</if>
</trim>
<selectKey resultType="java.lang.Integer" order="AFTER"
keyProperty="categoryId">
SELECT @@IDENTITY AS categoryID
</selectKey>
</insert>
最底部的那段代码
<selectKey resultType="java.lang.Integer" order="AFTER"
keyProperty="categoryId">
SELECT @@IDENTITY AS categoryID
</selectKey>
其中categoryId是实体中的自增的主键。
2.在代码中直接通过“实体.主键”的方式调用。
baseDao.addT("TCategoryMapper.insertSelective",
category);//存储
System.out.println("主键:"+category.getCategoryId());//获取主键