1. 程式人生 > >mybatis 註解方式插入,主鍵生成

mybatis 註解方式插入,主鍵生成

@Mapper
public interface UserMapper
{
    @Insert("insert into tbl_user (name, age) values (#{name}, #{age})")
    @Options(useGeneratedKeys=true, keyProperty="userId", keyColumn="id")
    void insertUser(User user);

 

 

    /**
     * keyProperty: 表示將select返回值設定到該屬性中
     * resultType: 返回型別
     * before: 是否在insert之前執行
     * statement: 自定義子查詢
     * @param userBase
     */
    @SelectKey(keyProperty = "userBase.id",resultType = String.class, before = true,
            statement = "select replace(uuid(), '-', '')")
    @Options(keyProperty = "userBase.id", useGeneratedKeys = true)
    @Insert("insert into user_base(id, " +
            "name, " +
            "passwd, " +
            "phone " +
            ") values (#{userBase.id}, " +
            "#{userBase.name}, " +
            "#{userBase.password}, " +
            "#{userBase.phone}" +
            ") "
    )
    public void insertForReg(@Param("userBase")UserBase userBase);