标签:mapper 自定义 映射 dept resultMap01 Mybatis 属性 select emp
解决字段名和属性不一致的情况:
数据库表单:
java中对应类:
其中empName与emp_name不一致
1.为字段取别名,保持和属性名一致
案例:
接口方法:
/** * 查询所有员工信息 */ List<Emp> SelectAllEmp();
mapper文件:
<!-- List<Emp> SelectAllEmp(); 给表中属性取别名,与类的属性一致,就可以解决属性不相符,类的属性数据为空 --> <select id="SelectAllEmp" resultType="emp"> select eid,emp_name as empName,age,sex,email from t_emp </select>
2.设置全局配置,将_自动映射为驼峰
在mybatis主配置文件中添加全局变量:
<!--设置Mybatis的全局配置--> <settings> <!-- 将下划线_自动映射为驼峰,规则为将下划线后面的第一个单词首字母大写即可 比如:emp_name====>empName --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
mapper文件:
<select id="SelectAllEmp" resultType="emp"> select *from t_emp </select>
3.通过resultMap解决字段名和属性名的映射关系
只需要在mapper文件中进行resultMap配置即可
<!-- resultMap:设置自定义映射关系 id:唯一标识,不能重复 type:设置映射关系中的实体类类型 --> <resultMap id="ResultMap" type="com.hrf.domain.Emp"> <!-- 子标签: id:设置主键的映射关系 result:设置普通字段的映射关系 属性: property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性(就是实体类中的属性) column:设置映射关系中的字段名,必须是sql语句查询出来的字段名(数据库表中字段名) --> <id column="eid" property="eid"/> <result column="emp_name" property="empName"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="email" property="email"/> </resultMap> <select id="SelectAllEmp" resultMap="ResultMap"> select *from t_emp </select>
处理数据库表多对一映射关系
1.级联方式处理映射关系
创建的实体类:
部门
员工:
接口方法:
/** * 查询员工信息和对应的部门信息 */ Emp SelectEmpAngDept(int eid);
mapper文件:
<!--Emp SelectEmpAngDept(int eid);--> <resultMap id="ResultEmpAndDeptOne" type="Emp"> <id column="eid" property="eid"/> <result column="emp_name" property="empName"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="email" property="email"/> <result column="did" property="dept.did"/> <result column="dept_name" property="dept.deptName"/> </resultMap> <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne"> select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = 1 </select>
测试方法:
@Test public void ResultEmpAndDeptOne(){ SqlSession session = MybatisUtils.getSqlSession(); EmpMapper mapper = session.getMapper(EmpMapper.class); Emp emp = mapper.SelectEmpAngDept(1); System.out.println("员工信息和部门信息"+emp); session.close(); }
2.使用association处理映射关系
在resultMap标签中设置association标签
id标签就是设置主键映射 column是表中字段名,property是类型属性
result设置非主键映射 column是表中字段名,property是类型属性
<resultMap id="ResultEmpAndDeptOne" type="Emp"> <id column="eid" property="eid"/> <result column="emp_name" property="empName"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="email" property="email"/> <result column="did" property="dept.did"/> <!-- association:处理多对一的映射关系 property:需要处理多对一的映射关系的属性名(此时设置的就是emp表中dept属性) javaType:该属性的类型 --> <association property="dept" javaType="Dept"> <id column="did" property="did"/> <result column="dept_name" property="deptName"/> </association> </resultMap> <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne"> select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = 1 </select>
3.通过分步查询处理多对一的映射关系
创建两条sql语句 ,将其中一条语句查询出来的结果通过resultMap的association标签映射给类型属性
第一条sql语句:
<!-- Dept SelectDept(@Param("did") int did); --> <select id="SelectDept" resultType="dept"> select * from t_dept where did = #{did} </select>
emp类型属性说明:
第二条sql语句:
<resultMap id="ResultEmpAndDeptOne" type="Emp"> <id column="eid" property="eid"/> <result column="emp_name" property="empName"/> <result column="age" property="age"/> <result column="sex" property="sex"/> <result column="email" property="email"/> <result column="did" property="dept.did"/> <association property="dept" select="com.hrf.dao.DeptMapper.SelectDept" column="did"></association> </resultMap> <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne"> select * from t_emp where eid = #{eid} </select>
通过测试类方法观察结果
可以看到执行了两条sql语句
标签:mapper,自定义,映射,dept,resultMap01,Mybatis,属性,select,emp 来源: https://www.cnblogs.com/Aikz/p/15947657.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。