传媒网站建设,国内跨境电商平台有哪些?,韩国最新新闻,网站建设和推广话术8.1、resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致#xff0c;则可以通过resultMap设置自定义映射
!--resultMap#xff1a;设置自定义映射属性#xff1a;id#xff1a;表示自定义映射的唯一标识type#xff1a;查询的数据要映射的实体类的…8.1、resultMap处理字段和属性的映射关系
若字段名和实体类中的属性名不一致则可以通过resultMap设置自定义映射
!--resultMap设置自定义映射属性id表示自定义映射的唯一标识type查询的数据要映射的实体类的类型子标签id设置主键的映射关系result设置普通字段的映射关系association设置多对一的映射关系collection设置一对多的映射关系属性property设置映射关系中实体类中的属性名column设置映射关系中表中的字段名
--
resultMap iduserMap typeUserid propertyid columnid/idresult propertyuserName columnuser_name/resultresult propertypassword columnpassword/resultresult propertyage columnage/resultresult propertysex columnsex/result
/resultMap
!--ListUser testMohu(Param(mohu) String mohu);--
select idtestMohu resultMapuserMap!--select * from t_user where username like %${mohu}%--select id,user_name,password,age,sex from t_user where user_name like concat(%,#{mohu},%)
/select若字段名和实体类中的属性名不一致但是字段名符合数据库的规则使用_实体类中的属性名符合Java的规则使用驼峰
此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系
可以通过为字段起别名的方式保证和实体类中的属性名保持一致可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase可以在查询表中数据时自动将_类型的字段名转换为驼峰
settingssetting namemapUnderscoreToCamelCase valuetrue/
/settings例如字段名user_name设置了mapUnderscoreToCamelCase此时字段名就会转换为userName ListUser getAllUser();select idgetAllUser resultTypeUserselect * from t_user
/select查询结果
[User(id4, userNameadmin, password123456, age18, gender男, email), User(id5, userNameadmin, password123456, age18, gender男, email)]8.2、多对一映射处理
场景模拟
查询员工信息以及员工所对应的部门信息
8.2.1、级联方式处理映射关系
resultMap idempDeptMap typeEmp
id columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/resultresult columndid propertydept.did/resultresult columndname propertydept.dname/result
/resultMap
!--Emp getEmpAndDeptByEid(Param(eid) int eid);--
select idgetEmpAndDeptByEid resultMapempDeptMapselect emp.*,dept.* from t_emp emp left join t_dept dept on emp.did dept.did where emp.eid #{eid}
/select8.2.2、使用association处理映射关系
resultMap idempDeptMap typeEmp
id columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/resultassociation propertydept javaTypeDeptid columndid propertydid/idresult columndname propertydname/result/association
/resultMap
!--Emp getEmpAndDeptByEid(Param(eid) int eid);--
select idgetEmpAndDeptByEid resultMapempDeptMapselect emp.*,dept.* from t_emp emp left join t_dept dept on emp.did dept.did where emp.eid #{eid}
/select8.2.3、分步查询
1、查询员工信息
/**
* 通过分步查询查询员工信息
* param eid
* return
*/
Emp getEmpByStep(Param(eid) int eid);resultMap idempDeptStepMap typeEmpid columneid propertyeid/idresult columnename propertyename/resultresult columnage propertyage/resultresult columnsex propertysex/result!--select设置分步查询查询某个属性的值的sql的标识namespace.sqlIdcolumn将sql以及查询结果中的某个字段设置为分步查询的条件--association propertydept selectcom.atguigu.MyBatis.mapper.DeptMapper.getEmpDeptByStep columndid/association
/resultMap!--Emp getEmpByStep(Param(eid) int eid);--
select idgetEmpByStep resultMapempDeptStepMapselect * from t_emp where eid #{eid}
/select2、根据员工所对应的部门id查询部门信息
/**
* 分步查询的第二步 根据员工所对应的did查询部门信息
* param did
* return
*/
Dept getEmpDeptByStep(Param(did) int did);!--Dept getEmpDeptByStep(Param(did) int did);--
select idgetEmpDeptByStep resultTypeDeptselect * from t_dept where did #{did}
/select8.3、一对多映射处理
8.3.1、collection
/**
* 根据部门id查新部门以及部门中的员工信息
* param did
* return
*/
Dept getDeptEmpByDid(Param(did) int did);resultMap iddeptEmpMap typeDeptid propertydid columndid/idresult propertydname columndname/result!--ofType设置collection标签所处理的集合属性中存储数据的类型--collection propertyemps ofTypeEmpid propertyeid columneid/idresult propertyename columnename/resultresult propertyage columnage/resultresult propertysex columnsex/result/collection
/resultMap!--Dept getDeptEmpByDid(Param(did) int did);--
select idgetDeptEmpByDid resultMapdeptEmpMapselect dept.*,emp.* from t_dept dept left join t_emp emp on dept.did emp.did where dept.did #{did}
/select8.3.2、分步查询
1、查询部门信息
/**
* 分步查询部门和部门中的员工
* param did
* return
*/
Dept getDeptByStep(Param(did) int did);resultMap iddeptEmpStep typeDeptid propertydid columndid/idresult propertydname columndname/resultcollection propertyemps fetchTypeeager selectcom.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid columndid/collection
/resultMap!--Dept getDeptByStep(Param(did) int did);--
select idgetDeptByStep resultMapdeptEmpStepselect * from t_dept where did #{did}
/select备注javaType和ofType都是用来指定对象类型的但是javaType是用来指定pojo中属性的类型而ofType指定的是映射到list集合属性中pojo的类型。
2、根据部门id查询部门中的所有员工
/**
* 根据部门id查询员工信息
* param did
* return
*/
ListEmp getEmpListByDid(Param(did) int did);!--ListEmp getEmpListByDid(Param(did) int did);--
select idgetEmpListByDid resultTypeEmpselect * from t_emp where did #{did}
/select分步查询的优点可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息
lazyLoadingEnabled延迟加载的全局开关。当开启时所有关联对象都会延迟加载
aggressiveLazyLoading当开启时任何方法的调用都会加载该对象的所有属性。否则每个属性会按需加载
也可以通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载 fetchType“lazy(延迟加载)|eager(立即加载)”
更多关于延迟加载的资料可以参考https://www.cnblogs.com/zbh355376/p/14986307.html
本文章参考B站 【尚硅谷】SSM框架全套教程MyBatisSpringSpringMVCSSM整合一套通关仅供个人学习使用部分内容为本人自己见解与尚硅谷无关。