Mybatis相关:ResultMap作用(结果映射,一对多,多对多等)

注:当sql语句需要参数时,可以用parameter指定参数类型,但事实上即使不予指定mybatis也可以自行解析。

       当sql语句是查询时,需要指定resultType,即返回值类型,但增删改是受影响的行数,不需要指定

1.两大作用(配置结果类型): 

  • 解决数据表列名和javabean属性不一致的问题
  • 提供一对一、一对多、多对多等高级映射

2.当实体类对象与数据库字段不匹配时:

  • 方案1:使用sql语句as 起别名的方式修改查询结果的名称
  • 方案2:使用resultMap,完成数据库字段与实体类属性的映射。(可被多个sql语句共用)
<!--
解决列名和实体类属性不匹配方法
1.  select  id,name ,age,sex,height,s_address as address  from  student_tb where id = #{id}
2.  使用 resultmap
     1.声明resultMap标签  在内部完成映射
     2.查询结果使用resultMap="studentMap1" 应用声明的resultMap的id
-->
    <!-- 定义一个 resultMap
    使用resultMap 完成 数据库列名到 实体类的映射
    <id property="id" column="id"></id>  实体类属性与数据表 主键列名 映射
    <result property="name" column="name"></result>  实体类属性与 普通列名 映射
    -->
    <resultMap id="studentMap1" type="com.qfedu.entity.Student">
        <id property="id" column="id"></id>   <!-- 主键-->
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="address" column="s_address"></result>

    </resultMap>

    <select id="findStudentById" resultMap="studentMap1" parameterType="int">
         select  id,name ,age,sex,height,s_address   from  student_tb where id = #{id}
    </select>

3.一对一:

使用一对一查询时,在一个实体类中添加另一实体类属性。用resultMap实现映射关系时,使用association连接,javaType为封装的类型。

如下:在Score中添加Student属性,以及getter和setter方法

映射关系

 <!--定义 resultType  来接收getAllScoreWithStudent2 查询结果-->
    <!--
        一对一查询
       <association 解决一对一问题
       property="student" 对应Score 中 student属性
       column="studentid" 对应Score_tb 与 student_tb关联的外键 列名,可以不写
       javaType="Student"  将其他非Score 中的字段写入Student 类
             <result property="name" column="name"></result> 写入student对应的属性
    -->
    <resultMap id="scoreMap1" type="Score">
        <id column="scoreid" property="scoreid"></id>
        <result column="couresename" property="couresename"></result>
        <result column="score" property="score"></result>
        <result column="studentid" property="studentid"></result>

        <association property="student" column="studentid" javaType="Student" >
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="height" column="height"></result>
            <result property="sAddress" column="s_address"></result>
        </association>
    </resultMap>

    <select id="getAllScoreWithStudent2" resultMap="scoreMap1">
         SELECT a.*,b.* FROM score_tb a  LEFT JOIN student_tb b ON a.studentid = b.id;
    </select>

3.一对多

一对多是在一个类中包含另一个类list的集合,在映射时,使用Collection,ofType为封装类型。注意封装类型与一对一不同

如下:在学生实体中。添加score集合

映射关系:

 <!--
    <collection  解决一对多问题

    property="scoreList"  将collection 组成的成绩集合放置到 student对象中
    column="id"   根据相同 的学生id组成集合
    ofType="Score"  将学生id组成集合 每一个item 生成一个 Score
    -->
    <resultMap id="studentWithScoreMap" type="Student">
        <id column="id" property="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="sAddress" column="s_address"></result>

        <collection property="scoreList" column="id" ofType="Score">
            <id property="scoreid" column="scoreid"></id>
            <result property="couresename" column="couresename"></result>
            <result property="score" column="score"></result>
            <result property="studentid" column="id"></result>
        </collection>

    </resultMap>

    <!-- 查询所有的学生并包含成绩-->
    <select id="findAllStudentWithScore" resultMap="studentWithScoreMap">
        SELECT  * FROM student_tb a LEFT JOIN score_tb b ON a.id = b.studentid
    </select>

4.多对多

表之间的多对多关系稍微复杂,需要一个中间表来表示

