上一篇文章我們講解了《Mybatis——解析SqlMapConfig.xml文件(四)》下面我們繼續來講解輸入輸出映射。
【輸入映射】
通過parameterType指定輸入參數的類型,類型可以是簡單類型、hashmap、pojo的包裝類型。我們主要來講解傳遞pojo的包裝對象。
1、需求
完成用戶信息的綜合查詢,需要傳入查詢條件很複雜(可能包括用戶信息、其它信息,比如商品、訂單的)
2、定義包裝類型pojo
針對上邊需求,建議使用自定義的包裝類型的pojo。在包裝類型的pojo中將複雜的查詢條件包裝進去。
public class UserQueryVo {
//在這裏包裝所需要的查詢條件
//用戶查詢條件
private UserCustom userCustom;
public UserCustom getUserCustom() {
return userCustom;
}
public void setUserCustom(UserCustom userCustom) {
this.userCustom = userCustom;
}
3、mapper.xml
<!-- 用戶信息綜合查詢
#{userCustom.sex}:取出pojo包裝對象中性別值
${userCustom.username}:取出pojo包裝對象中用戶名稱
-->
<select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo"
resultType="cn.itcast.mybatis.po.UserCustom">
SELCET * FROM USER WHERE user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.username}%'
</select>
4、mapper.java
public interface UserMapper {
//用戶信息綜合查詢
public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
}
5、測試代碼
@Test
public void testFindUserList() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//創建UserMapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//創建包裝對象,設置查詢條件
UserQueryVo userQueryVo = new UserQueryVo();
UserCustom userCustom = new UserCustom();
userCustom.setSex("1");
userCustom.setUsername("張三丰");
userQueryVo.setUserCustom(userCustom);
//調用userMapper的方法
List<UserCustom> list= userMapper.findUserCount(userQueryVo);
System.out.println(list);
【輸出映射】
1、 resultType
使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列纔可以映射成功。
如果查詢出來的列名和pojo中的屬性名全部不一致,沒有創建pojo對象。
只要查詢出來的列名和pojo中的屬性有一個一致,就會創建pojo對象。
(1)輸出簡單類型
(1.1) 需求
用戶信息的綜合查詢列表總數,通過查詢總數和上邊用戶綜合查詢列表纔可以實現分頁。
(1.2) mapper.xml
<!-- 用戶信息綜合查詢總數
parameterType:指定輸入類型和findUserList一樣
resultType:輸出結果類型
-->
<select id="findUserCount" parameterType="cn.itcast.mybatis.po.UserQueryVo" resultType="int">
SELECT count(*) FROM USER WHERE user.sex=#{userCustom.sex} AND user.username LIKE '%${userCustom.user}'
</select>
(1.3)mapper.java
//用戶信息綜合查詢總數
public int findUserCount(UserQueryVo userQueryVo) throws Exception;
(1.4) 測試代碼
@Test
public void testFindUserCount() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//創建UserMapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//創建包裝對象,設置查詢條件
UserQueryVo userQueryVo = new UserQueryVo();
UserCustom userCustom = new UserCustom();
userCustom.setSex("1");
userCustom.setUsername("張三丰");
userQueryVo.setUserCustom(userCustom);
//調用userMapper的方法
int count = userMapper.findUserCount(userQueryVo);
System.out.println(count);
}
(1.5)小結
查詢出來的結果集只有一行且一列,可以使用簡單類型進行輸出映射。
(2)7.1.2輸出pojo對象和pojo列表
不管是輸出的pojo單個對象還是一個列表(list中包括pojo),在mapper.xml中resultType指定的類型是一樣的。
在mapper.java指定的方法返回值類型不一樣:
(2.1)輸出單個pojo對象,方法返回值是單個對象類型
(2.2)輸出pojo對象list,方法返回值是List
生成的動態代理對象中是根據mapper方法的返回值類型確定是調用selectOne(返回單個對象調用)還是selectList (返回集合對象調用 ).
2、resultMap
mybatis中使用resultMap完成高級輸出結果映射。
(1)resultMap使用方法
如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關係。
①定義resultMap
②使用resultMap作爲statement的輸出映射類型
(2)將下邊的sql使用User完成映射
SELECT id id_,username username_ FROM USER WHERE id=#{value}
//User類中屬性名和上邊查詢列名不一致。
(2.1)定義reusltMap
<!-- 定義resultMap
將SELECT id id_,username username_ FROM USER 和User類中的屬性作一個映射關係
type:resultMap最終映射的java對象類型,可以使用別名
id:對resultMap的唯一標識
-->
<resultMap type="user" id="userResultMap">
<!-- id表示查詢結果集中唯一標識
column:查詢出來的列名
property:type指定的pojo類型中的屬性名
最終resultMap對column和property作一個映射關係 (對應關係)
-->
<id column="id_" property="id"/>
<!--
result:對普通名映射定義
column:查詢出來的列名
property:type指定的pojo類型中的屬性名
最終resultMap對column和property作一個映射關係 (對應關係)
-->
<result column="username_" property="username"/>
</resultMap>
(2.2)使用resultMap作爲statement的輸出映射類型
(2.3)mapper.java
(2.4)測試
@Test
public void testFindUserByIdResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//創建UserMapper對象,mybatis自動生成mapper代理對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//調用userMapper的方法
User user = userMapper.findUserByIdResultMap(1);
System.out.println(user);
}
3、小結
(1)使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列纔可以映射成功。
(2)如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關係。
好了小編今天的總結就到這裏了。。歡迎關注下面的內容《Mybatis—動態sql》。