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.存储过 ...
随机推荐
- require/load/include/extend的区别
require 一般用于加载一个库,当多次使用require加载一个库时,只有第一次有效,后面的都会加载失败,也就是会返回"false",以为require会追踪文件是否被加载. ...
- Zookeeper在Centos7上搭建单节点应用
(默认机器上已经安装并配置好了jdk) 1.下载zookeeper并解压 $ tar -zxvf zookeeper-3.4.6.tar.gz 2.将解压后的文件夹移动到 /usr/local/ 目录 ...
- [转发] win8安装mindget mindmanger
win8安装mindget mindmanger 1安装MindManager时,显示安装Visual C++ 2005 Redistributable时报错 解决方法:1.把安装程序移动到没有 ...
- wp 去除google字体加载
add_filter('gettext_with_context', 'disable_open_sans', 888, 4 ); function disable_open_sans( $trans ...
- Go语言学习笔记七: 函数
Go语言学习笔记七: 函数 Go语言有函数还有方法,神奇不.这有点像python了. 函数定义 func function_name( [parameter list] ) [return_types ...
- 机器学习--聚类系列--DBSCAN算法
DBSCAN算法 基本概念:(Density-Based Spatial Clustering of Applications with Noise) 核心对象:若某个点的密度达到算法设定的阈值则其为 ...
- 在Bash中定制炫酷的命令提示符
如果你使用的是Linux桌面(例如:Fedora或者Ubuntu)的话,在Terminal上使用Bash通常是必须地,但是默认的Bash提示符都很普通.本文将提供简单的Bash脚本(通过定制PS1)定 ...
- Hive中自定义Map/Reduce示例 In Python
Hive支持自定义map与reduce script.接下来我用一个简单的wordcount例子加以说明.使用Python开发(如果使用Java开发,请看这里). 开发环境: python:2.7.5 ...
- hadoop学习笔记(九):MapReduce程序的编写
一.MapReduce主要继承两个父类: Map protected void map(KEY key,VALUE value,Context context) throws IOException, ...
- gdb中run出现的Missing separate debuginfos, use: debuginfo-install XXX
问题: Missing separate debuginfos, use: debuginfo-install glib 解决方法: 1.将/etc/yum.repo.d/CentOS-Debugin ...