mybatis resultMap映射详解
生活随笔
收集整理的這篇文章主要介紹了
mybatis resultMap映射详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<resultMap>是Maybatis的結果集封裝,搭配<select><association>等標簽的resultMap屬性使用屬性:id:該封裝規則的唯一標識type:封裝出來的類型,可以是jdk自帶的,比如Map,應該可以是自定義的,比如EmployeeautoMapping:自動封裝,如果數據庫字段和javaBean的字段名一樣,可以使用這種方式,但是不建議采取,還是老老實實寫比較穩妥,如果非要使用此功能,那就在全局配置中加上mapUnderscoreToCamelCase=TRUE,它會使經典數據庫字段命名規則翻譯成javaBean的經典命名規則,如:a_column翻譯成aColumnextends:繼承其他封裝規則,和Java中的繼承一樣,也特別適合這樣的場景子標簽:<id>用來標識出對象的唯一性,比如用表的主鍵,如:<id column="employee_id" property="empId"/>column指定數據庫字段名或者其別名property指定javaBean的屬性名還有jdbcType,javaType,typeHandler,分別數據庫類型,屬性的java類型,數據庫與Java類型匹配處理器默認的類型匹配處理器有:145012_VWtp_3049601.png以上羅列的資料不全,可以參考官方文檔的如下章節145150_qXkB_3049601.png<result>非主鍵的映射規則<result column="dept_name" property="deptName"/><association>屬性:property:同<id>標簽javaType:同<id>標簽select:指定嵌套SQL,可以是本XML或者其他XML文件中的<select>fetchType:延遲加載,lazy打開延遲加載;eager積極加載column:同<id>標簽resultMap:不使用嵌套SQL,而是使用復雜SQL一次取出關聯的對象,并封裝,對應下面的方式三resultSet:引用根據<select>標簽得到的resultSets,如:SELECT * FROM BLOG WHERE ID = #{id}SELECT * FROM AUTHOR WHERE ID = #{id}
<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult" statementType="CALLABLE">{call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}
</select>
<resultMap id="blogResult" type="Blog"><id property="id" column="id" /><result property="title" column="title"/><association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id"><id property="id" column="id"/><result property="username" column="username"/><result property="password" column="password"/><result property="email" column="email"/><result property="bio" column="bio"/></association>
</resultMap>autoMapping:同<id>標簽columnPrefix:關聯多張表查詢時,為了使列明不重復,使用此功能可以減少開發量<select id="selectBlog" resultMap="blogResult">selectB.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_biofrom Blog Bleft outer join Author A on B.author_id = A.idleft outer join Author CA on B.co_author_id = CA.idwhere B.id = #{id}
</select>
抽取出共同的resultMap,因為作者和其他作者都具有同樣的屬性,因此有:<resultMap id="authorResult" type="Author"><id property="id" column="author_id"/><result property="username" column="author_username"/><result property="password" column="author_password"/><result property="email" column="author_email"/><result property="bio" column="author_bio"/>
</resultMap>
<resultMap id="blogResult" type="Blog"><id property="id" column="blog_id" /><result property="title" column="blog_title"/><association property="author"resultMap="authorResult" /><association property="coAuthor"resultMap="authorResult"columnPrefix="co_" />
</resultMap>foreignColumn:外鍵列notNullColumn:不為空的列,如果指定了列,那么只有當字段不為空時,Mybatis才會真正創建對象,才能得到我們想要的值typeHandler:同<id>標簽方式一:<!-- 嵌套方式 --><resultMap type="com.jv.bean.Employee" id="EmpAndDeptMethod2"><id column="employee_id" property="employeeId"/><result column="first_name" property="firstName"/><result column="last_name" property="lastName"/><result column="gendor" property="gendor"/><result column="birthday" property="birthday"/><!-- 可以將下面這部分抽離出來單獨使用一個resultMap進行標記,然后在association標簽中使用resultMap指向該標記 --><association property="dept" javaType="com.jv.bean.DepartMent"><id column="dept_id" property="deptId"/><result column="dept_name" property="deptName"/><result column="dept_desc" property="deptDesc"/></association></resultMap><select id="getEmployeeDetailInfo2" resultMap="EmpAndDeptMethod2">select employee_id,first_name,last_name,gendor,birthday,a.dept_id,dept_name,dept_desc from employee a,department bwhere a.dept_id=b.dept_idand a.employee_id=#{id}</select>方式二:<!-- 分布查詢 --><!-- 該功能有一個很重要的用途:可以支持延遲加載(按需加載),需要在全局配置文件中增加: lazyLoadingEnabled 打開延遲加載aggressiveLazyLoading 設置為TRUE是使用時全部加載,false是按需加載這兩個配置。--><resultMap type="com.jv.bean.Employee" id="EmpAndDeptMethod3"><id column="employee_id" property="employeeId"/><result column="first_name" property="firstName"/><result column="last_name" property="lastName"/><result column="gendor" property="gendor"/><result column="birthday" property="birthday"/><association property="dept"select="com.jv.dao.DepartMentMapper.getDepartMentByIdNew" column="dept_id"></association><association property="post"select="com.jv.dao.PostMapper.getPostById" column="post_id"></association></resultMap><select id="getEmployeeDetailInfo3" resultMap="EmpAndDeptMethod3">select employee_id,first_name,last_name,gendor,birthday,dept_id,post_idfrom employee awhere a.employee_id=#{id}</select>方式三:<!-- 嵌套方式 --><resultMap type="com.jv.bean.Employee" id="EmpAndDeptMethod2"><id column="employee_id" property="employeeId"/><result column="first_name" property="firstName"/><result column="last_name" property="lastName"/><result column="gendor" property="gendor"/><result column="birthday" property="birthday"/><!-- 可以將下面這部分抽離出來單獨使用一個resultMap進行標記,然后在association標簽中使用resultMap指向該標記 --><association property="dept" resultMap="deptMap"></association></resultMap><resultMap id="deptMap" javaType="com.jv.bean.DepartMent"><id column="dept_id" property="deptId"/><result column="dept_name" property="deptName"/><result column="dept_desc" property="deptDesc"/></resultMap><select id="getEmployeeDetailInfo2" resultMap="EmpAndDeptMethod2">select employee_id,first_name,last_name,gendor,birthday,a.dept_id,dept_name,dept_desc from employee a,department bwhere a.dept_id=b.dept_idand a.employee_id=#{id}</select><collection>和association很像,collection是負責處理多行的結果集,如:方式一:使用嵌套SQL<resultMap id="blogResult" type="Blog"><collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap><select id="selectBlog" resultMap="blogResult">SELECT * FROM BLOG WHERE ID = #{id}
</select><select id="selectPostsForBlog" resultType="Post">SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>方式二:使用復雜查詢-在collection中直接配置封裝規則<select id="selectBlog" resultMap="blogResult">selectB.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 Bleft outer join Post P on B.id = P.blog_idwhere B.id = #{id}
</select>
<resultMap id="blogResult" type="Blog"><id property="id" column="blog_id" /><result property="title" column="blog_title"/><collection property="posts" ofType="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><result property="body" column="post_body"/></collection>
</resultMap>方式三:使用復雜查詢-抽取封裝規則放到resultMap中<select id="selectBlog" resultMap="blogResult">selectB.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 Bleft outer join Post P on B.id = P.blog_idwhere B.id = #{id}
</select>
<resultMap id="blogResult" type="Blog"><id property="id" column="blog_id" /><result property="title" column="blog_title"/><collection property="posts" ofType="Post" resultMap="blogPostResult" columnPrefix="post_"/>
</resultMap><resultMap id="blogPostResult" type="Post"><id property="id" column="id"/><result property="subject" column="subject"/><result property="body" column="body"/>
</resultMap>方式二和方式三只有在封裝的方式上有區別<descriminator>負責根據返回的字段的值封裝不同的類型,如:<resultMap id="vehicleResult" type="Vehicle"><id property="id" column="id" /><result property="vin" column="vin"/><result property="year" column="year"/><result property="make" column="make"/><result property="model" column="model"/><result property="color" column="color"/><discriminator javaType="int" column="vehicle_type"><case value="1" resultMap="carResult"/><case value="2" resultMap="truckResult"/><case value="3" resultMap="vanResult"/><case value="4" resultMap="suvResult"/></discriminator>
</resultMap>
<resultMap id="carResult" type="Car" extends="vehicleResult"><result property="doorCount" column="door_count" />
</resultMap>上面的配置就是如果vehicle_type的值是1,那么封裝的結果為carResult,同事carResult又繼承了vehicleResult的所有普通封裝規則,最后的它結果可以按照下面的配置理解<resultMap id="carResult" type="Vehicle"><id property="id" column="id" /><result property="vin" column="vin"/><result property="year" column="year"/><result property="make" column="make"/><result property="model" column="model"/><result property="color" column="color"/><result property="doorCount" column="door_count"/>
</resultMap><constructor>使用構造器注入屬性值,可能會問既然通過<id>和<result>就可以給屬性注入值了,為什么還有一個構造器注入的,原因是有的JavaBean沒有提供屬性的getter和setter方法。<constructor><idArg column="id" javaType="int" name="id" /><arg column="age" javaType="_int" name="age" /><arg column="username" javaType="String" name="username" />
</constructor>
為了維護更方便,推薦在使用構造器注入的時候加上name屬性,該屬性值與構造器參數名字相同(需要編譯參數加上-parameters,Jdk版本為1.8,而且Mybatis的參數useActualParamName設置為true(默認值為true)),也可以使用@param("paramName")指定特殊的參數名。轉載于:https://my.oschina.net/u/3049601/blog/1613921
總結
以上是生活随笔為你收集整理的mybatis resultMap映射详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018年第九届蓝桥杯真题解析 | 星期
- 下一篇: 360服务器老是未响应怎么办,win10