Mybatis註解

學習就完事兒。

@Result

使數據庫中表的字段和實體類的字段名相對應。

@Select({"select id, name, class_id from my_student"})
@Results
(id="studentMap", value=
//依次對應column="數據表字段",property="實體類字段",jdbcType="數據表字段的類型" id=true確定是主鍵
{@Result(column="id",property="id",jdbcType=JdbcType.INTEGER,id=true),   
 @Result(column="name", property="name",jdbcType=JdbcType.VARCHAR),   
 @Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)})
List selectAll();
```java
@Select({"select id, name, class_id from my_student where id = #{id}"})
//根據上面代碼塊裏的代碼可以確定 ResultMap的名字 可簡化重複操作
@ResultMap(value="studentMap")Student selectById(integer id);

@Param

註解單一屬性
用#{}的方式把@Param註解括號內的參數進行引用
注意:
當@param聲明的是參數的時候
使用@param 用#{}或者${}都可以

當@param不是聲明參數的時候
不使用@param 只能用#{}

 @Select("select column from table where userid = #{userid} ")
 public int selectColumn(@Param("userid") int userid);

不使用@Param註解
只有一個參數,而且還是javabean
在SQL語句裏可以引用JavaBean的屬性,而且只能引用JavaBean的屬性。

@Options

設置緩存時間,讓對象生成自增的主鍵值

@Insert("insert into user(username,password) values(#{username},#{password})")
     //加入該註解可以保存對象後,查看對象插入id
    @Options(useGeneratedKeys = true,keyProperty = "uid",keyColumn = "uid")
    void regist(User user);

設置緩存暫時還沒用到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章