假设有表 TableA 和 TableB 两张表,其对应的域类如下:
class TableA {
TableB tableB
String name
}
class TableB {
String name
}
内连接(inner join)
TableA.createCriteria().get {
// 默认以内连接方式
tableB {
eq 'name', 'b'
}
eq 'name', 'a'
}
以上代码生成的SQL,相当于:
select *
from table_a ta
inner join table_b tb on ta.table_b_id = tb.id
where ta.name = 'a'
and tb.name = 'b'
左连接(inner join)
TableA.createCriteria().get {
// 显示声明别名与左连接方式
createAlias "tableB", "tb", JoinType.LEFT_OUTER_JOIN.joinTypeValue
eq 'tb.name', 'b'
eq 'name', 'a'
}
以上代码生成的SQL,相当于:
select *
from table_a ta
left join table_b tb on ta.table_b_id = tb.id
where ta.name = 'a'
and tb.name = 'b'
OkHttp 增强版 OkHttps 推荐
https://ok.zhxu.cn/
Bean Searcher 专注高级查询的只读 ORM
https://bs.zhxu.cn/