一、mybatisplus連表查詢結果集封裝
Mybatisplus通過使用@Entity註解來標識實體類的主表,使用@TableField註解來標識實體類中的外鍵關聯字段,從而實現連表查詢的結果集封裝。例如:
@Entity @Table(name = "user_info") public class UserInfo { @TableId(type = IdType.ASSIGN_ID) private Long id; private String username; private String nickname; @TableField(exist = false) private List<Order> orders; } @Entity @Table(name = "order") public class Order { @TableId(type = IdType.ASSIGN_ID) private Long id; private String orderNo; private Double orderAmt; private Long userId; }
在這裡,UserInfo和Order是兩個實體類,通過在UserInfo中使用@TableField(exist = false)註解來標識實體類中的外鍵關聯字段,實現了與Order的多表關聯查詢。
二、mybatisPlus連表查詢
在使用mybatisPlus進行多表關聯查詢時,可以使用Wrapper來構建查詢條件,也可以自定義sql來實現查詢。例如:
使用Wrapper構建查詢條件:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("username", "test"); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
使用自定義sql語句查詢:
@Select("SELECT * FROM user_info ui INNER JOIN order o ON ui.id = o.userid WHERE ui.username = #{username}") List<UserInfo> selectUserInfoByUsername(@Param("username") String username);
三、mybatis查詢表結構
使用mybatis-plus進行多表關聯查詢時,查詢表結構非常重要,一般使用select *方式查詢表結構。例如:
SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'user_info' UNION ALL SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'order';
四、mybatisplus多條件查詢
使用mybatis-plus進行多條件查詢時,可以使用Wrapper來構建查詢條件,例如:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("username", "test") .ge("age", 18) .le("age", 30); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
五、mybatisplus多表查詢分頁
使用mybatis-plus進行多表查詢時,分頁查詢是非常常見和必要的。通過以下代碼可以實現多表查詢分頁:
IPage<UserInfo> page = new Page<>(1, 10); QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("username", "test"); IPage<UserInfo> userInfoIPage = userInfoMapper.selectPage(page, wrapper);
六、mybatisplus多表關聯查詢
使用mybatis-plus進行多表關聯查詢時,可以使用Wrapper來構建關聯查詢條件,例如:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("ui.username", "test") .eq("o.orderNo", "123456") .inSql("ui.id", "SELECT o.userId FROM order o WHERE o.orderNo = '123456'"); wrapper.select("ui.*", "o.*") .leftJoin("order o", "ui.id = o.userId"); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
七、mybatisplus聯表查詢
使用mybatis-plus進行聯表查詢時,可以通過在Wrapper中使用table()方法來指定需要聯表查詢的表,例如:
QueryWrapper<Order> wrapper = new QueryWrapper<>(); wrapper.select("o.*", "ui.nickname") .eq("o.orderNo", "123456") .eq("ui.username", "test") .table("order o") .leftJoin("user_info ui", "o.userId = ui.id"); List<Order> orderList = orderMapper.selectList(wrapper);
八、mybatisplus查詢列表
使用mybatis-plus查詢列表時,需要根據實際需求來選擇使用Wrapper構建查詢條件或是使用自定義sql語句查詢。例如:
使用Wrapper構建查詢條件:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("username", "test"); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
使用自定義sql語句查詢:
@Select("SELECT * FROM user_info WHERE username = #{username}") List<UserInfo> selectUserInfoByUsername(@Param("username") String username);
九、mybatisplus查詢語句選取
使用mybatis-plus進行查詢時,需要根據具體的業務需求來選擇使用哪種查詢語句,例如:
使用select x from table where y = ? and z = ?方式查詢:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.select("id", "username") .eq("username", "test") .eq("age", 20); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
使用select * from table where y = ? and z = ?方式查詢:
QueryWrapper<UserInfo> wrapper = new QueryWrapper<>(); wrapper.eq("username", "test") .eq("age", 20); List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
通過以上分析,我們可以看出,使用mybatisplus進行多表連表查詢十分靈活和簡單。同時,通過靈活的條件構建,可以滿足不同類型的查詢需求。希望此篇文章能為大家提供一些幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/232466.html