国外优秀的网站,wordpress自建主题,蓟门桥网站建设,制作网站需要哪些素材自定义映射resultMap 自定义映射resultMap 自定义映射resultMapresultMap处理字段和属性的映射关系字段名和属性名不一致的情况#xff0c;如何处理映射关系?1、为查询的字段设置别名#xff0c;和属性名保持一致2、核心配置文件(mybatis-config.xml)中设置一个全局配置3、使…自定义映射resultMap 自定义映射resultMap 自定义映射resultMapresultMap处理字段和属性的映射关系字段名和属性名不一致的情况如何处理映射关系?1、为查询的字段设置别名和属性名保持一致2、核心配置文件(mybatis-config.xml)中设置一个全局配置3、使用resultMap自定义映射处理1处理多对一的映射关系①级联方式处理②association③分步查询 2处理一对多的映射关系 resultMap处理字段和属性的映射关系
字段名和属性名不一致的情况如何处理映射关系?
属性名 字段名
1、为查询的字段设置别名和属性名保持一致 select idgetEmpByEmpId resultTypeEmpselect emp_id empId,emp_name empName,age,gender from t_emp where emp_id #{empId}/select2、核心配置文件(mybatis-config.xml)中设置一个全局配置
当字段符合MySQL的要求使用_而属性符合java的要求使用驼峰 此时可以在MyBatis的核心配置文件(mybatis-config.xml)中设置一个全局配置可以自动将下划线映射为驼峰 emp_id:empId,emp_name:empName settings!--将下划线映射为驼峰--setting namemapUnderscoreToCamelCase valuetrue/!--开启延迟加载--setting namelazyLoadingEnabled valuetrue/!--按需加载--setting nameaggressiveLazyLoading valuefalse//settings3、使用resultMap自定义映射处理
resultMap设置自定义映射
属性 id表示自定义映射的唯一标识 type查询的数据要映射的实体类的类型 子标签 id设置主键的映射关系 result设置普通字段的映射关系 association设置多对一的映射关系 collection设置一对多的映射关系 属性 property设置映射关系中实体类中的属性名 column设置映射关系中表中的字段名
resultMap iduserMap typeUser
id propertyid columnid/id
result propertyuserName columnuser_name/result
result propertypassword columnpassword/result
result propertyage columnage/result
result 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},%)
/select1处理多对一的映射关系
多个员工对应一个部门
①级联方式处理 !-- 级联方式处理--resultMap idempAndDeptResultMapOne typeEmpid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columngender propertygender/resultresult columndept_id propertydept.deptId/resultresult columndept_name propertydept.deptName/result/resultMapselect idgetEmpAndDeptByEmpId resultMapempAndDeptResultMapOneselectt_emp.*,t_dept.*from t_empleft join t_depton t_emp.dept_id t_dept.dept_idwhere t_emp.emp_id #{empId}/select
②association !--association--resultMap idempAndDeptResultMap typeEmpid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columngender propertygender/result!--association处理多对一的映射关系处理实体类类型的属性property设置需要处理映射关系的属性的属性名javaType设置要处理的属性的类型--association propertydept javaTypeDeptid columndept_id propertydeptId/idresult columndept_name propertydeptName/result/association/resultMapselect idgetEmpAndDeptByEmpId resultMapempAndDeptResultMapselectt_emp.*,t_dept.*from t_empleft join t_depton t_emp.dept_id t_dept.dept_idwhere t_emp.emp_id #{empId}/select
③分步查询 DeptMapper.xml部门
select idgetEmpAndDeptByStepTwo resultTypeDeptselect * from t_dept where dept_id #{deptId}/select
EmpMapper.xml员工
!--分步查询--resultMap idempAndDeptByStepResultMap typeEmpid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columngender propertygender/result!--property设置需要处理映射关系的属性的属性名select设置分步查询的sql的唯一标识column将查询出的某个字段作为分步查询的sql的条件fetchType在开启了延迟加载的环境中通过该属性设置当前的分步查询是否使用延迟加载fetchTypeeager(立即加载)|lazy(延迟加载)--association propertydept fetchTypeeagerselectcom.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwocolumndept_id/association/resultMapselect idgetEmpAndDeptByStepOne resultMapempAndDeptByStepResultMapselect * from t_emp where emp_id #{empId}/select
2处理一对多的映射关系
一个部门对应多个员工 ①collection resultMap iddeptAndEmpResultMap typeDeptid columndept_id propertydeptId/idresult columndept_name propertydeptName/result!--ofType设置集合类型的属性中存储的数据的类型--collection propertyemps ofTypeEmpid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columngender propertygender/result/collection/resultMapselect idgetDeptAndEmpByDeptId resultMapdeptAndEmpResultMapSELECT *FROM t_deptLEFT JOIN t_empON t_dept.dept_id t_emp.dept_idWHERE t_dept.dept_id #{deptId}/select
②分步查询 DeptMapper.xml部门 resultMap iddeptAndEmpResultMapByStep typeDeptid columndept_id propertydeptId/idresult columndept_name propertydeptName/resultcollection propertyempsselectcom.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwocolumndept_id/collection/resultMap!--Dept getDeptAndEmpByStepOne(Param(deptId) Integer deptId);--select idgetDeptAndEmpByStepOne resultMapdeptAndEmpResultMapByStepselect * from t_dept where dept_id #{deptId}/selectEmpMapper.xml员工 select idgetDeptAndEmpByStepTwo resultTypeEmpselect * from t_emp where dept_id #{deptId}/select