spring boot(9)-mybatis关联映射
一对多
public class Type {
String id;
String name;
List<User> users;
dao层
@Select("select * from user where typeid = #{typeid}")
public List<User> findUserById(String typeid);
@Results({
@Result(property="id",column="id"),
//users映射List<User> users,many=@Many是调用关联查询方法,"id"是关联查询条件,FetchType.LAZY是延迟加载
@Result(property="users",column="id", many=@Many(select="hello.dao.MybatisDao.findUserById",fetchType=FetchType.LAZY))
})
@Select("select * from type where id=#{id}")
public Type findTypeById(String id);
Type type = mybatisDao.findTypeById("1");
System.out.println("延迟加载");
type.getUsers();
因为设置了fetchType=FetchType.LAZY,mybatisDao.findTypeById("1")只会查询type表,当访问type.getUsers()时才会查询其关联表。
其它关联
一对一:把上面的many=@Many换成one=@One,其他原理是一样的
多对多:把多个字段映射成many=@Many,就是多对多了
多对一:把上面dao方法的返回值从Type换成List<Type>
JAVA注解的局限性
xml无限层嵌套映射
这里以三层嵌套为例,以实现前端的三级菜单树。这是一个tree表,pid是其上级菜单的id。
public class Tree {
String id;
String name;
List<Tree> child;
@Mapper
public interface TreeDao {
@ResultMap("tree")
@Select("SELECT p1.id,p1.name,p2.id id2,p2.name name2,p3.id id3,p3.name name3 "
+ "FROM tree p1,tree p2,tree p3 WHERE p1.id=p2.pid AND p2.id=p3.pid")
public List findTree();
这个SQL在数据库中的查询结果是这样的,可以发现前四个字段是一样的,而且都是冗余数据,如果用java注解的关联查询是不会这样的
#指定xml映射文件的路径
mybatis.mapper-locations=classpath:hello/mapper/*
hello.mapper.TreeMapper.xml
<?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">
<!-- 映射dao -->
<mapper namespace="hello.dao.TreeDao">
<!-- 结果集类型 -->
<resultMap id="tree" type="hello.pojo.Tree">
<!-- 映射字段 -->
<result column="id" property="id" />
<result column="name" property="name" />
<!-- 嵌套第二张表 -->
<collection property="child" ofType="hello.pojo.Tree" >
<id column="id2" property="id" />
<result column="name2" property="name" />
<!-- 嵌套第三张表 -->
<collection property="child" ofType="hello.pojo.Tree" >
<id column="id3" property="id" />
<result column="name3" property="name" />
</collection>
</collection>
</resultMap>
</mapper>
这里只是配置一个嵌套映射,在dao方法中通过@ResultMap("tree")使用这个映射。最终查询结果会映射成一个Tree对象,通过spring mvc转换为json结果如下,在一些前端框架中,实现树形菜单就是需要用这种结构的JSON数据赋值
[
{
"id": "1",
"name": "一级树",
"child": [
{
"id": "11",
"name": "二级树-1",
"child": [
{
"id": "112",
"name": "三级树-1",
"child": null
},
{
"id": "113",
"name": "三级树-2",
"child": null
}
]
}
]
}
]
使用JAVA注解还是XML
spring boot(9)-mybatis关联映射的更多相关文章
- Spring Boot (11) mybatis 关联映射
一对多 查询category中的某一条数据,同时查询该分类下的所有Product. Category.java public class Category { private Integer id; ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- Spring boot教程mybatis访问MySQL的尝试
Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 详解Spring Boot集成MyBatis的开发流程
MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集. spring Boot是能支持快速创建Spring应用的Java框 ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
随机推荐
- jQuery表格自动增加
<!DOCTYPE html> <html dir="ltr" lang="zh-CN"> <head> <meta ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Android侧滑菜单和轮播图之滑动冲突
接手一个项目,有一个问题需要修改:轮播图不能手动滑动,手动滑动轮播图只会触发侧滑菜单. 猜测:viewpager控件(轮播图)的触摸事件被SlidingMenu控件(侧滑菜单,非第三方项目,乃是上个开 ...
- html的css选择器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 代码查看php是否已开启rewrite功能模块
通过php代码来查询,是否把rewrite模块打开了 <?php $result = apache_get_modules(); if(in_array('mod_rewrite', $resu ...
- C#对json数据的解析
一,基础知识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类 ...
- CentOS 7 安装 RabbitMQ 3.7
目录 CentOS 7 安装 RabbitMQ 3.7 安装Erlang 安装依赖 创建yum源 参考 添加内容 安装 进入erlang命令行表示成功 安装 socat RabbitMQ 安装 sys ...
- Linux内核编程规范与代码风格
source: https://www.kernel.org/doc/html/latest/process/coding-style.html translated by trav, travmym ...
- 我是怎么从安卓到php再成为前端开发工程师的
记得我下定决心学Android(安卓)是17年的暑假,暑假前,学校组织了一次集训,美其名曰帮我们巩固知识,实际上就是学校和长沙的培训学校某牛达成了合作,教我们一些基础知识,然后集训完建议那些在学校没学 ...
- 【WePY小程序框架实战二】-页面结构
[WePY小程序框架实战一]-创建项目 项目结构 |-- dist |-- node_modules |-- src | |-- components |-- a.wpy |-- b.wpy |-- ...