中间表有三个字段,其中两个字段分别作为外键指向各自一方的主键,由此通过中间表来表示多对多关系,通过一个表联合另一个中间表可以表示为一对多关系。

多对多,其实可以从一方到另一方表示为一对多关系,反之亦然

student_role_tb :  id,studentid, roieid

如:查找职位,并包含对应的学生信息,一个职位可能有很多学生,如组长,一对多关系

第一步: 在role实体中添加student属性,

第二步:书写映射关系,按一对多即可

 <resultMap id="roleWithStudent" type="Role">
        <id property="roleid" column="roleid"></id>
        <result property="rolename" column="rolename"></result>
<!--property  role表中的属性-->
        <collection property="studentList" column="studentid" ofType="Student"> 
            <id column="id" property="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="height" column="height"></result>
            <result property="sAddress" column="s_address"></result>
        </collection>
    </resultMap>
    <select id="findAllRoleWithStudent" resultMap="roleWithStudent">
        select r.*,sr.studentid, s.* from role_tb r LEFT JOIN student_role_tb sr on r.roleid=sr.roleid  
        LEFT JOIN student_tb s on s.id = sr.studentid
    </select>

反之亦然,如:查找学生并包含职位信息,也是一对多关系

第一步: 在student实体中添加role属性,

第二步:书写映射关系,按一对多即可

