电子商务网站建设的相关流程ui设计软件sketch
web/
2025/10/5 17:26:11/
文章来源:
电子商务网站建设的相关流程,ui设计软件sketch,互站网,简单房地产网站在哪ResultMap与多表查询的处理
当字段名与实类名不一致时
使用别名进行处理
字段名#xff1a;emp_name
实体类名#xff1a;empName
映射文件中写法#xff1a; select idgetAllEmp resultTypeEmpselect eid, emp_name empName, age, se…ResultMap与多表查询的处理
当字段名与实类名不一致时
使用别名进行处理
字段名emp_name
实体类名empName
映射文件中写法 select idgetAllEmp resultTypeEmpselect eid, emp_name empName, age, sex, email, did from t_emp/select使用全局配置将下划线命名映射为驼峰
在mybatis-config.xml文件的properties标签和typeAlias标签之间添加settings标签如下可以将下划线式命名映射为驼峰 settingssetting namemapUnderscoreToCamelCase valuetrue//settings使用resultMap创建自定义的映射关系
在mapper.xml文件中进行定义 定义resultMap !-- 就算是自定义映射关系也需要相对应的实体类 --resultMap idempResultMap typeEmp!-- id用来声明主键,property用来表示实体类中的属性名、column用来标识数据库表中的字段名 --id propertyeid columneid/id!-- result用来声明普通字段 --result propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/result/resultMap传入resultMap的id以用来使用自定义映射 select idgetAllEmp resultMapempResultMapselect * from t_emp/select注意resultMap一般用于处理多对一、一对多的关系
一对多情况的处理
对于多对一的情况一个员工只会在一个部门中
只需要在员工实体类中添加一个部门属性 private Integer eid;private String empName;private Integer age;private String sex;private String email;private Integer did;private Dept dept;通过级联属性赋值resultMap解决多对一问题
在mapper.xml文件中创建resultMap resultMap idempAndDeptResultMap typeEmpid propertyeid columneid/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/resultresult propertydept.did columndid/resultresult propertydept.deptName columndept_name/result/resultMap再将这个传入语句进行调用
!-- Emp getEmpAndDept(Param(eid) Integer eid);--select idgetEmpAndDept resultMapempAndDeptResultMapselect *from t_emp left join t_dept on t_emp.did t_dept.didwhere eid#{eid}/select级联属性赋值的方式一般不使用
使用association解决多对一问题
在mapper.xml文件中创建resultMap resultMap idempAndDeptResultMapAsscoiation typeEmpid propertyeid columneid/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/resultassociation propertydept javaTypeDeptid propertydid columndid/idresult propertydeptName columndept_name/result/association/resultMap再将之传入即可
通过分步查询解决多对一问题
在mapper.xml建立如下
!-- 注意在分步查询的过程中association中的column代表传入的条件--resultMap idempAndDeptByStepResultMap typeempid propertyeid columneid/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/resultassociation propertydeptselectcom.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwocolumndid/association/resultMapDept的mapper如下
!-- Dept getEmpAndDeptByStepTwo(Param(did) Integer did);--select idgetEmpAndDeptByStepTwo resultTypeDeptselect * from t_dept where did #{did}/select调用 select idgetEmpAndDeptByStepOne resultMapempAndDeptByStepResultMapselect * from t_emp where eid #{eid}/select分布查询的优点延迟加载
当mybatis的查询语句由多步查询构成时我们可以开启mybatis的懒加载此时若我们只需要第一步的某个属性mybatis就不会去调用第二步的sql语句这样在多种场景下最大限度的保证了性能。 在全局配置mybatis-config.xml中添加如下配置 !-- 全局配置自动将下划线转为驼峰懒加载--settingssetting namemapUnderscoreToCamelCase valuetrue/setting namelazyLoadingEnabled valuetrue/setting/settings同时我们也可以在association中使用fetchType标签来使延迟加载变得可控eager代表立即加载、lazy代表延迟加载
!-- 注意在分步查询的过程中association中的column代表传入的条件--resultMap idempAndDeptByStepResultMap typeempid propertyeid columneid/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/resultassociation propertydeptselectcom.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwocolumndidfetchTypeeager/association/resultMap多对一情况的处理
在dept实体类中添加多的的那个List private ListEmp empList;使用collection标签进行一口气的处理
在deptMapper下进行如下操作 resultMap iddeptAndEmpResultMap typeDeptid propertydid columndid/idresult propertydeptName columndept_name/result
!-- 这里的ofType代表传入的List的泛型--collection propertyempList ofTypeEmpid propertyeid columneid/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/resultresult propertyemail columnemail/result/collection/resultMapselect idgetDeptAndEmp resultMapdeptAndEmpResultMapselect *from t_emp left join t_dept on t_emp.did t_dept.didwhere t_dept.did#{did}/select通过分步查询进行处理
在DeptMapper中建立第一步的接口 /*** 通过分步查询部门以及部门中的员工信息*/Dept getDeptAndEmpByStepOne(Param(did) Integer did);在EmpMapper中建立第二步的接口 /*** 分步查询第二步根据did查询员工信息*/ListEmp getDeptAndEmpByStepTwo(Param(did) Integer did);在EmpMapper.xml文件中定义xml
!-- ListEmp getDeptAndEmpByStepTwo(Param(did) Integer did);--select idgetDeptAndEmpByStepTwo resultTypeEmpselect * from t_emp/select在DeptMapper.xml文件中定义xml resultMap iddeptAndEmpByStepResultMap typeDeptid propertydid columndid/idresult propertydeptName columndept_name/resultcollection propertyempListselectcom.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwocolumndid/collection/resultMapselect idgetDeptAndEmpByStepOne resultMapdeptAndEmpByStepResultMapselect * from t_dept where did #{did}/select
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87478.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!