輸入映射
通過parameterType指定輸入參數的類型,類型可以是簡單類型、hashmap、pojo包裝類型
包裝類示例
public class UserQueryVo{
//在這裏包裝所需要的查詢條件
//用戶查詢條件
private UserCustom getUserCustom(){
return userCustom;
}
public void setUserCustom(UserCustom userCustom){
this.userCustom = userCustom;
}
//可以包裝其他的查詢條件,訂單、商品
}
mapper.xml文件示例:
<!--用戶信息綜合查詢-->
<!-- #{userCustom.sex} :取出pojo包裝對象中性別值-->
<select id="findUserList" parameterType="UserQueryVo全路徑/別名" resultType="包裝類型全路徑或別名">
select * from user where user.id=#{屬性.屬性(對象組合)} and user.username like '%${屬性(單純的屬性,Java基本類型)}%'
</select>
mapper.java:
//用戶信息綜合查詢
public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
輸出映射
- 1、resultType
- 2、resultMap
resultType
使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列纔可以映射成功。
如果查詢出來的列名和pojo中的屬性名全部不一致,沒有創建pojo對象。
只要查詢出來的列名和pojo中的屬性有一個一致,就會創建pojo對象
resultMap詳解
mybatis中使用resultMap完成高級輸出結果映射
resultMap使用方法:
如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間做一個映射關係
1、定義resultMap
mapper.xml文件中
定義resultMap:
<!-- type:resultMap最終所映射的Java對象類型,可以使用別名 -->
<!-- id:對resultMap的唯一標識、 -->
<resultMap type="user" id="userResultMap">
<!-- id表示查詢結果集中唯一標識 column: 查詢出來的列名 property:type指定的pojo類型的屬性名 最終resultMap對column和property做一個映射關係(對應關係) -->
<id column="" property=""/>
<!-- result:對普通列名進行映射定義 column: 查詢出來的列名 property:type指定的pojo類型的屬性名 最終resultMap對column和property做一個映射關係(對應關係) -->
<result column="" property=""/>
</resultMap>
mapper.java文件中
<!-- 根據id查詢用戶信息,使用resultMap -->
public User findUserByIdResultMap(int id) throws Exception;
2、使用resultMap作爲statement的輸出映射類型
<!-- 使用resultMap進行輸出映射-->
<!-- resultMap:指定定義的resultMap的id,如果resultMap在其他的mapper文件中,前邊需要加namespace-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
SELECT id id_,username username_ FROM USER WHERE id =#{value}
</select>