1、mybatis的实体类继承

参考资料:
1、mybatis中实体类,po类继承另一个po类的情况
2、mybatis中resultMap配置细则

实体类的继承的作用是:可以通过继承减少代码在实体类中的重复使用,比如数据库的表中常常出现的字段所对应的实体类中的属性。

2、mybatis的resultMap标签中的属性extends,可以继承另外一个命名空间的resultMap标签。

<mapper namespace="xuecheng.dao.ACVSFundTest"><resultMap id="res" extends="xuecheng.dao.BaseEntityTest.baseCol" type="xuecheng.domain.ACVSFund"><!--<id column="id" property="id"/>--><result column="fund_code" property="fundCode"/><result column="fund_name" property="fundName"/><result column="gmt_Create" property="gmtCreate"/></resultMap>

继承另一个

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="xuecheng.dao.BaseEntityTest"><resultMap id="baseCol" type="xuecheng.domain.BaseEntity"><id column="id" property="id"/><result column="username" property="username"/><result column="age" property="age"/></resultMap>

一、mybatis的属性标签

<resultMap><constructor><idArg/><arg/></constructor><id/><result/><association property=""/><collection property=""/><discriminator javaType=""><case value=""></case></discriminator>
</resultMap>

1.1、标签constructor

默认情况下,mybatis会使用实体类中的无参构造。当提供一个有参构造后,实体类中就不会提供无参构造。这时就需要在resultMap中特殊配置

假设实体类中的配置如下:

public User(Long id, String username, String password, String address) {this.id = id;this.username = username;this.password = password;this.address = address;}

映射文件的配置如下:

<resultMap id="userResultMap" type="org.sang.bean.User"><constructor><idArg column="id" javaType="long"/><arg column="username" javaType="string"/><arg column="password" javaType="string"/><arg column="address" javaType="string"/></constructor></resultMap>

1.2、标签association

适用的场景是一对一的

两张表:1、alias

2、province

provinceMapper.xml文件如下

<mapper namespace="xuecheng.dao.ProvinceMapper"><resultMap id="provinceResultMapper" type="xuecheng.domain.Province"><id column="id" property="id"/><!--association 中的属性property 是一对一中的属性名称,column是指传入参数的idselect 是指执行的方法--><association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/></resultMap><select id="getProvince" resultMap="provinceResultMapper">SELECT * FROM province</select></mapper>

alias的xml文件如下:

<mapper namespace="xuecheng.dao.AliasMapper"><select id="findAliasByPid" parameterType="long" resultType="xuecheng.domain.Alias">SELECT * FROM alias WHERE pid=#{id}</select>
</mapper>

1.3、标签collection

 <resultMap id="provinceResultMapper" type="xuecheng.domain.Province"><id column="id" property="id"/><result column="name" property="name"/><!--association 中的属性property 是一对一中的属性名称,column是指传入参数的idselect 是指执行的方法--><!--<association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/>--><!--<collection property="cities" column="id" select="xuecheng.dao.CityMapper.findById"/>-->
</resultMap>

1.4、标签 discriminator (有点类似于switch语句)

    <resultMap id="provinceResultMapper" type="xuecheng.domain.Province"><id column="id" property="id"/><result column="name" property="name"/><!--association 中的属性property 是一对一中的属性名称,column是指传入参数的idselect 是指执行的方法--><!--<association property="alias" column="id" select="xuecheng.dao.AliasMapper.findAliasByPid"/>--><!--collection的测试--><!--<collection property="cities" column="id" select="xuecheng.dao.CityMapper.findById"/>--><!--以下有点类似于switch语句--><discriminator javaType="int" column="area"><case value="1" resultMap="noodleResultMap"></case><case value="2" resultMap="riceResultMap"></case></discriminator><!--以下collection,不能查出准确数据。原因不知因为是啥?--><!--<collection property="cities" ofType="xuecheng.domain.City">--><!--<id column="id" property="id"/>--><!--<result column="name" property="name"/>--><!--<result column="pid" property="pid"/>--><!--</collection>--></resultMap>

1.5、注意点:mybatis的懒加载

1.5.1、开启的两种方式,方式一(全局开启,即对所有查询):

<settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>

说明:lazyLoadingEnabled默认是false,即不开启。aggressiveLazyLoading默认是true,即按照层级来进行查询,noodle和rice是同一级,即查询noodle的同时,rice也查询。如果是false,即是按需进行查询,需求查询noodle的时候,就不去查询rice了。

1.5.2、方式二(对局部进行操作)

在collection标签和association标签中,使用fetchType属性,eager代表立即加载,lazy代表延迟加载。

<association property="alias" column="id" select="org.sang.db.AliasMapper.findAliasByPid" fetchType="eager"/>
<collection property="cities" column="id" select="org.sang.db.CityMapper.findCityByPid" fetchType="lazy"/>

