位置: IT常识 - 正文

自定义映射resultMap(映射器可以定义参数类型)

编辑:rootadmin
resultMap处理字段和属性的映射关系 如果字段名与实体类中的属性名不一致,该如何处理映射关系? 第一种方法:为查询的字段设置别名,和属性名保持一致 下面是实体类中的属性名: private Integer empId; private String empName; private Integ ... resultMap处理字段和属性的映射关系

推荐整理分享自定义映射resultMap(映射器可以定义参数类型),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:自定义映射app,自定义映射什么意思,自定义映射什么意思,自定义映射app下载,自定义映射solidworks到dxf/dwg,自定义映射solidworks到dxf/dwg,自定义映射游戏按键,自定义映射solidworks到dxf/dwg,内容如对您有帮助,希望把文章链接给更多的朋友!

如果字段名与实体类中的属性名不一致,该如何处理映射关系?

第一种方法:为查询的字段设置别名,和属性名保持一致

下面是实体类中的属性名:

private Integer empId; private String empName; private Integer age; private String gender;

这是建表时设置的字段名:

emp_id emp_name age gender

我们只需要在Mapper.xml中在写sql语句时,对字段名进行设置别名,使得与属性名一致:

select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}

第二种方法:当字段符合Mysql要求使用下划线,而属性名符合Java要求使用驼峰,此时可以在Mybatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,就可以在查询表中数据时,自动将下划线类型的字段名转换为驼峰。

<settings> <!--将下划线映射为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>自定义映射resultMap(映射器可以定义参数类型)

第三种方法:使用resultMap处理

<!-- resultMap:设置自定义的映射关系 id:唯一标识 type:处理映射关系的实体类的类型 常用标签: id:处理主键和实体类中属性的映射关系 result:处理普通字段和实体类中属性的映射关系 column:设置映射关系中的字段名,必须是sql查询出的某个字段 property:设置映射关系中的属性的属性名,必须是处理实体类型类型中的属性名 --> <resultMap id="empResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> </resultMap> <!-- Emp getEmpByEmpId(@Param("empId") Integer emId);--> <select id="getEmpByEmpId" resultMap="empResultMap"> select * from t_emp where emp_id = #{empId} </select>多对一的映射关系1.级联方式处理映射关系

当Emp实体类中具有Dept对象,但是字段中不存在这个属性,我们需要将Dept对象中的属性与查询的字段名建立映射关系。

<resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <result column="dept_id" property="dept.deptId"></result> <result column="dept_name" property="dept.deptName"></result> </resultMap> <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap"> select t_emp.*,t_dept.* from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{empId} </select>2.使用association处理映射关系association:处理多对一的映射关系(处理实体类类型的属性)property:设置需要处理映射关系的属性的属性名javaType:设置要处理的属性的类型 <resultMap id="empAndDeptResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <association property="dept" javaType="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> </association> </resultMap>3.分步查询

首先查询员工的信息

/** * 通过分步查询员工的信息 * @param empId * @return */ Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId); <resultMap id="empAndDeptByStepResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <!--select:设置分步查询,查询某个属性的值的sql标识(namespace.sqlId)column:将sql以及查询结果中的某个字段设置为分步查询的条件--> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id"></association> </resultMap> <!-- Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);--> <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap"> select * from t_emp where emp_id = #{empId} </select>

根据员工所对应的部门id查询部门信息

/** * 分步查询第二步:根据员工所对应的id查询部门信息 * @param deptId * @return */ Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); <!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);--> <select id="getEmpAndDeptByStepTwo" resultType="Dept"> select * from t_dept where depy_id = #{deptId} </select>

分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关,当开启时,所有关联对象都会延迟加载。

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载

此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载。

一对多的映射关系1.collection /** * 根据部门id查部门中员工的信息 * @param deptId * @return */ Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId); <resultMap id="deptAndEmpResultMap" type="Dept"> <id column="dept_id" property="deptId"></id> <result column="dept_name" property="deptName"></result> <!-- ofType:设置collection标签所处理的集合属性中存储数据的类型 --> <collection property="emps" ofType="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> </collection> </resultMap> <!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);--> <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap"> select * from t_dept LEFT JOIN t_emp ON t_dept.dept_id = t_emp.dept_id WHERE t_dept.dept_id = #{deptId}; </select>2.分步查询

查询部门信息

