mybatis中resultMap简单使用以及关联查询

1. resulttype和resultMap

当查询接口需要查询数据库时,通常我们会封装查询的数据库数据映射到实体类中,result type类型为自定义的实体类,当实体类的属性和数据库的字段不是一一对应时,就需要我们手动配置resultMap

2. resultMap简单使用

2.1 实体类

public class Class {

   private  Integer id;

   private String classname;

}

2.2 数据库字段

在这里插入图片描述
可以看出数据库中的字段名为name,实体类的字段名为classname

2.3 controller层

 @GetMapping("/findClass/{id}")
    public Class test1(@PathVariable int id){


        return  resultMapDao.selectClass(id);


    }

2.4 dao层

lass selectClass(int id);

2.5 mapper层

<select id="selectClass" parameterType="int" resultMap="TestMap">
        select * from class where id=#{id}
   </select>
    <resultMap id="TestMap" type="com.example.demo.Class">
    <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
        <!-- property:主键在pojo中的属性名 -->
        <!-- column:主键在数据库中的列名 -->
        <id property="id" column="ID"/>
		 <!-- 定义普通属性 -->
        <result property="classname" column="name"/>

    </resultMap>

结果

{
	"id": 1,
	"classname": "高一一班"
}

3. 使用resultMap进行关联查询

3.1 一对一查询

一个班级只能有一个班主任

3.1.1 实体类

班级类

public class Class {

   private  Integer id;

   private String classname;

   private Teacher teacher;

}

教师类

public class Teacher {

   private  Integer id;

   private String teachername;

   private  Integer classid;

}

3.1.2 数据库字段

教师表
在这里插入图片描述

3.1.3 mapper 层
<select id="selectClass" parameterType="int" resultMap="TestMap">
        select *
        from class c
                 left join teacher t on t.classid = c.id
        where c.id = #{id}   </select>
    <resultMap id="TestMap" type="com.example.demo.Class">
        <id property="id" column="ID"/>
        <result property="classname" column="name"/>

        <!-- association :配置一对一属性 -->
        <!-- property:Teacher里面的teacher属性名 -->
        <!-- javaType:属性类型 -->
        <!-- column:那个字段作为条件 -->
        <association property="teacher" javaType="com.example.demo.Teacher" column="id">
            <!-- id:声明主键,表示ID是关联查询对象的唯一标识-->
            <id property="id" column="ID"/>
            <result property="teachername" column="tname"/>
            <result property="classid" column="classid"/>
        </association>


    </resultMap>
3.1.4 查询结果
{
	"id": 1,
	"classname": "高一一班",
	"teacher": {
		"id": 1,
		"teachername": "小王老师",
		"classid": 1
	}
}

3.2 一对多查询

一个班级有多个学生

3.2.1 修改实体类

班级类

public class Class {

   private  Integer id;

   private String classname;

//   private Teacher teacher;
   private List<Class> aclass;

}

学生类

public class Student {

   private  Integer id;

   private String name;

   private Integer classid;

}
3.2.2 修改mapper
  <select id="selectClass" parameterType="int" resultMap="TestMap">
        select *
        from class c
                 left join student s on s.classid = c.id
        where c.id = #{id}   </select>
    <resultMap id="TestMap" type="com.example.demo.Class">
        <id property="id" column="ID"/>
        <result property="classname" column="name"/>

        <!-- association :配置一对一属性 -->
        <!-- property:Class里面的aclass属性名 -->
        <!-- javaType:属性类型 -->
        <collection property="aclass" javaType="java.util.ArrayList" ofType="com.example.demo.Student">
            <!-- 配置主键,是关联Order的唯一标识 -->
            <id property="id" column="sid"/>
            <result property="name" column="cname"/>
            <result property="classid" column="classid"/>
        </collection>


    </resultMap>
3.2.3 结果
{
	"id": 1,
	"classname": "高一一班",
	"aclass": [
		{
			"id": 1,
			"name": "刘翔",
			"classid": 1
		},
		{
			"id": 2,
			"name": "苏炳添",
			"classid": 1
		}
	]
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一种基于XML文件配置的持久化框架,其resultMap是其的一个非常重要的组件之一。简单来说,就是将查询结果映射成一个Java对象,以便于程序员的使用和维护。下面就来详细了解一下MyBatisresultMap。 1. resultMap的定义 在MyBatis的Mapper XML文件,可以通过定义resultMap来映射查询结果。具体的定义如下: ``` <resultMap id="resultMapName" type="resultType"> <!-- 这里定义映射关系 --> </resultMap> ``` 其,id为resultMap的名字,type为结果类型,也就是结果对象所在的类的全限定名。这里需要注意,resultType和type只能设置一个。 2. resultMap的映射关系 在resultMap,映射关系定义使用的是result标签,具体定义如下: ``` <result property="propertyName" column="columnName" javaType="javaType" jdbcType="jdbcType" typeHandler="typeHandler" /> ``` 其,property为结果对象的属性名,column为查询语句的列名,javaType为该属性的Java类型,jdbcType为该属性在数据库的类型,typeHandler为类型转换器。需要注意的是,column、javaType、jdbcType和typeHandler是可选的,如果没有指定,则MyBatis会自动根据属性名和列名进行简单的映射。 3. 关联关系映射 在实际开发,经常会涉及到多张表之间的关联查询,此时需要用到MyBatis关联映射。具体定义如下: ``` <association property="propertyName" resultMap="resultMapName" /> ``` 其,propertyName为当前结果对象用来保存关联对象的属性名,resultMapName为该关联对象所用的resultMap的名字。 4. 集合关联映射 除了单个对象之外,有时候还需要将查询结果映射成一个包含多个对象的集合。此时需要用到MyBatis的集合关联映射。具体定义如下: ``` <collection property="propertyName" resultMap="resultMapName" /> ``` 其,propertyName为当前结果对象用来保存集合的属性名,resultMapName为该集合元素所用的resultMap的名字。 5. resultMap使用 在映射结果时,需要在查询语句使用resultMap,具体定义如下: ``` <select id="selectUser" parameterType="int" resultMap="resultMapName"> select id, username, password from user where id = #{id} </select> ``` 其resultMapName为映射结果所用的resultMap的名字。 综上所述,MyBatisresultMap是一个非常重要的组件,能够实现查询结果的映射和关联关系的处理,大大提高了程序员的开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值