mybatis入门(四)----输入映射和输出映射
閱讀目錄
- 一:輸入映射
- 二:輸出映射
一:輸入映射
通過parameterType指定輸入?yún)?shù)的類型,類型可以是簡單類型、hashmap、pojo的包裝類型。
1.1.傳遞pojo的包裝對象
1.1.1.需求描述
完成用戶信息的綜合查詢,需要傳入的查詢條件可能很復(fù)雜(可能包括用戶信息,其它信息,比如,商品,訂單等等)。
1.1.2.定義包裝類型的pojo
針對上邊的需求,建議使用自定義的包裝類型pojo,在包裝類型的pojo中將復(fù)雜的查詢條件包裝進(jìn)去。
包裝查詢條件的pojo類UserQueryVo類的代碼:
View Code?UserCustom類的代碼
View Code?UserMapper.xml的代碼
在UserMapper.xml中定義用戶信息綜合查詢(假設(shè)查詢條件很復(fù)雜,通過高級查詢復(fù)雜關(guān)聯(lián)查詢)
View CodeUserMapper.java類的代碼
View CodeJunit單元測試代碼
View Code 回到頂部二:輸出映射
1.resultType
使用resultType進(jìn)行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。
如果查詢出來的列名和pojo中的屬性名全部不一致,沒有創(chuàng)建pojo對象。
只要查詢出來的列名和pojo中的屬性有一個(gè)一致,就會創(chuàng)建pojo對象。
1.1.輸出簡單類型
? 需求:用戶信息的綜合查詢列表,通過查詢總數(shù)才能實(shí)現(xiàn)分頁功能。‘
?UserMapper.xml的代碼
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace命名空間,作用就是對sql進(jìn)行分類化的管理,理解為sql隔離 6 注意:使用mapper代理開發(fā)時(shí),namespace有特殊作用,namespace等于mapper接口地址 7 --> 8 <!-- 用戶信息綜合查詢總數(shù) 9 parameterType:指定輸入類型,和findUserList一致。 10 resultType:輸出結(jié)果類型. 11 --> 12 <mapper namespace="com.mybatis.mapper.UserMapper"> 13 <select id="findUserCount" parameterType="com.mybatis.entity.UserQueryVo" resultType="int"> 14 select count(*) from t_user where sex=#{userCustom.sex} and username like '%${userCustom.username}%' 15 </select> 16 </mapper>UserMapper.java的代碼
1 public interface UserMapper { 2 //用戶信息綜合查詢總數(shù) 3 public int findUserCount(UserQueryVo userQueryVo); 4 }Junit測試代碼
View Code小結(jié):查詢出來的結(jié)果集只有一行一列,可以使用簡單類型進(jìn)行輸出映射。
1.2.輸出pojo對象和pojo列表?
? 不管是輸出的pojo單個(gè)對象還是一個(gè)列表(list中包括pojo),在mapper.xml中resultType指定的類型是一樣的。
在mapper.java指定的方法返回值類型不一樣。
1.2.1.輸出單個(gè)pojo對象,方法返回值是單個(gè)對象類型
1 public interface UserMapper { 2 /** 根據(jù)ID查詢用戶信息 */ 3 public User findUserById(int id); 4 }1.2.2.輸出pojo對象list,方法返回值是List<pojo>
1 public interface UserMapper { 2 /** 根據(jù)用戶名稱模糊查詢用戶信息 */ 3 public List<User> findUserByName(String username); 4 }小結(jié):生成的動(dòng)態(tài)代理對象中是根據(jù)mapper.java方法的返回值類型確定是調(diào)用selectOne(返回單個(gè)對象調(diào)用)還是selectList(返回集合對象調(diào)用)
1.3.輸出類型resultMap
mybatis中使用resultMap完成高級輸出結(jié)果映射。
1.3.1.resultMap使用方法
(1).定義resultMap
(2).使用resultMap作為statement的輸出映射類型
1.3.2.demo例子
(1).需求:將下面的sql使用User完成映射
select id id_,username username_ from t_user where id=?
User類中屬性名和上邊的列名不一致。
(2).定義resultMap
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace命名空間,作用就是對sql進(jìn)行分類化的管理,理解為sql隔離 6 注意:使用mapper代理開發(fā)時(shí),namespace有特殊作用,namespace等于mapper接口地址 7 --> 8 <!-- 用戶信息綜合查詢總數(shù) 9 parameterType:指定輸入類型,和findUserList一致。 10 resultType:輸出結(jié)果類型. 11 --> 12 <mapper namespace="com.mybatis.mapper.UserMapper"> 13 <!-- 定義resultMap 14 select id id_,username username_ from t_user和User類中的屬性作為一個(gè)映射關(guān)系 15 type:resultMap最終映射的java對象類型,可以使用別名; 16 id:對resultMap的唯一標(biāo)識 17 --> 18 <resultMap type="user" id="userResultMap"> 19 <!--id表示查詢結(jié)果中的唯一標(biāo)識, 20 column:查詢出來的列名 21 property:type指定的pojo類型中的屬性名 22 最終resultMap對column和property作一個(gè)映射關(guān)系(對應(yīng)關(guān)系) 23 --> 24 <id column="id_" property="id"/> 25 <!--result:表示對普通列名的映射, 26 column:查詢出來的列名 27 property:type指定的pojo類型中的屬性名 28 最終resultMap對column和property作一個(gè)映射關(guān)系(對應(yīng)關(guān)系) 29 --> 30 <result column="username_" property="username"/> 31 </resultMap> 32 </mapper>?
(3).使用resultMap作為statement的輸出類型映射
1 <!-- 使用resultMap作為輸出映射 2 resultMap:指定定義的resultMap的id,如果這個(gè)resultMap在其它的mapper.xml文件中,前邊需要添加namespace命名空間 3 --> 4 <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap" > 5 select id id_,username username_ from t_user where id=#{id} 6 </select>UserMapper.java代碼
public interface UserMapper {/** 根據(jù)ID查詢用戶信息,使用resultMap進(jìn)行輸出 */ public User findUserByIdResultMap(int id); }Junit測試代碼:
View Code小結(jié):
用resultType進(jìn)行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。
如果查詢出來的列名和pojo的屬性名不一致,通過定義一個(gè)resultMap對列名和pojo屬性名之間作一個(gè)映射關(guān)系。
轉(zhuǎn)載于:https://www.cnblogs.com/yanyan0520/p/8758412.html
總結(jié)
以上是生活随笔為你收集整理的mybatis入门(四)----输入映射和输出映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ThinkPHP5显示数据库字段内容
- 下一篇: [BZOJ3214][ZJOI2013]