/** * 分步查询部门以及部门中的员工信息第一步 * @param id * @return */ Dept getDeptAndEmpByStepOne(@Param("id") Integer id); <resultMap id="deptAnEmpResultMapByStep" type="Dept"> <id column="dept_id" property="depyId"></id> <result column="dept_name" property="deptName"></result> <collection property="emps" select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="dept_id"></collection> </resultMap> <!-- Dept getDeptAndEmpByStepOne(@Param("id") Integer id);--> <select id="getDeptAndEmpByStepOne" resultMap=""> select * from t_dept where dept_id = #{deptId} </select>

根据部门id查询部门中的员工信息

/** * 分步查询部门以及部门中的员工信息第二步 * @param dept_id * @return */ List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id); <resultMap id="empAndDeptByStepResultMap" type="Emp"> <id column="emp_id" property="empId"></id> <result column="emp_name" property="empName"></result> <result column="age" property="age"></result> <result column="gender" property="gender"></result> <association property="dept" select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id"></association> </resultMap> <!--List<Emp> getDeptAndEmpByStepTwo(@Param("dept_id") Integer dept_id);--> <select id="getDeptAndEmpByStepTwo" resultType="Emp"> select * from t_emp where dept_id = #{deptId} </select>
本文链接地址:https://www.jiuchutong.com/zhishi/311548.html 转载请保留说明!

上一篇:从 jQuery 到 Vue3 的快捷通道(jquery vue.js)

下一篇:设计模式---代理模式(设计模式代理模式)

  • 已抵扣的进项税发票怎么查询明细
  • 增值税专用发票抵扣最新规定
  • 国际重复征税解决方法有
  • 全国税务师官网报名
  • 其他综合收益属于损益类科目吗
  • 预提费用的附件怎么做
  • 场地使用费入什么科目
  • 个税是每个月都扣吗
  • 出口退免税备案
  • 外资企业撤退
  • 企业跨年补开发票
  • 没有ca证书怎么连接wifi加密设备
  • 高工资怎么说
  • 人民币结算意味着什么
  • 银行利息税是多少2021
  • 电力安装工程合同范本
  • 报销凭证找不到了怎么办
  • 票据遗失情况说明格式及范文
  • 税费通用申报表样板
  • 一般纳税人企业要交哪些税
  • 关于财产保险公司的论文
  • 技术服务费属于什么税收分类编码
  • 一般纳税人既有批发零售又有不动产租赁
  • 4s店额外收取服务费
  • 工会工费缴纳标准
  • 报销单据粘贴单图片
  • 增值税普通发票几个点
  • 土地无形资产摊销年限怎么确定
  • 两处发工资,如何合并
  • 上年度第四季度总结
  • php实现的三个常数是什么
  • php session实例
  • 跟踪路由如何实现
  • 电脑dat文件怎么打开
  • win11自动更新卡在94%
  • PHP:pg_query()的用法_PostgreSQL函数
  • 存放同业属于什么业务
  • 乌鲁米耶湖春季湖水更深
  • 企业付给个人工资怎么做
  • 小规模纳税人交税怎么做会计分录
  • react 入门教程
  • 购进的样品怎样做账
  • 如何简单去除
  • 中小型科技企业所面临的普遍问题是资金缺乏
  • 房屋租赁可以开具住宿费发票吗
  • 其他应收款其他应付款的风险
  • 认缴制注册资金不交可以吗
  • 公司性质和单位性质的区别
  • sql server 内存管理
  • 出口销售收入要交印花税吗
  • 增值税普通发票税率
  • 以前年度损失如何记账
  • 不带息银行汇票
  • 出口退回的增值税计入哪个会计科目
  • 房地产成本的概念
  • 投资收益期末余额在借方还是贷方
  • 百旺金赋领回来的发票怎么读入
  • 年末结账与财务的关系
  • sql server语句查询
  • sql server 快照
  • MySQL PXC构建一个新节点只需IST传输的方法(推荐)
  • 添加网络打印机找不到设备
  • ubuntu1
  • 注册表的命令
  • win8显示桌面图标
  • win10快速启动怎么进入bios
  • windows下键盘不能用
  • win7系统开机蓝屏0x0000007b
  • svchost占用
  • win8系统运行慢怎么办
  • win7系统开机登录不了怎么修复
  • quick cocos UIListView之isItemInViewRect方法修正
  • android中使用md5后报非法延续字节
  • perl -pe
  • unity 3d脚本编程
  • bat批处理的if里面双感叹号
  • shell脚本 su
  • 我置顶你也只顶你
  • 举例说明json数据格式的语法
  • 留抵退税退回的款如何做账
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设