河北搜恒不给做网站,广西住建局官方网站,做壁画在哪个网站,dw做的静态网站怎么分享链接1.1、resultMap处理字段和属性的映射关系
如果字段名和实体类中的属性名不一致的情况下#xff0c;可以通过resultMap设置自定义映射。
常规写法
/***根据id查询员工信息* param empId* return*/
Emp getEmpByEmpId(Param(empId) Integer empId);select id…1.1、resultMap处理字段和属性的映射关系
如果字段名和实体类中的属性名不一致的情况下可以通过resultMap设置自定义映射。
常规写法
/***根据id查询员工信息* param empId* return*/
Emp getEmpByEmpId(Param(empId) Integer empId);select idgetEmpByEmpId resultTypeempselect * from emp where emp_id #{empId}/selectTest
public void testGetEmpByEmpId(){Emp emp empMapper.getEmpByEmpId(2);System.out.println(emp);
}
结果查出的id和name为空值
解决
⑴可以通过为字段起别名的方式别名起成和属性名一致。保证字段名和实体类中的属性名一致
select idgetEmpByEmpId resultTypeempselect emp_id empId,emp_name empName,age,sex from emp where emp_id #{empId}
/select
⑵如果字段名和实体类中的属性名不一致的情况下但是字段名符合数据库的规则(使用_),实体类中使用的属性名符合java的规则(使用驼峰命名)可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase可以在查询表中的数据时自动将带下划线“_”的字段名转为驼峰命名 user_name:userName emp_id:empId
mybatis-config.xml文件中
settings !--将数据库字段名的下划线映射为驼峰-- setting namemapUnderscoreToCamelCase valuetrue/
/settings
!--Emp getEmpByEmpId(Param(empId) Integer empId);--
select idgetEmpByEmpId resultTypeempselect * from emp where emp_id #{empId}
/select
⑶使用resutlMap自定义映射处理
select idgetEmpByEmpId resultMapempResultMapselect * from emp where emp_id #{empId}
/selectresultMap idempResultMap typeempid propertyempId columnemp_id/idresult propertyempName columnemp_name/resultresult propertyage columnage/resultresult propertysex columnsex/result
/resultMap
1.2一对一映射处理
1、级联方式处理
/**
* 根据id查询人员信息
* param id
* return*/ Person findPersonById(Param(id) Integer id);!-- Person findPersonById(Integer id);-- select idfindPersonById resultMapIdCardWithPersonResultSELECT person.*,idcard.codeFROM person,idcardWHERE person.card_ididcard.id AND person.id#{id} /selectresultMap idIdCardWithPersonResult typepersonid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertysex columnsex/result result propertycard.id columnid/resultresult propertycard.code columncode/result
/resultMapTest
public void testFindPersonById(){Person person personMapper.findPersonById(2);System.out.println(person);
}
2、Association
resultMap idIdCardWithPersonResult2 typepersonid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertysex columnsex/result!--association 一对一多对一--association propertycard javaTypeIdCardid propertyid columnid/idresult propertycode columncode/result/association/resultMap
3、分步查询
!--分步查询第一步--
!-- Person findPersonById3(Param(id) Integer id);--select idfindPersonById3 resultMapIdCardWithPersonResult3select * from person where id#{id}
/selectresultMap idIdCardWithPersonResult3 typepersonid propertyid columnid/idresult propertyname columnname/resultresult propertyage columnage/resultresult propertysex columnsex/result association propertycard javaTypeIdCard columncard_id selectcom.qcby.mybatis.mapper.IdCardMapper.findCodeById /association
/resultMap
!--分步查询的第二步--
!--IdCard findCodeById(Param(id) Integer id);--select idfindCodeById resultTypeidcard SELECT * from idcard where id#{id} /select
1.3多对一映射处理
场景模拟
查询员工信息以及员工所对应的部门信息
使用resultMap自定义映射处理处理多对一的映射关系
1.级联方式处理
/*** 获取员工以及所对应的部门信息* param empId* return*/
Emp getEmpAndDeptByEmpId(Param(empId) Integer empId);select idgetEmpAndDeptByEmpId resultMapempAndDeptResultMapselect emp.*,dept.*from empleft join depton emp.dept_iddept.dept_idwhere emp.emp_id#{empId}
/select//此处注意要先写column,在写property
resultMap idempAndDeptResultMap typeempid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columnsex propertysex/resultresult columndept_id propertydept.deptId/resultresult columndept_name propertydept.deptName/result
/resultMapTest
public void testGetEmpAndDeptByEmpId(){Emp emp empMapper.getEmpAndDeptByEmpId(1);System.out.println(emp);
} 2.association
resultMap idempAndDeptResultMap typeempid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columnsex propertysex/resultassociation propertydept javaTypedeptid columndept_id propertydeptId/result columndept_name propertydeptName//association
/resultMap
3.分步查询
/*** 通过分步查询来查询员工以及所对应的部门信息的第一步* param empId* return*/
Emp getEmpAndDeptByStepOne(Param(empId) Integer empId);select idgetEmpAndDeptByStepOne resultMapempAndDeptResultMap2select * from emp where emp_id #{empId}
/select
resultMap idempAndDeptResultMap2 typeempid columnemp_id propertyempId/idresult columnemp_name propertyempName/resultresult columnage propertyage/resultresult columnsex propertysex/resultassociation propertydept columndept_idselectcom.qc.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo/association
/resultMap
/*** 通过分步查询来查询员工以及所对应的部门信息的第二步* param deptId* return*/
Dept getEmpAndDeptByStepTwo(Param(deptId) Integer deptId);!--Dept getEmpAndDeptByStepTwo(Param(deptId) Integer deptId);--
select idgetEmpAndDeptByStepTwo resultTypedeptselect * from dept where dept_id${deptId}
/select
测试
Test
public void testGetEmpAndDeptByStepOne(){Emp emp empMapper.getEmpAndDeptByStepOne(3);System.out.println(emp);
}
分步查询的优点可以实现延迟加载(懒加载)但是必须在核心配置文件中设置全局配置信息
lazyLoadingEnabled延迟加载的全局开关。当开启时所有管理对象都会延迟加载
aggressiveLazyLoading当开启时任何方法的调用都会加载该对象的所有属性。否则每个属性会按需加载此时就可以实现按需加载获取的数据是什么就会执行相应的sql语句
此时可以通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载fetchType“lazy(延迟加载)|eager(立即加载)”
1.4一对多映射处理
没有级联方式的查询只有collection 和分步查询
8.4.1 collection
dept接口
/*** 查询部门以及部门中的员工信息* param deptId* return*/Dept getDeptAndEmpByDeptId(Param(deptId) Integer deptId);
dept映射文件中
!--Dept getDeptAndEmpByDeptId(Param(deptId) Integer deptId);-- select idgetDeptAndEmpByDeptId resultMapdeptAndEmpResultMapSELECT * FROM dept LEFT JOIN emp ON dept.dept_idemp.dept_id WHERE dept.dept_id#{deptId} /selectresultMap 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 columnsex propertysex/result/collection
/resultMap
测试方法
Test public void testGetDeptAndEmpByDeptId(){Dept dept deptMapper.getDeptAndEmpByDeptId(1);System.out.println(dept);
}
1.4.2分步查询
员工表设计 部门表设计 ⑴查询部门信息
/*** 通过分步查询进行查询部门及部门中的员工信息的第一步查询部门信息* param deptId* return
*/ Dept getDeptAndEmpBySetpOne(Param(deptId) Integer deptId);!-- Dept getDeptAndEmpBySetpOne(Param(deptId) Integer deptId);--select idgetDeptAndEmpBySetpOne resultMapdeptAndEmpResultMapByStepselect * from dept where dept_id #{deptId} /selectresultMap iddeptAndEmpResultMapByStep typedeptid columndept_id propertydeptId/idresult columndept_name propertydeptName/resultcollection propertyemps columndept_id selectcom.qcby.mybatis.mapper.EmpMapper.getDeptAndEmpBySetpTwo
/collection
/resultMap
⑵查询员工信息
/*** 通过分步查询进行查询部门及部门中的员工信息的第二步查询员工信息* param deptId* return
*/
ListEmp getDeptAndEmpBySetpTwo(Param(deptId)Integer deptId);!-- ListEmp getDeptAndEmpBySetpTwo(Param(deptId)Integer deptId);--select idgetDeptAndEmpBySetpTwo resultTypeempselect * from emp where dept_id #{deptId} /select
⑶测试方法
Testpublic void testGetDeptAndEmpBySetp(){Dept dept deptMapper.getDeptAndEmpBySetpOne(2);System.out.println(dept);
}
1.5多对多映射关系
商品和订单两者之间的关系 一种商品存在多个订单中、一个订单存在多个商品
创建一个中间表来描述两者的关联关系
商品的表结构 订单的表结构 中间表 1.5.3 分步查询
⑴查询订单信息
/*** 通过分步查询进行查询订单以及订单中的商品信息的第一步* param id
* return*/ ListOrders findOrdersWithProduct2(Param(id) Integer id);
!-- ListOrders findOrdersWithProduct2(Param(id) Integer id);--select idfindOrdersWithProduct2 resultMapOrdersWithProductResult2select * from orders where id #{id} /selectresultMap idOrdersWithProductResult2 typeordersid columnid propertyid/idresult columnnumber propertynumber/resultcollection propertyproductList columnid ofTypeproduct selectcom.qcby.mybatis.mapper.ProductMapper.findProductById /collection
/resultMap
⑵查询商品信息
/*** 通过分步查询进行查询订单以及订单中的商品信息的第二步* param id* return */ListProduct findProductById(Param(id) Integer id);!--ListProduct findProductById(Param(id) Integer id);--select idfindProductById resultTypeproductselect * from product where id in(select product_id from ordersitem where orders_id #{id} )
/select
⑶测试
Testpublic void testFindOrdersWithProduct2(){ ListOrders orders ordersMapper.findOrdersWithProduct2(1); orders.forEach(System.out::println);
}