1.6、sql标签

可以只封装一部分查询语句,如下:

<sql id="selectAll3">id,username,address,password
</sql>

引用方式如下:

<select id="getUser3" resultType="user">SELECT<include refid="selectAll3"/> FROM user</select>

还可以在封装的时候使用一些变量,如下:

<sql id="selectAll4">${prefix}.id,${prefix}.username,${prefix}.address
</sql>

注意变量引用方式是$符号哦,不是#,引用方式如下:

<select id="getUser4" resultType="user" parameterType="string">SELECT<include refid="selectAll4"><property name="prefix" value="u"/></include> FROM user u
</select>

mybatis的resultMap配置详解相关推荐

  1. Spring、Spring MVC、MyBatis整合文件配置详解

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经 ...

  2. MyBatis(一)MyBatis介绍和配置详解

    在Java程序里面去操作数据库,最原始的办法是使用JDBC的API.需要分为六步: 注册驱动 通过DriverManager获取一个Connection 通过Connection创建一个Stateme ...

  3. Mybatis的逆向工程配置详解

    相信熟悉Hibernate和Mybatis的都知道,Hibernate是一种全自动的ORM数据持久框架,而Mybatis是一种半自动化的ORM数据持久框架.即使用Mybatis时,我们需要自己进行编写 ...

  4. mybatis 同名方法_MyBatis(四):xml配置详解

    目录 1.我们将 数据库的配置语句写在 db.properties 文件中 2.在 mybatis-configuration.xml 中加载db.properties文件并读取 通过源码我们可以分析 ...

  5. 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)> 介绍了properties与environments, ...

  6. mybatis获取表名_mybatis plus 的动态表名的配置详解

    mybatis plus简介 详见mybatis plus的官网 业务要求 业务上要求存储数据的时候根据某个字段动态的选择数据要存储的表. 如根据code字段: code->[1001,1002 ...

  7. mybatis 配置详解

    mybatis 配置详解 mybatis-config.xml 核心配置文件 mybatis-config.xml 包含的内容如下 configuration(配置) properties(属性) s ...

  8. mybatis基础总结02 -配置详解

    二 mybatis配置详解 MyBatis最关键的组成部分是SqlSessionFactory,我们可以从中获取SqlSession, 并执行映射的SQL语句.SqlSessionFactory对象可 ...

  9. Mybatis系列全解(四):全网最全!Mybatis配置文件XML全貌详解

    封面:洛小汐 作者:潘潘 做大事和做小事的难度是一样的.两者都会消耗你的时间和精力,所以如果决心做事,就要做大事,要确保你的梦想值得追求,未来的收获可以配得上你的努力. 前言 上一篇文章 <My ...

最新文章

  1. 推荐60+ Flex开发参考网站
  2. kohana中的路由规则
  3. Scrapy实例1-爬取天气预报存储到Json
  4. 【设计模式】单例模式-生成器模式-原型模式
  5. 两个等号(==)和三个等号(===)的区别
  6. linux支持表情的字体,让应用程序支持emoji字符
  7. JavaWeb学习之路——jsp与serverlet(一)
  8. 【渝粤教育】国家开放大学2018年秋季 0273-22T中国现代文学 参考试题
  9. python ddos攻击脚本_python版本DDOS攻击脚本
  10. dw2020表格不可见_【分享表格模板】手把手教你算小目标实际本金和综合年化收益率...
  11. 如何进行网站性能优化
  12. POJ 3580 SuperMemo 伸展树
  13. 【转】其他人的BUG
  14. 微信公众号授权突破微信开放平台只能配置两个的限制
  15. HTML-img图片详解
  16. 已加密的Zip压缩包暴力破解方式Archpr+John
  17. 这个城市最安静的声音 - Suzy
  18. java版铁傀儡刷新机制,我的世界:新版村庄的铁傀儡数量都快赶上村民了?刷新效率很高!...
  19. 河北科技大学计算机辅助制造试题,EDA关键技术考试试卷.doc
  20. matlab 怎么控制ccd,CCD camera的一般控制流程及些许困惑

热门文章

  1. PHP 企业级框架 Laravel9 版全新发布
  2. uni-app学习二
  3. Proteus基于51单片机通过PWM脉冲调制控制电机转速_按键与串口控制转速_电机转速可测
  4. python 中list去重
  5. 温度历史数据php,历史天气API接口_免费数据接口 - 极速数据
  6. android之定时发送短信消息
  7. 什么是“堆”,栈,堆栈,队列,它们的区别
  8. 大不了高三艹个FZU
  9. 地理信息培训考试(all)20+min90+
  10. 抽奖概率 php_php抽奖概率算法