<!--多对多中的一对多-->
    <resultMap id="studentWithRole" type="Student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="sAddress" column="s_address"></result>
        <collection property="roleList" column="roleid" ofType="Role">
        <id property="roleid" column="roleid"></id>
            <result property="rolename" column="rolename"></result>
        </collection>
    </resultMap>
    <select id="findAllStudentWithRole" resultMap="studentWithRole">
        select s.*,sr.studentid, r.* from student_tb s LEFT JOIN student_role_tb sr on s.id = sr.studentid
          LEFT JOIN role_tb r on r.roleid = sr.roleid;
    </select>

 

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 1 Mybatis入门 1 1.1 单独使用jdbc编程问题总结 1 1.1.1 jdbc程序 1 1.1.2 jdbc编程步骤: 2 1.1.3 jdbc问题总结如下: 3 1.2 MyBatis介绍 3 1.3 Mybatis架构 3 1.4 mybatis下载 4 1.5 创建mysql数据库 5 1.6 Mybatis入门程序 5 1.6.1 需求 5 1.6.2 第一步:创建java工程 6 1.6.3 第二步:加入jar包 6 1.6.4 第三步:log4j.properties 6 1.6.5 第四步:SqlMapConfig.xml 6 1.6.6 第五步:po类 7 1.6.7 第六步:程序编写 8 1.6.8 Mybatis解决jdbc编程的问题 15 1.6.9 与hibernate不同 16 2 Dao开发方法 16 2.1 需求 16 2.2 SqlSession的使用范围 17 2.2.1 SqlSessionFactoryBuilder 17 2.2.2 SqlSessionFactory 17 2.2.3 SqlSession 17 2.3 原始Dao开发方式 18 2.3.1 映射文件 18 2.3.2 Dao接口 19 2.3.3 问题 20 2.4 Mapper动态代理方式 20 2.4.1 实现原理 20 2.4.2 Mapper.xml(映射文件) 20 2.4.3 Mapper.java(接口文件) 21 2.4.4 加载UserMapper.xml文件 22 2.4.5 测试 22 2.4.6 总结 23 3 SqlMapConfig.xml配置文件 24 3.1 配置内容 24 3.2 properties(属性) 24 3.3 settings(配置) 25 3.4 typeAliases(类型别名) 26 3.4.1 mybatis支持别名: 26 3.4.2 自定义别名: 27 3.5 typeHandlers(类型处理器) 27 3.6 mappers(映射器) 28 3.6.1 <mapper resource=" " /> 28 3.6.2 <mapper url=" " /> 28 3.6.3 <mapper class=" " /> 29 3.6.4 <package name=""/> 29 4 Mapper.xml映射文件 29 4.1 parameterType(输入类型) 29 4.1.1 #{}与${} 29 4.1.2 传递简单类型 30 4.1.3 传递pojo对象 30 4.1.4 传递pojo包装对象 31 4.1.5 传递hashmap 32 4.2 resultType(输出类型) 33 4.2.1 输出简单类型 33 4.2.2 输出pojo对象 34 4.2.3 输出pojo列表 34 4.2.4 resultType总结: 35 4.2.5 输出hashmap 35 4.3 resultMap 36 4.3.1 Mapper.xml定义 36 4.3.2 定义resultMap 36 4.3.3 Mapper接口定义 37 4.4 动态sql(重点) 37 4.4.1 If 37 4.4.2 Where 38 4.4.3 foreach 38 4.4.4 Sql片段 43 5 关联查询 44 5.1 商品订单数据模型 45 5.2 一对一查询 45 5.2.1 方法一: 46 5.2.2 方法二: 48 5.3 一对多查询 50 5.3.1 Sql语句: 50 5.3.2 定义po类 50 5.3.3 Mapper.xml 51 5.3.4 定义resultMap 51 5.3.5 Mapper接口: 52 5.3.6 测试: 52 5.3.7 小结 53 5.4 多对多查询 53 5.4.1 查询用户购买的商品信息 53 5.4.2 小结 55 5.5 resultMap小结 55 5.6 延迟加载 56 5.6.1 打开延迟加载开关 56 5.6.2 一对一查询延迟加载 56 5.6.3 一对多延迟加载 59 5.6.4 延迟加载小结 59 6 查询缓存 59 6.1 mybatis缓存介绍 59 6.2 一级缓存 60 6.2.1 原理 60 6.2.2 测试1 61 6.2.3 测试2 61 6.3 二级缓存 62 6.3.1 原理 62 6.3.2 开启二级缓存: 62 6.3.3 实现序列化 63 6.3.4 测试 63 6.3.5 禁用二级缓存 63 6.3.6 刷新缓存 64 6.3.7 Mybatis Cache参数 64 6.3.8 mybatis整合ehcache 64 6.3.9 应用场景 67 6.3.10 局限性 67 7 与spring整合 68 7.1 mybatis与spring整合jar 68 7.2 Mybatis配置文件 68 7.3 Spring配置文件: 69 7.4 Mapper编写的三种方法 70 7.4.1 Dao接口实现类继承SqlSessionDaoSupport 70 7.4.2 使用org.mybatis.spring.mapper.MapperFactoryBean 71 7.4.3 使用mapper扫描器 71 8 Mybatis逆向工程 72 8.1 第一步:mapper生成配置文件: 72 8.2 第二步:使用java类生成mapper文件: 72 8.3 第三步:拷贝生成的mapper文件到工程中指定的目录中 73 8.3.1 Mapper.xml 73 8.3.2 Mapper.java 73 8.3.3 第四步Mapper接口测试 73 8.4 逆向工程注意事项 74 8.4.1 Mapper文件内容不覆盖而是追加 74 8.4.2 Table schema问题 74
MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in action之九mybatis 代码生成工具的使用 mybatis SqlSessionDaoSupport的使用附代码下载 转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看中文的:http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html) 写在这个系列前面的话: 以前曾经用过ibatis,这是mybatis的前身,当时在做项目时,感觉很不错,比hibernate灵活。性能也比hibernate好。而且也比较轻量级,因为当时在项目中,没来的及做很很多笔记。后来项目结束了,我也没写总结文档。已经过去好久了。但最近突然又对这个ORM 工具感兴趣。因为接下来自己的项目中很有可能采用这个ORM工具。所以在此重新温习了一下 mybatis, 因此就有了这个系列的 mybatis 教程. 什么是mybatis MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点: 1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。 mybatis实战教程(mybatis in action)之一:开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包。这些软件工具均可以到各自的官方网站上下载。 首先建立一个名字为 MyBaits 的 dynamic web project 1. 现阶段,你可以直接建立java 工程,但一般都是开发web项目,这个系列教程最后也是web的,所以一开始就建立web工程。 2. 将 mybatis-3.2.0-SNAPSHOT.jar,mysql-connector-java-5.1.22-bin.jar 拷贝到 web工程的lib目录. 3. 创建mysql 测试数据库和用户表,注意,这里采用的是 utf-8 编码 创建用户表,并插入一条测试数据 程序代码 程序代码 Create TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong'
### 回答1: Mybatis中的ResultMap可以用来映射一对多的关系。在ResultMap中,可以使用collection标签来定义一个集合属性,用来表示一对多的关系。在集合属性中,可以使用result标签来定义子对象的映射规则。例如: <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="name" column="order_name"/> </collection> </resultMap> 在上面的例子中,User对象包含一个orders属性,用来表示一个用户可以有多个订单。使用collection标签来定义orders属性,并使用ofType属性来指定子对象的类型为Order。在集合属性中,使用id和result标签来定义子对象的映射规则。这样,在查询结果中,就可以将多个订单映射到一个User对象中的orders属性中。 ### 回答2: 在MyBatis中,ResultMap是一种用于将查询到的结果集映射Java对象的机制。当查询结果集中包含多个对象的属性时,就需要使用ResultMap一对多映射一对多映射的实现,需要在ResultMap中定义一个collection元素来表示将多个对象映射成一个集合。collection元素中需要设置property、ofType和select等属性。 property属性表示映射到结果集中的查询条件,也就是查询多个对象时需要根据哪个属性进行关联。 ofType属性表示集合元素的类型,这里表示集合中元素的类型为哪个Java类。 select属性表示查询的语句,对应于查询结果集的collection列。 例如,以下是一段使用ResultMap实现一对多映射的示例代码: <resultMap id="UserMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <collection property="books" ofType="Book" select="selectBooksByUser" column="id"/> </resultMap> 在上面的代码中,UserMap是一个ResultMap配置,books是User类中的一个List<Book>类型属性。通过collection元素的配置,表示User对象与多个Book对象之间存在一对多关系。 需要注意的是,以上的示例代码中,还需要在mapper文件中定义selectBooksByUser语句,用于查询对应的Book对象。同时,需要配置查询语句中的查询条件,也就是column属性,与User对象的id属性关联起来。 通过以上的配置,如果查询结果集中包含了User对象与多个Book对象,MyBatis就会根据配置进行自动映射,将查询结果集中的数据转化为Java对象的形式,方便我们进行业务逻辑的处理。 ### 回答3: MyBatis是一种轻量级的ORM框架,支持复杂的SQL查询。其中,ResultMapMyBatis非常重要的一种映射规则,可以将查询结果映射Java对象中。 在MyBatis中,使用ResultMap进行一对多映射时,可以通过以下步骤完成: 1. 定义主实体类和从实体类 在一对多映射中,主实体类代表一端,从实体类代表多端。例如,在一个学校的管理系统中,Student代表主实体类,Grade代表从实体类。一名学生可以拥有多个成绩单。 2. 在主实体类中增加从实体类的集合 在主实体类中增加从实体类的集合属性,如下所示: public class Student { private int id; private String name; private List<Grade> grades; } 3. 定义ResultMap 定义ResultMap时,需要使用collection标签来映射从实体类的集合属性。注意,collection标签的属性property应该指向主实体类中的集合属性,同时需要指定从实体类的ResultMap,如下所示: <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="grades" ofType="Grade" resultMap="gradeResultMap"/> </resultMap> 4. 定义从实体类的ResultMap 在从实体类的ResultMap中,需要定义每一列的映射关系。同样地,需要定义id标签,指向主实体类的外键列,如下所示: <resultMap id="gradeResultMap" type="Grade"> <id property="id" column="id"/> <result property="subject" column="subject"/> <result property="score" column="score"/> <association property="student" javaType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </association> </resultMap> 5. 编写查询语句 最后,编写查询语句时,需要使用select标签,并在其中指定ResultMap,如下所示: <select id="getStudent" resultMap="studentResultMap"> SELECT s.id, s.name, g.id, g.subject, g.score, g.student_id, s2.name as student_name FROM student s LEFT JOIN grade g on s.id = g.student_id LEFT JOIN student s2 on g.student_id = s2.id </select> 这样,就完成了从数据库中查询学生及其所拥有的成绩单,并将结果映射为Student的对象。其中,每个Student对象的grades属性是一个List,其中包含多个Grade的对象。 以上就是使用ResultMap实现MyBatis一对多映射的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值