简介:
mybatis-flex 是一个优雅的 mybatis 增强框架,它非常轻量、同时拥有极高的性能与灵活性。我们可以轻松的使用 mybaits-flex 链接任何数据库,其内置的 "querywrapper " → 亮点,帮助我们极大的减少了 sql 编写的工作的同时,减少出错的可能性。
官网:
mybatis-flex - mybatis-flex 官方网站
多表联查
初始对比:
为了方便熟悉,我们和mybatis-plus的查询做一个对比,更加容易理解:
mybatis-flex:
querywrapper query = new querywrapper() .select( account.id, account.user_name, max(account.birthday), avg(account.sex).as("sex_avg") ); list<employee> employees = employeemapper.selectlistbyquery(query);
mybatis-plus:
querywrapper<employee> querywrapper = new querywrapper<>(); querywrapper .select( "id" ,"user_name" ,"max(birthday)" ,"avg(birthday) as sex_avg" ); list<employee> employees = employeemapper.selectlist(querywrapper);
在多表联查前面我们先尝试一个基础查询(基础映射):
@table(value = "tb_account") public class account { @id(keytype = keytype.auto) private long id; private string username; private int age; //getter setter }
account.java
与表 tb_account
是字段和属性是一一对应关系的。此时,我们在查询数据的时候,可以通过 accountmapper
方法直接查询,例如:
querywrapper qw = new querywrapper(); qw.select(account.all_columns) .where(account.id.ge(100)); list<account> accounts = accountmapper.selectlistbyquery(qw);
或者使用如下的链式查询,都可以直接得到 list<account>
结果:accounts
。
querychain.of(accountmapper) .select(account.all_columns) .where(account.id.ge(100)) .list();
as 映射
假设我们在 account.java
中多定义了一些其他属性,如下所示:
@table(value = "tb_account") public class account { @id(keytype = keytype.auto) private long id; private string username; private int age; //最大年龄 private int maxage; //平均年龄 private int avgage; //getter setter }
那么,我们在查询的时候,就可以通过 as
进行映射关联,查询代码如下:
querychain.of(accountmapper) .select( account.all_columns, max(account.age).as("maxage"), avg(account.age).as("avgage") ).where(account.id.ge(100)) .groupby(account.age) .list();
或者:
querychain.of(accountmapper) .select( account.all_columns, max(account.age).as("max_age"), avg(account.age).as("avg_age") ).where(account.id.ge(100)) .groupby(account.age) .list();
或者使用 lambda:
querychain.of(accountmapper) .select( account.all_columns, max(account.age).as(account::getmaxage), avg(account.age).as(account::getavgage) ).where(account.id.ge(100)) .groupby(account::getage) .list();
以上代码执行的 sql 如下:
select tb_account.* , max(tb_account.age) as maxage , avg(tb_account.age) as avgage where tb_account.id >= 100 group by tb_account.age
多表映射
假设我们定义了一个 bootvo.java
,其中包含了图书的基本信息,也包含了图书归属的用户信息,例如:
public class bookvo { //图书的基本字段 private long id; private long accountid; private string title; private string content; //用户表的字段 private string username; private int userage; }
此时,我们再进行 left join
多表查询时,代码如下:
list<bookvo> bookvos = querychain.of(bookmapper) .select( book.all_columns, //图书的所有字段 account.user_name, //用户表的 user_name 字段 account.age.as("userage") //用户表的 age 字段, as "userage" ).from(book) .leftjoin(account).on(book.account_id.eq(account.id)) .where(account.id.ge(100)) .listas(bookvo.class);
或者,我们也可以直接在 bookvo 中,定义 account
对象,例如:
public class bookvo { //图书的基本字段 private long id; private long accountid; private string title; private string content; //用户 private account account; }
查询代码如下:
list<bookvo> bookvos = querychain.of(bookmapper) .select( book.default_columns, account.default_columns, ) .from(book) .leftjoin(account).on(book.account_id.eq(account.id)) .where(account.id.ge(100)) .listas(bookvo.class);
高级映射
在以上的表结构中,一个账户可以有多本图书,那么我们假设定义的 accountvo.java
的结构如下:
public class accountvo { private long id; private string username; private int age; //账户拥有的 图书列表 private list<book> books; }
list<accountvo> bookvos = querychain.of(accountmapper) .select() // 不传入参数等同于 sql 的 select * .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
亦或者指定查询参数:
list<accountvo> bookvos = querychain.of(accountmapper) .select( account.id, account.user_name, account.age, book.title, book.content, ) .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
高级映射的场景中,我们还可以通过注解 @relationmanytoone
进行查询, 详情请点击 这里。
重名映射
在很多类型嵌套的场景下,可能会出现字段名定义重复的情况,例如:
@tableref(account.class) public class accountvo { private long id; private string name; private int age; //账户拥有的 图书列表 private list<book> book; }
public class book { private long id; private long accountid; private string name; }
在以上的嵌套定义中, accountvo
以及 book
都包含了 id
和 name
的定义,假设我们查询的方法如下:
list<accountvo> bookvos = querychain.of(accountmapper) .select( account.id, account.name, account.age, book.id, book.name, ) .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
其执行的 sql 如下:
select tb_account.id as tb_account$id, tb_account.name as tb_account$name, tb_account.age, tb_book.id as tb_book$id, -- flex 发现有重名时,会自动添加上 as 别名 tb_book.name as tb_book$name -- flex 发现有重名时,会自动添加上 as 别名 from tb_account left join tb_book on tb_account.id = tb_book.account_id where tb_account.id >= 100
此时,查询的数据可以正常映射到 accountvo
类。
注意事项
- 在查询 vo 类当中有重名字段时,需要给 vo 类标记
@tableref
注解,指定其对应的实体类,以正确添加别名。 - 在 querywrapper 的
select(...)
中,mybatis-flex 在 多表查询 的情况下,且有相同的字段名时,mybatis-flex 内部会主动帮助用户添加上 as 别名,默认为:表名$字段名
。
错误的情况:
若我们修改查询代码如下:
list<accountvo> bookvos = querychain.of(accountmapper) .select() .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
那么,其执行的 sql 如下:
select * from tb_account left join tb_book on tb_account.id = tb_book.account_id where tb_account.id >= 100
此时,查询的结果集中,会有多个 id
和 name
列,程序无法知道 id
和 name
对应的应该是 accountvo
的还是 book
的,因此,可能会出现数据错误赋值的情况。
所以,若程序中出现包裹对象有重名属性的情况时,querywrapper
的 select(...)
方法必须传入具体的字段才能保证数据正常赋值。
如下的代码也是没问题的:
list<accountvo> bookvos = querychain.of(accountmapper) .select( account.default_columns, book.default_columns ) .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
@columnalias
注解:
@columnalias
注解的作用是用于定义在 entity 查询时,默认的 sql 别名名称,可以取代自动生成的别名,例如:
public class book { @columnalias("bookid") private long id; private long accountid; @columnalias("bookname") private string name; }
那么,假设我们的查询代码如下:
list<accountvo> bookvos = querychain.of(accountmapper) .select( account.id, account.name, account.age, book.id, book.name, ) .from(account) .leftjoin(book).on(account.id.eq(book.account_id)) .where(account.id.ge(100)) .listas(accountvo.class);
其执行的 sql 为:
select tb_account.id, tb_account.name, tb_account.age, tb_book.id as bookid, -- @columnalias("bookid") tb_book.name as bookname -- @columnalias("bookname") from tb_account left join tb_book on tb_account.id = tb_book.account_id where tb_account.id >= 100
此时,数据也是可以正常映射。
到此这篇关于mybatis-flex实现多表联查(自动映射)的文章就介绍到这了,更多相关mybatis-flex多表联查内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论