通常,我们使用Mybatis实现join表关联的时候,一般都是通过在xml或注解里写自定义sql实现。

本文通过Mybatis Generator的插件功能新增一个JoinPlugin插件,只要在配置文件里加上该插件就可以使用。无其他第三方依赖。如下图:

该插件符合mbg plugin即插即用的特点,不影响生成的实体类,只对生成的Example文件做少量变动(新增一个内部类)。

首选我们看一下使用效果,如果符合你的要求,请关注支持。

/**
* 简单join查询示例
* select t0.user_id as t0_user_id,t0.user_name as t0_user_name,t0.login_account as t0_login_account,t0.login_password as t0_login_password,
* t0.user_sex as t0_user_sex,t0.user_email as t0_user_email,t0.user_mobile as t0_user_mobile,t0.user_avatar as t0_user_avatar,
* t0.user_company as t0_user_company,t0.user_dept as t0_user_dept,t0.is_del as t0_is_del,t0.is_admin as t0_is_admin,
* t0.system_type as t0_system_type,t0.last_login_time as t0_last_login_time,t0.create_time as t0_create_time,t0.create_user as t0_create_user,
* t0.update_time as t0_update_time,t0.update_user as t0_update_user,
* t2.role_id as t2_role_id,t2.role_name as t2_role_name
* from auth_user as t0
* inner join auth_user_role as t1 on t0.user_id = t1.user_id
* left join auth_role as t2 on t2.role_id = t1.role_id
* where ( ( t0.user_id is not null and ( t0.create_time is not null or t0.create_user is not null ) ) ) and ( t1.user_id <>999999 and t1.role_id >=1 )
* order by t2.role_id desc,t0.create_time
* limit 10,100
*/
public void joinTable() {
AuthUserExample authUserExample=new AuthUserExample();
AuthUserExample.Criteria criteria=authUserExample.createCriteria();
//where条件
criteria.andUserIdIsNotNull();
criteria.createOrCriteria().andCreateTimeIsNotNull().andCreateUserIsNotNull(); //关联user_role
AuthUserRoleExample urExample=new AuthUserRoleExample();
//where中user_role表的条件
urExample.createCriteria().andUserIdNotEqualTo(999999).andRoleIdGreaterThanOrEqualTo(1);
criteria.createJoinCriteria()
.innerJoinTable(urExample)
//使用User表的user_id关联user_role表的user_id
.on(a->a.getUserId(), a->a.equalTo(b->b.tableInfo.getUserId())); //关联role
AuthRoleExample roleExample=new AuthRoleExample();
criteria.createJoinCriteria()
.leftJoinTable(roleExample)
//使用role表的role_id关联user_role表的role_id
.on(a->a.tableInfo.getRoleId(), a->a.equalTo(b->b.tableInfo.getRoleId()), urExample)
//先按照role表的role_id降序
.orderByDesc(a->a.tableInfo.getRoleId())
//再按照user表的create_time升序
.orderByFirstTable(a->a.getCreateTime())
//跳过前10条
.skip(10)
//取100条记录
.take(100)
//只查询role表的role_id和role_name两个字段
.select(a->new String[] {a.tableInfo.getRoleId(),a.tableInfo.getRoleName()}); //执行查询
List<Tuple> userList=userDao.selectJoinByExample(authUserExample);
int totalItemCount=(int) userDao.countJoinByExample(authUserExample);
System.out.println(userList.size()+",total:"+totalItemCount);
for(Tuple tuple:userList) {
AuthUser authUser=tuple.getObject(AuthUser.class);
AuthUserRole authUserRole=tuple.getObject(AuthUserRole.class);
AuthRole authRole=tuple.getObject(AuthRole.class);
totalItemCount++;
}
}

控制台打印内容如下:

