方法:1.sql中加入:
<insert id="insertSelective" parameterType="domain.XwRole" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
insert into t_xw_role
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="code != null" >
code,
</if>
<if test="level != null" >
level,
</if>
<if test="parent != null" >
parent,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="code != null" >
#{code,jdbcType=VARCHAR},
</if>
<if test="level != null" >
#{level,jdbcType=INTEGER},
</if>
<if test="parent != null" >
#{parent,jdbcType=VARCHAR},
</if>
</trim>
</insert>
2.在service 层接收返回的主键
public int add(XwRoleVO xwRoleVo) {
xwRoleVo.setCode(getCode(xwRoleVo));
xwRoleM.insertSelective(xwRoleVo);
return xwRoleVo.getId();
}
方法2 通过注解方式实现
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("INSERT INTO t_all_order (order_no, resident_id, total_amount, deduct_amount, "
+ "payable_amount, address_book_id, pay_status, deliver_status, "
+ "able_status,create_time,total_count,hang_msg,cancel_msg) "
+ "VALUES (#{orderNo}, #{residentId}, #{totalAmount}, #{deductAmount}, #{payableAmount}, "
+ "#{addressBookId}, #{payStatus}, #{deliverStatus}, #{ableStatus},"
+ "#{createTime},#{totalCount},#{hangMsg},#{cancelMsg});")
public int insert(AllOrder allOrder);
在service 层接收返回的主键
service 层也是绑定到插入的实体类上,同第一种方法里面的获取一样