Mybatis相關:基於註解的Mybatis開發

zitian246發表於2020-10-25

1.Mybatis常用註解

@Insert:實現新增
@Update:實現更新
@Delete:實現刪除
@Select:實現查詢
@Result:實現結果集封裝
@Results:可以與@Result 一起使用,封裝多個結果集
@ResultMap:實現引用@Results 定義的封裝
@One:實現一對一結果集封裝
@Many:實現一對多結果集封裝
@SelectProvider: 實現動態 SQL 對映
@CacheNamespace:實現註解二級快取的使用

2.基於註解的增刪改查

    @Insert("insert into student_tb(name,sex,age,height,s_address) values(#{name},#{sex},#{age},#{height},#{sAddress})")
    @SelectKey(keyProperty = "id",keyColumn = "id",before = false,statement = "select last_insert_id()",resultType = Integer.class)
    int addStudent(Student student);

    @Delete("delete from student_tb where id = #{id}")
    int deleteStudent(int id);

    @Update("update student_tb set name = #{name},age=#{age},sex=#{sex},height=#{height}, s_address = #{sAddress} where id = #{id}")
    int updateStudent(Student student);

    @Select("select * from student_tb where id = #{id}")
    //@ResultType(Student.class) 可以省略  設定返回值型別
    Student findStudentById(int id);

    @Select("select * from student_tb where id = #{id}")
    @Results(id = "studentMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "height",column = "height"),
            @Result(property = "sAddress", column = "s_address")
    }) //Resuts 相當於<resultMap   @Result相當於<result
    Student findStudentById2(int id);

    @Select("select * from student_tb")
    //@ResultType(Student.class)  可以省略
    @ResultMap("studentMap")
    List<Student> findAllStudent();

    /*
    * 當mybatis 方法引數為多個時,可以使用以下幾個方式傳參
    * 1.傳物件 傳map
    * 2.使用arg0 arg1...... 或者 param1 param2等命名
    * 3.先將引數宣告,再使用@Param給引數命名
    *
    * */

    /*org.apache.ibatis.binding.BindingException:
     Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]*/
    //@Select("select * from student_tb where name like #{arg0} and sex=#{arg1}")
    //@Select("select * from student_tb where name like #{param1} and sex=#{param2}")
    @Select("select * from student_tb where name like #{name} and sex=#{sex}")
    List<Student> findAllStudentByNameAndSex(@Param("name") String nameaa,@Param("sex") String sexxx);

3.一對多查詢

  @Select("select * from student_tb")
    @Results(id = "studentMap2",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "age",column = "age"),
            @Result(property = "name",column = "name"),
            @Result(property = "height",column = "height"),
            @Result(property = "sAddress",column = "s_address"),
            @Result(property = "scoreList",column = "id",many = @Many(
                    select = "com.qfedu.dao.IScoreDao2.findScoreById",fetchType = FetchType.EAGER
            ))
    })
    List<Student>findAllStudentWithScoreList();

4.註解式開啟快取

dao介面上之間加入此註解

// 開啟快取
@CacheNamespace(blocking = true)

5. 當mybatis 方法引數為多個時(報錯,解決)

當mybatis 方法引數為多個時,可以使用以下幾個方式傳參
    1.傳物件 傳map,將資料封裝成物件、map等傳遞
    2.使用arg0 arg1...... 或者 param1 param2等命名(注意從0或1開始)
    3.先將引數宣告,再使用@Param給引數命名

相關文章