MyBatis 执行Insert操作,返回实体对象的主键
1、dao 层定义和Mapper 实现
dao层定义:
int insertSelective(RoleInfo record);
Mapper实现:
<insert id="insertSelective" keyProperty="sid" parameterType="com.digipower.erms.domain.RoleInfo">
<selectKey keyProperty="sid" order="BEFORE" resultType="java.lang.Long">
SELECT erms_s_auth_role.nextval FROM DUAL
</selectKey>
insert into ERMS_AUTH_ROLE
<trim prefix="(" suffix=")" suffixOverrides=",">
SID,
<if test="roleName != null">
ROLE_NAME,
</if>
<if test="roleCode != null">
ROLE_CODE,
</if>
<if test="roleDescription != null">
ROLE_DESCRIPTION,
</if>
<if test="state != null">
STATE,
</if>
<if test="createdBy != null">
CREATED_BY,
</if>
CREATED_DT,
<if test="version != null">
VERSION,
</if>
<if test="updatedBy != null">
UPDATED_BY,
</if>
<if test="updatedDt != null">
UPDATED_DT,
</if>
<if test="zoneOrgCode != null">
ZONE_ORG_CODE,
</if>
<if test="value1 != null">
VALUE1,
</if>
<if test="value2 != null">
VALUE2,
</if>
<if test="value3 != null">
VALUE3,
</if>
<if test="deleteFlag != null">
DELETE_FLAG,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{sid,jdbcType=DECIMAL},
<if test="roleName != null">
#{roleName,jdbcType=VARCHAR},
</if>
<if test="roleCode != null">
#{roleCode,jdbcType=VARCHAR},
</if>
<if test="roleDescription != null">
#{roleDescription,jdbcType=VARCHAR},
</if>
<if test="state != null">
#{state,jdbcType=VARCHAR},
</if>
<if test="createdBy != null">
#{createdBy,jdbcType=VARCHAR},
</if>
sysdate,
<if test="version != null">
#{version,jdbcType=VARCHAR},
</if>
<if test="updatedBy != null">
#{updatedBy,jdbcType=VARCHAR},
</if>
<if test="updatedDt != null">
#{updatedDt,jdbcType=DATE},
</if>
<if test="zoneOrgCode != null">
#{zoneOrgCode,jdbcType=VARCHAR},
</if>
<if test="value1 != null">
#{value1,jdbcType=VARCHAR},
</if>
<if test="value2 != null">
#{value2,jdbcType=VARCHAR},
</if>
<if test="value3 != null">
#{value3,jdbcType=VARCHAR},
</if>
<if test="deleteFlag != null">
#{deleteFlag,jdbcType=DECIMAL},
</if>
</trim>
</insert>
2、service层定义和service 实现
service层定义:
Long insert(E entity);
service实现:
@Override
public Long insert(RoleInfo entity) {
// TODO Auto-generated method stub
mapper.insertSelective(entity);
return entity.getSid();
}
说明:插入实体对象,返回实体对象主键,主要通过配置:keyProperty属性,并且指定返回字段名称。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。