Mybatis映射文件(2)

野性酷女 2022-05-24 03:26 278阅读 0赞
Select元素
  1. <select id="selectById" parameterType="Integer" resultType="employee">
  2. SELECT * FROM t_employee WHERE id = #{id}
  3. </select>

id:唯一标识符

parameterType:参数类型

resultType:返回值类型

  1. <!--resultType:如果返回的是一个集合,那么要写集合中元素的类型-->
  2. <select id="getEmpsByLastNameLike" resultType="employee">
  3. SELECT * FROM t_employee WHERE last_name LIKE #{lastName}
  4. </select>

返回一条记录的map:key就是列名,值就是对应的值

  1. <select id="getEmpByIDReturnMap" resultType="Map">
  2. SELECT * FROM t_employee WHERE id=#{id}
  3. </select>

多条记录封装一个map:Map:键是这条记录的主键,值是封装后的javaBean

  1. //多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是封装后的javaBean
  2. //告诉mybatis封装这个map的时候使用哪个属性作为map的key
  3. @MapKey("id")
  4. Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);
  5. <select id="getEmpByLastNameLikeReturnMap" resultType="employee">
  6. SELECT * FROM t_employee WHERE last_name LIKE #{lastName}
  7. </select>

查出数据封装成什么类型?——列名和属性名不一样

1、别名

2、复合驼峰命名法,则在全局配置文件中开启驼峰命名法

3、resultMap自定义对应

自动封装(映射):

※在全局设置中加上autoMappingBehavior——》开启自动映射

※开启驼峰命名规则映射功能 :mapUnderscoreToCamelCase=true

更多更复杂的则需要自定义resultMap实现高级结果集映射功能。

resultMap

1、resultMap单表

  1. <mapper namespace="dao.EmployeeMapperPlus">
  2. <!--
  3. 自定义某个javaBean封装(映射)规则
  4. id:唯一id方便后面引用
  5. type:自定义规则的Java类型
  6. -->
  7. <resultMap id="emp" type="bean.Employee">
  8. <!--
  9. id定义主键mybatis会在底层有优化
  10. 指定主键列的封装规则
  11. column:指定那一列(数据库字段)
  12. property:指定对应的JavaBean的属性
  13. -->
  14. <id column="id" property="id"/>
  15. <!--定义普通列封装-->
  16. <result column="last_name" property="lastName"/>
  17. <!--其他不指定的列也会封装,但是推荐我们只要写resultMap就把所有的映射规则都写上-->
  18. <result column="email" property="email"/>
  19. <result column="gender" property="gender"/>
  20. </resultMap>
  21. <!--
  22. resultType和resultMap只能二选一
  23. resultMap:自定义结果集映射规则
  24. -->
  25. <select id="getEmpById" resultMap="emp">
  26. SELECT * FROM t_employee WHERE id=#{id}
  27. </select>
  28. </mapper>

2、resultMap多表级联

建立了Department实体类

  1. <!--
  2. 场景1:
  3. 查询Employee的同时查询员工对应的部门
  4. Employee==Department
  5. -->
  6. <!--联合查询使用级联属性封装结果(映射结果)-->
  7. <resultMap id="empAnddept1" type="bean.Employee">
  8. <id column="id" property="id"/>
  9. <result column="last_name" property="lastName"/>
  10. <result column="email" property="email"/>
  11. <result column="gender" property="gender"/>
  12. <result column="d_id" property="department.id"/>
  13. <result column="d_name" property="department.name"/>
  14. </resultMap>
  15. <!--使用association定义关联的单个对象的封装规则-->
  16. <resultMap id="empAnddept2" type="bean.Employee">
  17. <id column="id" property="id"/>
  18. <result column="last_name" property="lastName"/>
  19. <result column="email" property="email"/>
  20. <result column="gender" property="gender"/>
  21. <!--
  22. association可以指定联合的JavaBean对象
  23. property="department"指定那个对象是需要联合的对象
  24. javaType="Department"指定这个属性对象的类型【不能省略】
  25. -->
  26. <association property="department" javaType="bean.Department">
  27. <id column="id" property="id"/>
  28. <result column="d_name" property="name"/>
  29. </association>
  30. </resultMap>
  31. <select id="getEmployeeAndDepartment" resultMap="empAnddept2">
  32. SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,
  33. e.d_id d_id,d.id did,d.d_name d_name
  34. FROM t_employee e,t_department d
  35. WHERE e.d_id=d.id AND e.id=#{id}
  36. </select>

还有一种方法就是分开查询,分别建立员工和部门的借口和映射文件,最后将分别查出的结果合在一起。

  1. <!--
  2. 使用association进行分步查询
  3. 1、先按照员工id查出员工信息
  4. 2、根据员工信息中的d_id值去部门表查出部门信息
  5. 3、部门设置到员工中
  6. -->
  7. <resultMap id="empAnddept3" type="bean.Employee">
  8. <id column="id" property="id"/>
  9. <result column="last_name" property="lastName"/>
  10. <result column="email" property="email"/>
  11. <result column="gender" property="gender"/>
  12. <!--
  13. association定义关联对象的封装规则
  14. select:表明当前属性是调用select指定方法查出来的结果
  15. column;指定将那一列的值传给这个方法
  16. 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,封装给property指定的属性
  17. -->
  18. <association property="department" select="dao.DepartmentMapper.getDepartmentById" column="d_id"></association>
  19. </resultMap>
  20. <select id="getEmployeeByIdStep" resultMap="empAnddept3">
  21. SELECT * FROM t_employee WHERE id=#{id}
  22. </select>
  23. <!--
  24. 分步查询可以使用延迟加载:
  25. Employee==》DEpartment
  26. 我们每次查询Employee的时候都将包括Department的所有信息查询出来,比较耗费数据库
  27. 我们希望在使用部门信息的时候再去查询
  28. 分步查询的基础上加上两个配置
  29. 在全局配置文件中:
  30. 延迟加载功能设置
  31. <setting name="lazyLoadingEnabled" value="true"></setting>
  32. <setting name="aggressiveLazyLoading" value="false"></setting>
  33. -->

3、resultMap处理集合

  1. <!--collection嵌套结果集的方式,定义关联的集合类型元素的封装规则-->
  2. <resultMap id="dept1" type="bean.Department">
  3. <id column="id" property="id"/>
  4. <result column="d_name" property="name"/>
  5. <!--
  6. collection定义关联集合类型的属性封装
  7. ofType:指定集合里面元素的类型
  8. -->
  9. <collection property="employee" ofType="bean.Employee">
  10. <!--定义集合中元素的封装规则-->
  11. <id column="id" property="id"/>
  12. <result column="last_name" property="lastName"/>
  13. <result column="email" property="email"/>
  14. <result column="gender" property="gender"/>
  15. </collection>
  16. </resultMap>
  17. <select id="getDepartmentAndEmpsById" resultMap="dept1">
  18. SELECT d.id id,d.d_name d_name,e.id eid,e.last_name last_name,e.email email,e.gender gender
  19. FROM t_department d LEFT JOIN t_employee e ON d.id=e.d_id
  20. WHERE d.id=#{id}
  21. </select>

在集合类型中使用分步和延迟加载

  1. <resultMap id="dept2" type="bean.Department">
  2. <id column="id" property="id"/>
  3. <result column="d_name" property="name"/>
  4. <!--将查询出来的id传给getEmpsByDeptId方法-->
  5. <collection property="employee"
  6. select="dao.EmployeeMapperPlus.getEmpsByDeptId" column="id"></collection>
  7. </resultMap>
  8. <select id="getDepartmentAndEmpByIdSteps" resultMap="dept2">
  9. SELECT * FROM t_department WHERE id=#{id}
  10. </select>

懒加载直接在全局设置中开启或者使用fetchType

  1. <resultMap id="dept2" type="bean.Department">
  2. <id column="id" property="id"/>
  3. <result column="d_name" property="name"/>
  4. <!--
  5. 将查询出来的id传给getEmpsByDeptId方法
  6. fetchType="lazy":表示使用延迟加载
  7. fetchType="eager":表示立即使用
  8. -->
  9. <collection property="employee"
  10. select="dao.EmployeeMapperPlus.getEmpsByDeptId" column="id" fetchType="eager"></collection>
  11. <!--
  12. 如果需要将多列的值传递给getEmpsByDeptId方法
  13. 将多列的值封装成map传递
  14. column="{key1=column1,key2=column2}"
  15. -->
  16. <!--<collection property="employee"
  17. select="dao.EmployeeMapperPlus.getEmpsByDeptId" column="{id=id,name=name}"></collection>-->
  18. </resultMap>
  19. <select id="getDepartmentAndEmpByIdSteps" resultMap="dept2">
  20. SELECT * FROM t_department WHERE id=#{id}
  21. </select>

鉴别器:

  1. <!--
  2. <discriminator javaType="">
  3. <case value=""></case>
  4. </discriminator>
  5. 鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装的行为
  6. 封装Employee:
  7. 如果查出的是女生,就把部门信息查询出来,否则不查询;
  8. 如果是男生,就把last_name一列的值赋值给email
  9. -->
  10. <resultMap id="empAnddept4" type="bean.Employee">
  11. <id column="id" property="id"/>
  12. <result column="last_name" property="lastName"/>
  13. <result column="email" property="email"/>
  14. <result column="gender" property="gender"/>
  15. <!--
  16. column:指定判定的列名
  17. javaType:列值对应的Java类型
  18. -->
  19. <discriminator javaType="String" column="gender">
  20. <!--0代表女生 resultType指定封装的,不能缺少-->
  21. <case value="0" resultType="bean.Employee">
  22. <association property="department" select="dao.DepartmentMapper.getDepartmentById" column="d_id"></association>
  23. </case>
  24. <!--1代表男生, 如果是男生,就把last_name一列的值赋值给email-->
  25. <case value="1" resultType="bean.Employee">
  26. <id column="id" property="id"/>
  27. <result column="last_name" property="lastName"/>
  28. <result column="last_name" property="email"/>
  29. <result column="gender" property="gender"/>
  30. </case>
  31. </discriminator>
  32. </resultMap>

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis-映射文件

    Mybatis-映射文件(二) Mybatis的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器XML文件就显得相对简单。如果拿他跟具有相同功能的JD