MyBatis映射文件中parameterType與resultType的用法詳解
resultMap
表示查詢結(jié)果集與java對(duì)象之間的一種關(guān)系,處理查詢結(jié)果集,映射到j(luò)ava對(duì)象。
resultMap 是一種“查詢結(jié)果集---Bean對(duì)象”屬性名稱映射關(guān)系,使用resultMap關(guān)系可將將查詢結(jié)果集中的列一一映射到bean對(duì)象的各個(gè)屬性(兩者屬性名可以不同,配置好映射關(guān)系即可),適用與復(fù)雜一點(diǎn)的查詢。
(1)適用于表的連接查詢(在resultMap里面可以配置連接條件,見(jiàn)如下程序association標(biāo)簽)
<!-- 訂單查詢關(guān)聯(lián)用戶的resultMap將整個(gè)查詢的結(jié)果映射到cn.itcast.mybatis.po.Orders中 -->
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
<!-- 配置映射的訂單信息 -->
<!-- id:指定查詢列中的唯 一標(biāo)識(shí),訂單信息的中的唯 一標(biāo)識(shí),如果有多個(gè)列組成唯一標(biāo)識(shí),配置多個(gè)id ,column:訂單信息的唯 一標(biāo)識(shí)列 ,property:訂單信息的唯 一標(biāo)識(shí) 列所映射到Orders中哪個(gè)屬性 -->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property=note/>
<!-- 配置映射的關(guān)聯(lián)的用戶信息 -->
<!-- association:用于映射關(guān)聯(lián)查詢單個(gè)對(duì)象的信息property:要將關(guān)聯(lián)查詢的用戶信息映射到Orders中哪個(gè)屬性 -->
<association property="user" javaType="cn.itcast.mybatis.po.User">
<!-- id:關(guān)聯(lián)查詢用戶的唯 一標(biāo)識(shí)
column:指定唯 一標(biāo)識(shí)用戶信息的列
javaType:映射到user的哪個(gè)屬性-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>2)適用于表的一對(duì)多連接查詢,(如,訂單對(duì)應(yīng)多個(gè)訂單明細(xì)時(shí),需要根據(jù)連接條件訂單id匹配訂單明細(xì),并且消除重復(fù)的訂單信息(訂單明細(xì)中的),如下程序);
<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap">
<!-- 訂單信息 -->
<!-- 用戶信息 -->
<!-- 使用extends繼承,不用在中配置訂單信息和用戶信息的映射 -->
<!-- 訂單明細(xì)信息一個(gè)訂單關(guān)聯(lián)查詢出了多條明細(xì),要使用collection進(jìn)行映射
collection:對(duì)關(guān)聯(lián)查詢到多條記錄映射到集合對(duì)象中
property:將關(guān)聯(lián)查詢到多條記錄映射到cn.itcast.mybatis.po.Orders哪個(gè)屬性
ofType:指定映射到list集合屬性中pojo的類型 -->
<collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
<!-- id:訂單明細(xì)唯 一標(biāo)識(shí)
property:要將訂單明細(xì)的唯 一標(biāo)識(shí) 映射到cn.itcast.mybatis.po.Orderdetail的哪個(gè)屬性-->
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
</collection>
</resultMap> (3)映射的查詢結(jié)果集中的列標(biāo)簽可以根據(jù)需要靈活變化,并且,在映射關(guān)系中,還可以通過(guò)typeHandler設(shè)置實(shí)現(xiàn)查詢結(jié)果值的類型轉(zhuǎn)換,比如布爾型與0/1的類型轉(zhuǎn)換。
例如:
<resultMap type="hdu.terence.bean.Message" id="MessageResult">
<!--存放Dao值--><!--type是和數(shù)據(jù)庫(kù)對(duì)應(yīng)的bean類名Message-->
<id column="id" jdbcType="INTEGER"property=" id"/><!--主鍵標(biāo)簽-->
<result column="COMMAND" jdbcType="VARCHAR" property="command"/>
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
<result column="CONTENT" jdbcType="VARCHAR" property="content"/>
</resultMap>
<select id="queryMessageList" parameterType="hdu.terence.bean.Message" resultMap="MessageResult">
SELECTID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1
<if test="command!=null and!"".equals(command.trim())">
and COMMAND=#{command}
</if>
<if test="description!=null and!"".equals(description.trim())">
and DESCRIPTION like '%' #{description} '%'
</if>
</select>resultType
resultType 是一種“查詢結(jié)果集---Bean對(duì)象”數(shù)據(jù)類型映射關(guān)系,使用resultType關(guān)系,即可使Bean對(duì)象接收查詢結(jié)果集;見(jiàn)名知意,該方法是通過(guò)查詢結(jié)果集中每條記錄(屬性)的數(shù)據(jù)類型和Bean對(duì)象的數(shù)據(jù)類型作映射,若兩者都相同,則表示匹配成功,Bean可以接收到查詢結(jié)果。
但是本方法有局限性,要求Bean對(duì)象字段名和查詢結(jié)果集的屬性名相同(可以大小寫不同,大小寫不敏感)。因?yàn)檫@個(gè)局限性,可以省略調(diào)resultMap進(jìn)行屬性名映射。
一般適用于pojo(簡(jiǎn)單對(duì)象)類型數(shù)據(jù),簡(jiǎn)單的單表查詢。
以下是resultType的寫法,將其值設(shè)置成對(duì)應(yīng)的java類上即可。不需要上述resultMap的映射關(guān)系。
<select resultType="User" id="findAll">select *from user </select>
<select resultType="com.itxiaotong.pojo.User" id="findById" parameterType="int">select *from user where id = #{userId} </select><select resultType="com.itxiaotong.pojo.User" id="findByUsernameLike" parameterType="string">
<bind value="'%'+username+'%'" name="likeName"/>
select * from user where username like #{likeName}
</select><select resultType="com.itxiaotong.pojo.User" id="findPage">select * from user limit #{param1},#{param2} </select>
<select resultType="com.itxiaotong.pojo.User" id="findPage1">select * from user limit #{startIndex},#{pageSize} </select><select resultType="User" id="findPage2" parameterType="PageQuery">select * from user limit #{startIndex},#{pageSize} </select>其中parameterType="PageQuery"的類是,下列內(nèi)容
PageQuery.java
package com.itxiaotong.pojo;
public class PageQuery {
private int startIndex;
private int pageSize;
public PageQuery() {
}
public PageQuery(int startIndex, int pageSize) {
this.startIndex = startIndex;
this.pageSize = pageSize;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}<select resultType="com.itxiaotong.pojo.User" id="findPage3" parameterType="map">select * from user limit #{startIndex},#{pageSize} </select>
<select resultType="int" id="findCount">select count(id) from user </select>parameterType
在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType為輸入?yún)?shù),在配置的時(shí)候,配置相應(yīng)的輸入?yún)?shù)類型即可。parameterType有基本數(shù)據(jù)類型和復(fù)雜的數(shù)據(jù)類型配置。
1.基本數(shù)據(jù)類型,如輸入?yún)?shù)只有一個(gè),其數(shù)據(jù)類型可以是基本的數(shù)據(jù)類型,也可以是自己定的類類型。包括int,String,Integer,Date,如下:
(1)根據(jù)id進(jìn)行相應(yīng)的刪除:
(2)添加員工:
2.復(fù)雜數(shù)據(jù)類型:包含java實(shí)體類,map。
parameterType例子(一)
現(xiàn)在有一個(gè)Mapper配置文件,以下是片段:
<select id="queryCommandListByPage" resultMap="CommandResult" >
select <include refid="columns"/> from command a left join command_content b
on a.id=b.command_id
<where>
<if test="command.name != null and !"".equals(command.name.trim())">
and a.name=#{command.name}
</if>
<if test="command.description != null and !"".equals(command.description.trim())">
and a.description like '%' #{command.description} '%'
</if>
</where>
<if test="flag==1">
group by aid
</if>
order by id
</select>
<sql id="columns">
a.id aid,a.name,a.description,b.content,b.id,b.command_id
</sql>下面是IService接口:
/**
* 攔截器實(shí)現(xiàn)分頁(yè)
*/
public List<command> queryCommandListByPage(Map<String,Object>parameter);parameterType例子(二)
<insert id="add" parameterType="com.itxiaotong.pojo.User">insert into user(username, sex, address)values (#{username}, #{sex}, #{address}) </insert><update id="update" parameterType="com.itxiaotong.pojo.User">update user set username = #{username},sex = #{sex},address=#{address} where id = #{id} </update><delete id="delete" parameterType="int">delete from user where id = #{id} </delete><insert id="add2" parameterType="com.itxiaotong.pojo.User">
<!-- keyProperty:主鍵屬性名 keyColumn:主鍵列名 resultType:主鍵類型 order:執(zhí)行時(shí)機(jī) -->
<selectKey resultType="int" order="AFTER" keyColumn="id" keyProperty="id">SELECT LAST_INSERT_ID(); </selectKey>
insert into user(username, sex, address)values (#{username}, #{sex}, #{address})
</insert><select resultType="com.itxiaotong.pojo.User" id="findPage3" parameterType="map">select * from user limit #{startIndex},#{pageSize} </select>總結(jié)
到此這篇關(guān)于MyBatis映射文件中parameterType與resultType的用法詳解的文章就介紹到這了,更多相關(guān)parameterType與resultType用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot2.x整合shiro權(quán)限框架的使用
這篇文章主要介紹了springboot2.x整合shiro權(quán)限框架的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
mybatis-plus @select動(dòng)態(tài)查詢方式
這篇文章主要介紹了mybatis-plus @select動(dòng)態(tài)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
oracle+mybatis-plus+springboot實(shí)現(xiàn)分頁(yè)查詢的實(shí)例
本文主要介紹了oracle+mybatis-plus+springboot實(shí)現(xiàn)分頁(yè)查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Mapstruct對(duì)象插入數(shù)據(jù)庫(kù)某個(gè)字段總是為空的bug詳解
這篇文章主要為大家介紹了在一次需求開發(fā)Mapstruct中對(duì)象插入數(shù)據(jù)庫(kù)某個(gè)字段總是為空的bug問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
springboot項(xiàng)目如何開啟https服務(wù)
這篇文章主要介紹了springboot項(xiàng)目如何開啟https服務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
VSCode+Gradle搭建Java開發(fā)環(huán)境實(shí)現(xiàn)
這篇文章主要介紹了VSCode+Gradle搭建Java開發(fā)環(huán)境實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java單點(diǎn)登錄(SSO)的實(shí)現(xiàn)
SSO是指在多個(gè)應(yīng)用系統(tǒng)中個(gè),用戶只需要登陸一次就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng),本文主要介紹了java單點(diǎn)登錄的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07
Spring中的HandlerMapping執(zhí)行流程詳解
這篇文章主要介紹了Spring中的HandlerMapping執(zhí)行流程詳解,HandlerMapping在Spring MVC框架的jar包下面,他是處理映射器,為用戶發(fā)送的請(qǐng)求找到合適的Handler Adapter,它將會(huì)把請(qǐng)求映射為HandlerExecutionChain對(duì)象,需要的朋友可以參考下2023-08-08
postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù)
這篇文章主要介紹了postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

