不一样 使用别名 数据字段和bean_Mybatis-resultMap标签详解
是Maybatis的結(jié)果集封裝,搭配等標(biāo)簽的resultMap屬性使用
屬性:
id:該封裝規(guī)則的唯一標(biāo)識
type:封裝出來的類型,可以是jdk自帶的,比如Map,應(yīng)該可以是自定義的,比如Employee
autoMapping:自動封裝,如果數(shù)據(jù)庫字段和javaBean的字段名一樣,可以使用這種方式,但是不建議采取,還是老老實實寫比較穩(wěn)妥,如果非要使用此功能,那就在全局配置中加上mapUnderscoreToCamelCase=TRUE,它會使經(jīng)典數(shù)據(jù)庫字段命名規(guī)則翻譯成javaBean的經(jīng)典命名規(guī)則,如:a_column翻譯成aColumn
extends:繼承其他封裝規(guī)則,和Java中的繼承一樣,也特別適合這樣的場景
子標(biāo)簽:
用來標(biāo)識出對象的唯一性,比如用表的主鍵,如:
column指定數(shù)據(jù)庫字段名或者其別名
property指定javaBean的屬性名
還有jdbcType,javaType,typeHandler,分別數(shù)據(jù)庫類型,屬性的java類型,數(shù)據(jù)庫與Java類型匹配處理器
默認(rèn)的類型匹配處理器有:
以上羅列的資料不全,可以參考官方文檔的如下章節(jié)
非主鍵的映射規(guī)則
屬性:
property:同標(biāo)簽
javaType:同標(biāo)簽
select:指定嵌套SQL,可以是本XML或者其他XML文件中的
fetchType:延遲加載,lazy打開延遲加載;eager積極加載
column:同標(biāo)簽
resultMap:不使用嵌套SQL,而是使用復(fù)雜SQL一次取出關(guān)聯(lián)的對象,并封裝,對應(yīng)下面的方式三
resultSet:引用根據(jù)標(biāo)簽得到的resultSets,如:
SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM AUTHOR WHERE ID = #{id}
{call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}
autoMapping:同標(biāo)簽
columnPrefix:關(guān)聯(lián)多張表查詢時,為了使列明不重復(fù),使用此功能可以減少開發(fā)量
select
B.id as blog_id,
B.title as blog_title,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
CA.id as co_author_id,
CA.username as co_author_username,
CA.password as co_author_password,
CA.email as co_author_email,
CA.bio as co_author_bio
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Author CA on B.co_author_id = CA.id
where B.id = #{id}
抽取出共同的resultMap,因為作者和其他作者都具有同樣的屬性,因此有:
resultMap="authorResult" />
resultMap="authorResult"
columnPrefix="co_" />
foreignColumn:外鍵列
notNullColumn:不為空的列,如果指定了列,那么只有當(dāng)字段不為空時,Mybatis才會真正創(chuàng)建對象,才能得到我們想要的值
typeHandler:同標(biāo)簽
方式一:
select employee_id,first_name,last_name,gendor,birthday,a.dept_id,dept_name,dept_desc
from employee a,department b
where a.dept_id=b.dept_id
and a.employee_id=#{id}
方式二:
select="com.jv.dao.DepartMentMapper.getDepartMentByIdNew" column="dept_id">
select="com.jv.dao.PostMapper.getPostById" column="post_id">
select employee_id,first_name,last_name,gendor,birthday,dept_id,post_id
from employee a
where a.employee_id=#{id}
方式三:
select employee_id,first_name,last_name,gendor,birthday,a.dept_id,dept_name,dept_desc
from employee a,department b
where a.dept_id=b.dept_id
and a.employee_id=#{id}
和association很像,collection是負(fù)責(zé)處理多行的結(jié)果集,如:
方式一:
使用嵌套SQL
SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM POST WHERE BLOG_ID = #{id}
方式二:
使用復(fù)雜查詢-在collection中直接配置封裝規(guī)則
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
P.id as post_id,
P.subject as post_subject,
P.body as post_body,
from Blog B
left outer join Post P on B.id = P.blog_id
where B.id = #{id}
方式三:
使用復(fù)雜查詢-抽取封裝規(guī)則放到resultMap中
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
P.id as post_id,
P.subject as post_subject,
P.body as post_body,
from Blog B
left outer join Post P on B.id = P.blog_id
where B.id = #{id}
方式二和方式三只有在封裝的方式上有區(qū)別
負(fù)責(zé)根據(jù)返回的字段的值封裝不同的類型,如:
上面的配置就是如果vehicle_type的值是1,那么封裝的結(jié)果為carResult,同事carResult又繼承了vehicleResult的所有普通封裝規(guī)則,最后的它結(jié)果可以按照下面的配置理解
使用構(gòu)造器注入屬性值,可能會問既然通過和就可以給屬性注入值了,為什么還有一個構(gòu)造器注入的,原因是有的JavaBean沒有提供屬性的getter和setter方法。
為了維護更方便,推薦在使用構(gòu)造器注入的時候加上name屬性,該屬性值與構(gòu)造器參數(shù)名字相同(需要編譯參數(shù)加上-parameters,Jdk版本為1.8,而且Mybatis的參數(shù)useActualParamName設(shè)置為true(默認(rèn)值為true)),也可以使用@param("paramName")指定特殊的參數(shù)名。
總結(jié)
以上是生活随笔為你收集整理的不一样 使用别名 数据字段和bean_Mybatis-resultMap标签详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php会不会被人工智能取代,为什么这9种
- 下一篇: 给图片下方加水印_别再看不起美图秀秀啦,