2020-04-23 23:09:16.326  INFO 10308 --- [nio-9000-exec-4] p.r.m.i.SqlStatementInterceptor          : ===>sql:select t0.user_id as t0_user_id,t0.user_name as t0_user_name,t0.login_account as t0_login_account,t0.login_password as t0_login_password,t0.user_sex as t0_user_sex,t0.user_email as t0_user_email,t0.user_mobile as t0_user_mobile,t0.user_avatar as t0_user_avatar,t0.user_company as t0_user_company,t0.user_dept as t0_user_dept,t0.is_del as t0_is_del,t0.is_admin as t0_is_admin,t0.system_type as t0_system_type,t0.last_login_time as t0_last_login_time,t0.create_time as t0_create_time,t0.create_user as t0_create_user,t0.update_time as t0_update_time,t0.update_user as t0_update_user,t0.`type` as t0_type,t0.title as t0_title,t0.bz as t0_bz,t2.role_id as t2_role_id,t2.role_name as t2_role_name
from auth_user as t0
inner join auth_user_role as t1 on t0.user_id = t1.user_id
left join auth_role as t2 on t2.role_id = t1.role_id
where ( ( t0.user_id is not null and ( t0.create_time is not null or t0.create_user is not null ) ) ) and ( t1.user_id <>999999 and t1.role_id >=1 )
order by t2.role_id desc,t0.create_time
limit 10,100
2020-04-23 23:09:16.329 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.selectJoinByExample : ==> Preparing: select t0.user_id as t0_user_id,t0.user_name as t0_user_name,t0.login_account as t0_login_account,t0.login_password as t0_login_password,t0.user_sex as t0_user_sex,t0.user_email as t0_user_email,t0.user_mobile as t0_user_mobile,t0.user_avatar as t0_user_avatar,t0.user_company as t0_user_company,t0.user_dept as t0_user_dept,t0.is_del as t0_is_del,t0.is_admin as t0_is_admin,t0.system_type as t0_system_type,t0.last_login_time as t0_last_login_time,t0.create_time as t0_create_time,t0.create_user as t0_create_user,t0.update_time as t0_update_time,t0.update_user as t0_update_user,t0.`type` as t0_type,t0.title as t0_title,t0.bz as t0_bz,t2.role_id as t2_role_id,t2.role_name as t2_role_name from auth_user as t0 inner join auth_user_role as t1 on t0.user_id = t1.user_id left join auth_role as t2 on t2.role_id = t1.role_id WHERE ( ( t0.user_id is not null and ( t0.create_time is not null or t0.create_user is not null ) ) ) and ( t1.user_id <>? and t1.role_id >=? ) order by t2.role_id desc,t0.create_time limit 10,100
2020-04-23 23:09:16.330 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.selectJoinByExample : ==> Parameters: 999999(Integer), 1(Integer)
2020-04-23 23:09:16.361 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.selectJoinByExample : <== Total: 100
2020-04-23 23:09:16.367 INFO 10308 --- [nio-9000-exec-4] p.r.m.i.SqlStatementInterceptor : c.r.m.d.AuthUserMapper.selectJoinByExample:41ms
2020-04-23 23:09:16.371 INFO 10308 --- [nio-9000-exec-4] p.r.m.i.SqlStatementInterceptor : ===>sql:select count(*)
from auth_user as t0
inner join auth_user_role as t1 on t0.user_id = t1.user_id
left join auth_role as t2 on t2.role_id = t1.role_id
where ( ( t0.user_id is not null and ( t0.create_time is not null or t0.create_user is not null ) ) ) and ( t1.user_id <>999999 and t1.role_id >=1 )
2020-04-23 23:09:16.375 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.countJoinByExample : ==> Preparing: select count(*) from auth_user as t0 inner join auth_user_role as t1 on t0.user_id = t1.user_id left join auth_role as t2 on t2.role_id = t1.role_id WHERE ( ( t0.user_id is not null and ( t0.create_time is not null or t0.create_user is not null ) ) ) and ( t1.user_id <>? and t1.role_id >=? )
2020-04-23 23:09:16.376 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.countJoinByExample : ==> Parameters: 999999(Integer), 1(Integer)
2020-04-23 23:09:16.385 DEBUG 10308 --- [nio-9000-exec-4] c.r.m.d.A.countJoinByExample : <== Total: 1
2020-04-23 23:09:16.385 INFO 10308 --- [nio-9000-exec-4] p.r.m.i.SqlStatementInterceptor : c.r.m.d.AuthUserMapper.countJoinByExample:14ms
100,total:5190

通过上面简单例子我们可以看到,生成的sql是完全符合预期的,并且不需要我们额外的手写sql了。

该插件特点:

1.可关联多表,on多个字段关联,on多种条件查询,例如:left join t_role as t1 on t0.role_id=t1.role_id and t0.sys_type=t1.role_type and t1.is_del=0 and t1.role_type='T'

2.可自定义表别名

3.sql中的所有表都可自定义要查询的字段,表字段多时,指定字段查询明显提高效率

4.分页、多表排序支持,函数式编程写法

5.支持in,exists等子查询

6.mybatis原生特性,无其他依赖

