ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>### 1.属性名和数据库中的列名不同时,可以使用别名查询 ~~~ 例如列名是name 而属性名是e_name <select id="getAllEmp" resultType="cn.li.pojo.Employee"> select id,name e_name,gender,address from employee </select> ~~~ >#### 属性名和数据库中的列名不同时或者另一种方法: ~~~ <!-- 自定义封装规则 。resultMap="" column 表示数据库中查询的字段名称 。 --> <resultMap type="cn.li.pojo.Employee" id="rm2"> <!-- 使用id标签 来指定主键的封装规则 ,mybatis会做优化 。 --> <id column="id" property="id"></id> <result column="name" property="e_name" /> <result column="gender" property="gender" /> <result column="address" property="address" /> </resultMap> <select id="getAllEmp2" resultMap="rm2"> select id,name,gender,address from employee </select> ~~~ >### 2.级联封装 ~~~ <resultMap type="cn.li.pojo.Employee" id="rm3"> <id column="id" property="id" /> <result column="name" property="e_name" /> <result column="gender" property="gender" /> <result column="address" property="address" /> <result column="d_id" property="dept.dId" /> <result column="d_name" property="dept.dName" /> </resultMap> <select id="sel1" resultMap="rm3"> select e.*,d.* from employee e left join department d on e.d_id=d.d_id </select> ~~~ >### 3.使用联合标签配置,添加property,javaType属性 ~~~ <resultMap type="cn.li.pojo.Employee" id="rm4"> <id column="id" property="id" /> <result column="name" property="e_name" /> <result column="gender" property="gender" /> <result column="address" property="address" /> <!-- 联合 。 association 指向的是 实体类中的那个属性 需要做联合 配置。 --> <association property="dept" javaType="cn.li.pojo.Department"> <id column="d_id" property="dId" /> <result column="d_name" property="dName" /> </association> </resultMap> <select id="sel2" resultMap="rm4"> select e.*,d.* from employee e left join department d on e.d_id=d.d_id </select> public class Employee{ ... private Department dept; .... } ~~~ >### 4.分步查询&延迟加载 ~~~ <!-- com/igeek/pojo/EmployeeMapper.xml 分布查询 。。。 1.知道员工的id ,根据员工的id能拿到部门id . 2.再根据得到部门id,去查询部门所有信息。 --> <resultMap type="cn.li.pojo.Employee" id="selStep"> <id column="id" property="id" /> <result column="name" property="e_name" /> <result column="gender" property="gender" /> <result column="address" property="address" /> <!-- association 联合 property 要为实体类中的哪一个属性进行赋值。 select 表示当前的这个属性值 是通过查询拿到的 。(通过那个命名空间下的那一个标签去做查询 。) column 表示 第一次查询的哪一个列的结果作为参数传递给你第二次要调用的方法。 步骤1 : id name gender address d_id 1 张全蛋 1 暴走大事件 ( 1 ) 步骤2 : d_id dId,d_name dName ( 1 ) 开发 。 多个参数的分步查询 。 #{id},#{name},#{age} column="{name=d_name,id=d_id}"; 只有分步查询可以做延迟加载 懒加载 按需加载 。 --> <!-- select部门映射文件的namespace.标签id --> <association property="dept" select="cn.li.dao.DepartmentDao.selDepartmentByID" column="d_id"><!-- column表示将查询中那一列的结果作为参数传递给另外一个查询 --> </association> </resultMap> <select id="sel3" resultMap="selStep"> select * from employee where id=#{id} </select> //DepartmentMapper.xml文件里的内容 <select id="selDepartmentByID" resultType="cn.li.pojo.Department"> select d_id dId,d_name dName from department where d_id=#{id} </select> //开启延迟加载 只需要在配置文件中进行全局配置(两个值。) //延迟加载,又叫懒加载,按需加载 lazyLoadingEnable设置为true ,aggressiveLazyLoading 设置为false ~~~ 开启懒加载步骤: 1.导入三个jar包 ![](https://box.kancloud.cn/6b1a32759a2578e4d63a7ba4743f7489_202x62.png) 2.在全局配置文件里配置 也就是config.xml文件里 ~~~ <settings> <setting name="lazyLoadingEnabled" value="true"></setting> <setting name="aggressiveLazyLoading" value="false"></setting> </settings> ~~~ >### 5.使用collection查询 ~~~ //一对多 查询部门下的所有的员工信息。 <resultMap type="cn.li.pojo.Department" id="selMap2"> <id column="d_id" property="dId"/> <result column="d_name" property="dName"/> <!-- collection定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型 --> <collection property="emps" ofType="cn.li.pojo.Employee"> <id column="id" property="id"/> <result column="name" property="e_name"/> <result column="gender" property="gender"/> <result column="address" property="address"/> </collection> </resultMap> <select id="selDep" resultMap="selMap2"> select e.*,d.* from employee e left join department d on e.d_id=d.d_id where d.d_id=#{id} </select> public class Department{ ... private List<Employee> emps; ... } ~~~ ~~~