mysql plus多表关联_结合mybatis-plus 实现实体操作多表关联查询
這里將告訴您結(jié)合mybatis-plus 實(shí)現(xiàn)實(shí)體操作多表關(guān)聯(lián)查詢,教程操作方法:
multipleselect
java mybatis 實(shí)現(xiàn)簡(jiǎn)單多表查詢
簡(jiǎn)介
實(shí)現(xiàn)簡(jiǎn)單的實(shí)體類操作多表, 首先你的項(xiàng)目是使用了mybatis-plus 才可以使用。
? 通過(guò)解析實(shí)體,調(diào)用通用的XML來(lái)實(shí)現(xiàn)多表查詢, 提供一個(gè)設(shè)計(jì)多表查詢的思路,復(fù)雜的Sql嵌套等目前并不支持
目前只支持:
? left join方式,(能關(guān)聯(lián)的兩張表的實(shí)體中關(guān)聯(lián)字段名稱必須一樣,數(shù)據(jù)庫(kù)字段可以不一樣可以通@TableField注解來(lái)解決
? where 基本查詢條件等
?分頁(yè) 查詢
?order 排序
可以用來(lái)三兩句搞定一些簡(jiǎn)單關(guān)聯(lián)查詢業(yè)務(wù),解決不需要寫(xiě)的代碼
設(shè)計(jì)說(shuō)明
如何關(guān)聯(lián)表?
?找第一張表注解為 TableId (mybatis-plus 注解)的屬性名, 到每二張表找同樣的屬性名, 如果沒(méi)找到,反過(guò)來(lái)找,如果還沒(méi)找到,挨個(gè)屬性找。以此類推,實(shí)現(xiàn)關(guān)聯(lián)的前提條件是 主從表的實(shí)體關(guān)聯(lián)列名必須是一樣的
// user 表
@TableId
private Integer userId
// address 表
@TableId
private Integer addressId
private Integer userId
//那么自動(dòng)條件為 user.user_id = address.user_id
//或者是
@TableId(value="id")
private Integer userId
// address 表
@TableId(value="id")
private Integer addressId
@TableField(value="test_user_id")
private Integer userId
//目前只有l(wèi)eft join
//那么自動(dòng)條件為 user.id = address.test_user_id
使用說(shuō)明
?將 com.freedomen.multipselect 包放到你的項(xiàng)目中,使 com.freedomen.multipselect.mapper里的xml 要被掃描到,或手動(dòng)配置, com.freedomen.multipselect.service也要被發(fā)現(xiàn)
//引入service
@Autowired
private MultipleService multipleService;
//表關(guān)聯(lián), 關(guān)聯(lián)用戶表和地址表,查找 用戶表的所有字段和地址表的所有字段
MultipleSelect multipleSelect = MultipleSelect.newInstance("${1}", new User(), new Address());
multipleSelect
.where("${0}")
.like("userName", "張三");
multipleService.mulSelect(multipleSelect);
查找字段
//MultipleSelect.newInstance 的第一個(gè)參數(shù)是所要查找的字段
//${0} 或 ${user} 表是第一張表的所有字段 ${0}.userName或${user}.userName表示userName字段, 默認(rèn)第一張表的字段全部都返回的。 ${}中間的參數(shù)可以是后面實(shí)體的下標(biāo),也可以是表名 如user、user_address
//字段中有@TableField(exist=false)注解也是被跳過(guò)的
//下面是要訂單表的所有信息 和用戶的姓名與號(hào)碼 和地址
MultipleSelect.newInstance("${1}.userName,${1}.userPhone,${2}", new Orders(), new User(), new Address());
查找條件
eq: =
notEq: !=
like: LIKE (前置已經(jīng)加了 '%')
between: between
and: 改變連接方式為 AND練級(jí)(默認(rèn))
or: 改變 連接方式為 OR
division:括號(hào) ,不支持括號(hào)嵌套括號(hào)
in: IN
notIn: NOT IN
notLike: NOT LIKE
...
//實(shí)例好 查找實(shí)體后可以操作實(shí)體
//注意: 如何實(shí)體內(nèi)屬性有值 將會(huì)以 eq方式and連接做為where 條件
/*
可以關(guān)聯(lián)的必要條件
Orders:
@TableId //或者數(shù)據(jù)庫(kù)字段為其它@TableId(value="id")
private Long ordersId;
private Long userId;
...
User:
@TableId
private Long userId;
...
Address:
@TableId
private Long addressId;
private Long userId;
...
*/
MultipleSelect multipleSelect = MultipleSelect.newInstance("${1}.userName,${1}.userPhone,${2}", new Orders(), new User(), new Address());
multipleSelect
.where("${0}") //哪張表
.eq("ordersId", 1) //并且 訂單id = 1
.like("ordersName", "cmcc") //并且 訂單名稱 like ''%cmcc'
.or() //改變后續(xù)操作關(guān)系為 OR, 默認(rèn)為AND
.notEq("orderSno", "123"); //或者 orderSno 不等于 '123'
multipleSelect
.where("${1}") //哪張表接著用戶表 默認(rèn)and連接 可以 .or()改為 OR
.in("userId", [1, 2, 3]); // 并且userId in [1, 2, 3]
multipleSelect
.where("${2}")
.or()
.like("adressDetails", "江蘇"); //或者 地址 like '江蘇'
multipleService.mulSelect(multipleSelect); //查詢
//括號(hào)
multipleSelect.where("${0}")
.eq("componyId", 1)
.division()
.like("userName", "abcd")
.or()
.like("userPhone", "abcd");
// 部分sql: compony_id = 1 and (user_name = 'abcd' or user_phone = 'abcd')
排序
//MultipleSelect.setOrderBy(...columns)
MultipleSelect.setOrderBy("${0}.createTime", "${1}.ordersName desc", "${2}.userId asc", ...)
分頁(yè)
//MultipleSelect.setPage(pageNo, pageSize);
MultipleSelect.setPage(1, 15); //第一頁(yè) 每頁(yè) 15條
multipleService.mulSelect返回結(jié)果
//MultipleResult
/*原型
private List> data; //結(jié)果數(shù)據(jù)
private Integer pageNo; //如果設(shè)置了分頁(yè) 會(huì)有
private Integer pageSize; //如果設(shè)置了分頁(yè) 會(huì)有
private Integer total;//如果設(shè)置了分頁(yè) 會(huì)有
*/
邏輯刪除
//默認(rèn)是讀取 mybatis-plus 的 TableLogic 注解 0 未刪除,
//如果不是用 0 表示未刪除, 可以修改 MultipleSelect 的 setCustomWhere 方法中的下面這段中的 0
if (logic != null)
sb.append(" AND ")
.append(te.getNickName())
.append(".")
.append(logic)
.append(" = ")
.append("0");
結(jié)合mybatis-plus 實(shí)現(xiàn)實(shí)體操作多表關(guān)聯(lián)查詢就為您介紹到這里,感謝您關(guān)注懶咪學(xué)編程c.lanmit.com.
本文地址:https://c.lanmit.com/shujuku/qita/13784.html
總結(jié)
以上是生活随笔為你收集整理的mysql plus多表关联_结合mybatis-plus 实现实体操作多表关联查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为nova6se怎么升级鸿蒙,华为EM
- 下一篇: linux cmake编译源码,linu