该插件原理:

由于mbg每个表都生成一个xml,在xml里自动组装生成的sql,并返回map对象。通过mybatis的拦截器拦截返回结果,将map转换成不同表的实体对象然后组装返回到一个类Tuple中。

本插件初衷:为了简化开发人员的数据库sql繁琐查询工作及改善mybatis写join关联时只能自定义的状况。开发过程中当表结构改变时,所有手写自定义的sql里的字段都要改一遍。

另外,mbg1.4.0的dynamic-sql不怎么好用,这么久了更新这样一个新版本很失望。

Mybatis Generator通用Join的实现的更多相关文章

  1. springboot中通用mapper结合mybatis generator的使用

    通用mapper就是指的是  tk.mybatis  包下的.这个是通用mapper就是说自动生成的dao层需要继承这个框架提供的mapper类.而我们之前用的org.mybatis这个最开始是普通的 ...

  2. Mybatis通用Join的实现(最终版)

    你是否还在为mybatis的多表关联查询而写xml烦恼,是否还在为动态组装查询条件烦恼,是否还在为此没有合适的解决方案烦恼? mybatis-extension插件,解决开发过程中需要多表关联时需手写 ...

  3. MyBatis Generator 自动生成的POJO对象的使用(一)

    MyBatis Generator 会自动生成以下几种类型的对象(除非你使用MyBatis3DynamicSql 的运行环境): Java Model Objects(总是生成) SQL Map Fi ...

  4. mybatis Generator生成代码及使用方式

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...

  5. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  6. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  7. mybatis Generator配置文件详解

    这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...

  8. Mybatis Generator(定制化)代码生成器

    1.使用Mapper专用的MyBatis Generator插件 通用Mapper在1.0.0版本的时候增加了MyBatis Generator(以下简称MBG)插件,使用该插件可以很方便的生成实体类 ...

  9. mybatis.generator.configurationFile

    mybatis.generator.configurationFile 有一个更好的配置方法,可以不用在generateConfig.xml里面写死驱动的地址:如果你的mybatis连接也是在pom. ...

随机推荐

  1. Python第四章-流程控制

    流程控制 在以前的代码中,所有的代码都是交由 Python 忠实地从头执行到结束.但是这些远远不够.很多时候需要根据不同的情况执行不同的代码. 如果你想改变这一工作流程,应该怎么做? 就像这样的情况: ...

  2. PyTorch Hub发布!一行代码调用最潮模型,图灵奖得主强推

    为了调用各种经典机器学习模型,今后你不必重复造轮子了. 刚刚,Facebook宣布推出PyTorch Hub,一个包含计算机视觉.自然语言处理领域的诸多经典模型的聚合中心,让你调用起来更方便. 有多方 ...

  3. 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法

    Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...

  4. Jenkins打造多分支流水线指南

    overview: 多分支工作流程带来了以下几个关键能力: 在代码仓库中,每个新分支都有自己单独的工作流水线(job). 每个工作流水线都记录了对应分支的构建和变更历史. 可以自定义设置流水线随着分支 ...

  5. 加密解密 Python

    常见加密方式和Python实现 1. 前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时候,要确保我们 ...

  6. MyBatis 教程 ——检视阅读

    MyBatis 教程 --检视阅读 准备 官网文档-中文 教程地址yiibai,质量很差 教程地址w3cschool,纯理论,还不如直接看官网文档 教程地址Mybatis框架入门教程,Oracle M ...

  7. 【php】面向对象(一)

    1. 学习面向对象的目标: a) 语法的学习: b) 编程思想的学习: i. 过程化: ii. 面向对象:2. 比较(有对象和没对象的区别) a) 没对象: i. 我饿了 自己做饭 ii. 我渴了 自 ...

  8. keras与卷积神经网络(CNN)实现识别minist手写数字

    在本篇博文当中,笔者采用了卷积神经网络来对手写数字进行识别,采用的神经网络的结构是:输入图片——卷积层——池化层——卷积层——池化层——卷积层——池化层——Flatten层——全连接层(64个神经元) ...

  9. python爬取疫情数据详解

    首先逐步分析每行代码的意思: 这是要引入的东西: from os import path import requests from bs4 import BeautifulSoup import js ...

  10. Mac系统中安装virtualenv虚拟环境

    总体来说有三个步骤. 1.创建工作目录. python3 -m venv lanyue_env 注意: 2.安装virtualenv. pip3 install --user virtualenv 2 ...