mybatis中解决属性名和字段名不一致的方法
1、在sql查询语句中,给字段起别名保持和实体类中的属性名一致
<select id="getAllEmpOld" resultType="Emp">select eid,emp_name empName,age,sex,email from t_emp
</select>
2、设置全局配置,将_下划线自动映射为驼峰(在mybatis-config.xml中进行配置)
设置mybatis中的全局配置<settings><!--mapUnderscoreToCamelCase:将_自动映射为驼峰:emp_name -> empName--><!--这样就可以解决字段名和实体类属性名字不一致的问题--><setting name="mapUnderscoreToCamelCase" value="true"/></settings>
3、使用resultMap:设置自定义映射关系
<resultMap id="resultEmpMap" type="Emp"><!--resultMap:设置自定义映射关系type:设置映射关系中的实体类类型id 设置主键的映射关系 result普通字段的映射关系property:type设置中的实体类中属性的名字column:sql查询出来的字段名字--><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result>
</resultMap><!--List<Emp> getAllEmp();--><select id="getAllEmp" resultMap="resultEmpMap">select * from t_emp</select>
补充:解决多对一映射关系的三种方式
1、通过级联赋值的方式
<resultMap id="empAndDeptResultMapOne" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="did_name"></result>
</resultMap>
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}</select>
2、通过association
<resultMap id="empAndDeptResultMapTwo" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept" javaType="Dept"><!--先把查询出来的did dept_name和 javaType中设置的Dept类型进行映射然后将映射得到的Dept赋值给Emp中Dept属dept--><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap><select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid}</select>
3、通过分步查询(常用方式);
优点:可以实现延迟加载,但是必须在核心配置文件中开启lazyLoadingEnabled(延迟加载的全局开关,默认关闭false)设置全局配置信息,同时需要将aggressiveLazyLoading关闭(默认为false关闭,版本<=3.4.1默认为true开启);
EmpMapper
/*** 通过分布查询员工以及员工所对应的部门信息*//*** 分布查询第一步查询员工信息* @return*/Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
EmpMapper.xml
<resultMap id="empAndDeptByStepResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名column:设置分步查询的条件(第二步骤是根据第一步得到的员工信息中的did进行查询,所以这里的分布查询条件就是did然后第二步骤就根据第一步骤得到的did根据select中设置的唯一标识,找到对应的select语句然后进行相应的查询,得到部门信息)--><association property="dept"select="whut.zyf.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association></resultMap><!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);--><select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}</select>
DeptMapper
/*** 通过分步查询查询员工以及员工所对应的部门信息* 分布查询第二步:通过did查询员工所对应的部门信息*/Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="whut.zyf.mybatis.mapper.DeptMapper"><!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><!--因为在mybatis-config配置文件中已经设置了全局配置讲下划线转化为驼峰所以可以根据查询到的did 和 dept_name赋值给resultType中设置的Dept类型中对应的值--><select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where did = #{did}</select>
</mapper>
mybatis中解决属性名和字段名不一致的方法相关推荐
- 解决属性名和字段名不一致的问题(Mybatis)
解决属性名和字段名不一致的问题(Mybatis) 1.数据库中的字段 新建一个项目,拷贝之前,测试实体字段不一致的情况 User package com.kk.pogo;public class Us ...
- mybatis -plus 将数据库中表名和字段名中的下划线去掉并且按照驼峰命名法映射
application.yml 配置 mybatis-plus:configuration:#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射map-underscore ...
- MyBatis构建sql时动态传入表名以及字段名
http://wendy-wxie.iteye.com/blog/1605193 用了mybatis很长一段时间了,但是感觉用的都是比较基本的功能,很多mybatis相对ibatis的新功能都没怎么用 ...
- mybatis获取表名_mybatis动态调用表名和字段名
一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用功能.今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查询以及某些字段是否显示,如某张表的某些字段不让用户查询到.这 ...
- mybatis动态调用表名和字段名
一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用功能.今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查询以及某些字段是否显示,如某张表的某些字段不让用户查询到.这 ...
- mybatis获取表名——mybatis动态调用表名和字段名#{},${}
一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用功能.今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查询以及某些字段是否显示,如某张表的某些字段不让用户查询到.这 ...
- 【Hibernate】Hibernate中查询表名、字段名以及字段类型等信息
Hibernate中查询表名.字段名以及字段类型等信息的工具类 package com.lmb.ui.util;import org.hibernate.cfg.Configuration; impo ...
- PowerDesigner生成SQL脚本时表名、字段名带引号问题及解决方法
使用PowerDesigner生成数据库脚本时,表名一般会带引号. 如下: 加引号是PL/SQL的规范,数据库会严格按照""中的名称建表,如果没有"",会按照O ...
- 【mysql】表名或字段名与关键字重名解决方法
如果在一个sql业务中,你见到如下字段: date,for,check,while,end,long等作为了表名或者字段名,那么sql执行肯定会有异常信息! 代码: SELECT for,long F ...
最新文章
- c++ 函数模板_C/C++编程笔记:C++入门知识,深入解析C++函数和函数模板
- 【weex开发】环境配置流程
- [Everyday Mathematics]20150113
- xml语言与html,XML与HTML的分析处理
- Mac中安装Node和版本控制工具nvm遇到的坑
- .Net思想篇:为何我们需要思想大洗礼?
- java8 lambda maplist排序_「java8系列」流式编程Stream
- Nmap配合Masscan实现高效率扫描资产
- 家庭用计算机怎样选择设置网络位置,win7系统怎么选择网络位置
- 转载:公司招聘中不能说的秘密
- X86汇编语言从实模式到保护模式10:进入保护模式
- 大数据分析要避免哪些错误
- 工控HMI界面设计基本原则
- java中使用activiti(工作流)
- Spyder单步调试
- c语言char10是什么意思,c语言char是什么意思
- 成功解决H5画布图片跨域,详解 uniapp H5 画布自定义海报实现长按识别,分享,转发
- MySQL 中 delete where in 语句的子查询限制
- video视频多个循环播放
- [计算机视觉] AprilTag: A robust and flexible visual fiducial system(2011)论文理解