Mybatis多表查询

MyBatis-多表查询-一对一查询(方式一)

一个菜品对应一个分类

直接菜品记录category对象

菜品id写入Dish,后面的分类直接写入 Category类

封装,如果sql不能封装上,那么直接使用resultmap封装

使用resultType只能封装基本属性

所以要定义一个resultmap手动封装

使用标签

要有id

使用resultmap 进行 一对一的封装

MyBatis-多表查询-一对一查询(方式二)

使用别名 : 别名就是类名的简写 首字母写法都可以 不区分大小写

当在peoperties中配置别名之后,直接写别名就可以

总结

Mybatis的多表查询 1对1

首先 1对1查询 ,

首先在pojo实体类中先声明出另一个对象的属性

因为是1对1查询

然后按照mybatis的三步约定

第一 同包同名 mapper接口要与mapper.xml所在的包的路径要相同 ,并且名字要一样

第二个 mapper.xml的namespace的地址要写 mappe的全类名

第三个mapper.xml的id要与mapper的方法名要一致

java是与resource是同一级的

都在main的下面

main的下面只有 java与resource

mybatis的使用

mybatis的使用首先要配置数据库的参数

mybatis的驼峰映射也要开启,不用设计参数名了

然后进行sql的书写

这个是多表的查询所以,在一个resultmap中是查询的引用对象

resulttype是查询的简单数据

使用resultMap

sql 复制代码
    <resultMap id="dishMap1" type="dish">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="category_id" property="categoryId"></result>
        <result column="price" property="price"></result>
        <result column="image" property="image"></result>
        <result column="description" property="description"></result>
        <result column="status" property="status"></result>
        <result column="create_time" property="createTime"></result>
        <result column="update_time" property="updateTime"></result>

        <result column="cid" property="category.id"></result>
        <result column="cname" property="category.name"></result>
        <result column="ctype" property="category.type"></result>
        <result column="csort" property="category.sort"></result>
        <result column="cstatus" property="category.status"></result>
        <result column="ccreate_time" property="category.createTime"></result>
        <result column="cupdate_time" property="category.updateTime"></result>
    </resultMap>

这是第一种方式,

直接列对应属性,

第二种方式使用association

sql 复制代码
    <resultMap id="dishMap2" type="Dish">

        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="category_id" property="categoryId"></result>
        <result column="price" property="price"></result>
        <result column="image" property="image"></result>
        <result column="description" property="description"></result>
        <result column="status" property="status"></result>
        <result column="create_time" property="createTime"></result>
        <result column="update_time" property="updateTime"></result>

        <association property="category" javaType="com.healer.pojo.Category">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
            <result column="ctype" property="type"></result>
            <result column="csort" property="sort"></result>
            <result column="cstatus" property="status"></result>
            <result column="ccreate_time" property="createTime"></result>
            <result column="cupdate_time" property="updateTime"></result>
        </association>


    </resultMap>
复制代码
<!--    查询菜品,同时查询出菜品所示的分类-->
<select id="findAlldishWithCategory" resultMap="dishMap2">
    select d.*,
           c.id          cid,
           c.name        cname,
           c.type        ctype,
           c.sort        csort,
           c.status      cstatus,
           c.create_time ccreate_time,
           c.update_time cupdate_time
    from dish d
             left join category c on d.category_id = c.id;
</select>

列对应着属性

如果想要简写,可以使用

java 复制代码
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.healer.pojo

然后就可以写别名

在association中

使用javaType

Mybatis-多表查询-一对多查询

查询的数据往分类里面封装

既然一个分类里面的基本信息不能满足需求了

使用集合 在pojo中定义一个集合

多对多的查询

保持原则

查询的数据要往哪个表中去封装

总结

不用记什么多对多,一对多 ,一对一

只需要记住使用resultmap

什么什么封装的
下面id写

然后其他的使用result

遇到对象了使用association , javaType

遇到集合了使用collection , ofType

Mybatis完结撒花

相关推荐
2301_32241428041 天前
基于SSM框架的在线考试管理系统的设计与实现14750-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·redis·mysql·tomcat·maven·mybatis·ssm
毕业设计7033 天前
(免费领源码)SSM董家湾线上点餐系统04682- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·redis·mysql·tomcat·maven·mybatis·ssm
云絮.4 天前
mybatis(二)
java·数据库·mybatis
夕除5 天前
redis--主从还是集群,内存
java·spring·mybatis
驭渊的小故事5 天前
MyBatis-Plus 实战指南:从基础 CRUD 到条件构造器与自定义 SQL(二)
数据库·sql·mybatis
毕业设计7036 天前
(免费领源码)基于node的学生公寓管理系统06412- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·mysql·node.js·maven·mybatis·express·b/s
驭渊的小故事6 天前
MyBatis XML 开发实战:从配置到 CRUD 全解析
xml·java·mybatis
云絮.7 天前
Mybatis(一)
java·数据库·mybatis
她的男孩9 天前
权限通了、列表有数,一点分页就炸Forge Admin 用 COUNT(1) 扛住复杂待办
java·spring boot·mybatis
paopaokaka_luck9 天前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis