1、xml方式:
@GeneratedValue(strategy = GenerationType.IDENTITY) 主键自增
<insert id="insertAuthor">
<bind name="username_bind" value='@java.util.UUID@randomUUID().toString().replace("-", "")' />
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username_bind}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
@GeneratedValue(generator = "UUID") UUID
<insert id="insertAuthor">
<bind name="username_bind" value='@java.util.UUID@randomUUID().toString().replace("-", "")' />
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username_bind}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
2、注解方式:
加入这连个属性每次做完插入后就会返回主键。
<!-- useGeneratedKeys="true", keyProperty="id"把新增加的主键赋值到自己定义的keyProperty(id)中 -->
<insert id="insert" parameterType="jw.base.entity.WrongRecApply" useGeneratedKeys="true" keyProperty="id" >
使用注解的方式为:
@Insert("insert into ycb_mcs_repair_log (title,openid,content,type,platform,station_id,operator_id,create_time) " + "VALUES(#{title},#{openid},#{content},#{type},#{platform},#{station_id},#{operator_id},Now())") @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") int insert(RepairLog repairLog);