1. 程式人生 > >Mybatis配置對映檔案中parameterType的用法

Mybatis配置對映檔案中parameterType的用法

在mybatis對映介面的配置中,有select,insert,update,delete等元素都提到了
parameterType的用法,parameterType為輸入引數,在配置的時候,配置相應的
輸入引數型別即可。parameterType有基本資料型別和複雜的資料型別配置。
1.基本資料型別,如輸入引數只有一個,其資料型別可以是基本的資料型別,也可以是
自己定的類型別。包括int,String,Integer,Date,如下:

(1)根據id進行相應的刪除:<delete id="deleteById" parameterType="Integer">

(2)新增員工:<insert id="addEmp" parameterType="com.pojo.Employee">

2.複雜資料型別:包含java實體類,map。

配置如:

  <select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher">  

       select * from Teacher where c_id=#{id} and sex=#{sex} 

  </select>  

另外MyBatis還提供了一個使用註解來參入多個引數的方式。這種方式需要在介面的引數上新增@Param註解
/**
* 此處要注意的是,由於該方法需要傳入多個引數,在進行Mybatis配置時,
* 沒有辦法同時配置多個引數,另外MyBatis還提供了一個使用註解來參入
* 多個引數的方式。這種方式需要在介面的引數上新增@Param註解。。
* 注意,以下兩種寫法是完全相同的。但使用的時候要使用第一種型別
*/

User login(@Param(value="name")String name,@Param(value="password")String password );
//    User login(String name,String password);

配置如下:

 <select id="login"  resultType="com.pojo.User">
    select * from us where name=#{name} and password=#{password}
   </select>