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