Mybatis单表自动映射;使用<resultMap>设置映射结果集;多表查询

本文介绍了MyBatis中不同场景下的映射配置方法,包括基本的自动映射、复杂情况下的resultMap设置,以及一对多和一对一关系的处理。

1. 自动映射【数据库字段名与实体类的属性名一致】

    <select id="selectList" resultType="cn.bjsxt.pojo.User">

       select id,name,pwd,age from

       t_user

    </select>

 

2. 使用resultMap设置映射结果集【数据库字段名与实体类的属性名不一致】

<!-- resultMap:定义结果集映射 id:代表结果集的唯一标记 type:结果集的类型,类的全路径名,或者别名 -->

     <resultMap type="cn.bjsxt.pojo.User" id="userone">

         <!-- id:用于设置主键字段于实体类属性的映射关系 -->

         <id property="id" column="id" />

         <!-- result:用于设置普通字段与实体类属性的映射关系-->

         <result property="uname" column="name" />

         <result property="pwd" column="pwd" />

         <result property="age" column="age" />

     </resultMap>

     <select id="selectList" resultMap="userone">

         select id,name, pwd,age from

         t_user

     </select>

 

3. Mybatis查询方式

a)  一对一关系, 查询学生所在的班级。

第一种方式

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace:SQL标签命名空间 -->

<mapper namespace="cn.bjsxt.mapper.StudentMapper">

     <select id="selectList" resultType="cn.bjsxt.pojo.Student">

     SELECT s.id,s.name ,s.gender ,s.age ,c.id as 'classes.cid',c.name as

     'classes.cname',c.beginTime as 'classes.beginTime' from t_student s

     LEFT JOIN t_classes c ON c.id = s.cid

     </select>

</mapper>


第二种方式                  

<resultMap type="cn.bjsxt.pojo.Student" id="stuMap">

         <id property="id" column="id"/>

         <result property="name" column="name"/>

         <result property="gender" column="gender"/>

         <result property="age" column="age"/>

         <!-- 映射的实体类

              property:关系数据属性名

              javaType:关系数据属性类型

         -->

         <association property="classes" javaType="cn.bjsxt.pojo.Classes">

              <id property="cid" column="id"/>

              <result property="cname" column="name"/>

              <result property="beginTime" column="beginTime"/>

         </association>

     </resultMap>

     <select id="selectList" resultMap="stuMap">

     SELECT s.id,s.name ,s.gender ,s.age, c.id,c.name,c.beginTime from t_student s LEFT JOIN t_classes c ON c.id = s.cid

     </select>


b)  一对多关系:查询所有班级,并查询班级中所有学生集合

i.    一次访问数据库的方式

<!-- 1对多关系 -->

     <resultMap type="cn.bjsxt.pojo.Classes" id="clsMap">

         <id property="cid" column="cid"/>

         <result property="cname" column="cname"/>

         <result property="beginTime" column="beginTime"/>

         <!-- 定义集合关系

              property:关系属性名

              javaType:关系对象属性:list:ArrayList

              ofType:集合的泛型

         -->

              <collection property="students" javaType="java.util.ArrayList"

                   ofType="cn.bjsxt.pojo.Student">

                   <id property="id" column="id"/>

                   <result property="name" column="name"/>

                   <result property="gender" column="gender"/>

                   <result property="age" column="age"/>

              </collection>

     </resultMap>

    

     <select id="selectList" resultMap="clsMap">

         SELECT s.id,s.name ,s.gender ,s.age, c.id as cid ,c.name as cname,c.beginTime from t_student s right JOIN t_classes c ON c.id = s.cid

     </select>

注意:id冲突问题

 

c)    N+1次访问数据方式

<!-- 多次访问数据库 -->

     <resultMap type="cn.bjsxt.pojo.Classes" id="clsMap1">

         <id property="cid" column="id"/>

         <result property="cname" column="name"/>

         <result property="beginTime" column="beginTime"/>

         <!-- 定义集合关系

              如何查询班级中的学生

              添加查询idselect

              添加id值:column

         -->

              <collection property="students" javaType="java.util.ArrayList"

                   ofType="cn.bjsxt.pojo.Student" select="selStu" column="id">

              </collection>

     </resultMap>

     <!-- 查询班级信息 -->

     <select id="selectList1" resultMap="clsMap1">

         select id , name , begintime from t_classes

     </select>

     <!-- 查询学生信息 -->

     <select id="selStu" resultType="cn.bjsxt.pojo.Student">

         select * from t_student where cid = #{id}

     </select>


### `<sql>` 元素 #### 作用 `<sql>` 元素主要用于定义可重用的 SQL 片段。在复杂的 SQL 语句中,可能会有一些重复使用的部分,如查询字段、条件语句等。使用 `<sql>` 元素可以将这些重复的部分提取出来,方便复用,提高 SQL 语句的可维护性和可读性。例如,在查询中都需要查询相同的字段,就可以将这些字段定义在一个 `<sql>` 元素中,然后在需要的地方引用。 #### 配置原理 配置 `<sql>` 元素时,需要为其指定一个唯一的 `id`,用于后续引用。在 SQL 语句中,可以使用 `<include>` 元素来引用定义好的 `<sql>` 片段。示例代码如下: ```xml <sql id="userColumns"> id, username, password, email </sql> <select id="selectUserById" resultType="User"> SELECT <include refid="userColumns"/> FROM users WHERE id = #{id} </select> ``` 在上述代码中,`<sql>` 元素定义了查询用户的字段,在 `<select>` 语句中使用 `<include>` 元素引用了这个片段。 ### `<resultMap>` 元素 #### 作用 MyBatis 通常可以自动查询结果和 POJO 实体类进行映射,但前提是数据库的字段名和 POJO 的属性名相同。然而,实际开发中,数据库命名和 Java 命名规范可能不同,如数据库使用下划线命名,Java 使用驼峰命名法;或者数据和实体类的创建者不同,命名差异更大,此时 MyBatis 无法自动映射,就需要使用 `<resultMap>` 元素手动配置映射规则 [^1]。 #### 配置原理 配置 `<resultMap>` 元素时,需要为其指定一个唯一的 `id` 和要映射的实体类的类名。在 `<resultMap>` 内部,使用 `<id>` 元素来映射主键字段,使用 `<result>` 元素来映射普通字段。示例代码如下: ```xml <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="userName" column="user_name"/> <result property="password" column="password"/> <result property="email" column="email"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT * FROM users WHERE id = #{id} </select> ``` 在上述代码中,`<resultMap>` 元素定义了 `User` 实体类和 `users` 字段的映射关系,在 `<select>` 语句中使用 `resultMap` 属性引用了这个映射规则。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值