mybatisplus多表查詢分頁

一、不用vo的mybatisplus多表查詢分頁

mybatisplus是一個基於mybatis的增強工具,它提供了很多方便的查詢操作。在進行多表查詢分頁時,我們可以通過使用mybatisplus提供的Wrapper,來進行多表條件查詢。

首先,我們需要引入mybatisplus和分頁插件的依賴:

“`

com.baomidou
mybatis-plus-boot-starter
最新版本

com.baomidou
mybatis-plus-extension
最新版本

com.baomidou
mybatis-plus
最新版本

com.github.pagehelper
pagehelper
最新版本

“`

然後,定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;
@TableField(exist = false)
private Integer pageNum;
@TableField(exist = false)
private Integer pageSize;


}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select u.*, r.role_name\n” +
“from user u\n” +
“left join user_role ur on u.id = ur.user_id\n” +
“left join role r on ur.role_id = r.id\n” +
“${ew.customSqlSegment}”)
List getUserPageList(@Param(Constants.WRAPPER) Wrapper wrapper);
“`

最後,在Service中調用mapper方法,將條件設置到Wrapper中,並使用PageHelper進行分頁操作:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List getUserPageList(Integer pageNum, Integer pageSize, String username, String roleName) {
Page page = PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
wrapper.eq(User::getUsername, username)
.nested(i -> i.eq(Role::getRoleName, roleName));
List userList = userMapper.getUserPageList(wrapper);
userList.forEach(user -> user.setRoles(userMapper.getUserRoles(user.getId())));
return userList;
}
}
“`

二、mybatisplus多表查詢

在進行多表查詢時,我們可以使用mybatisplus提供的Wrapper來設置多個表的條件查詢。在Wrapper中,我們可以使用innerJoin、leftJoin和rightJoin方法來進行聯表查詢操作。

首先,我們需要定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;

}

@Data
public class Role implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String roleName;


}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select u.*, r.role_name\n” +
“from user u\n” +
“left join user_role ur on u.id = ur.user_id\n” +
“left join role r on ur.role_id = r.id\n” +
“${ew.customSqlSegment}”)
List getUserList(@Param(Constants.WRAPPER) Wrapper wrapper);
“`

最後,在Service中調用mapper方法,將條件設置到Wrapper中,即可獲取到對應的查詢結果:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List getUserList(String roleName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
wrapper.nested(i -> i.eq(Role::getRoleName, roleName));
List userList = userMapper.getUserList(wrapper);
userList.forEach(user -> user.setRoles(userMapper.getUserRoles(user.getId())));
return userList;
}
}
“`

三、mybatisplus自定義sql查詢

在有些情況下,我們需要自定義一些複雜的SQL語句來進行查詢。這時,我們可以使用mybatisplus提供的SqlInjector來進行自定義SQL查詢。

首先,我們需要實現一個繼承了AbstractMethod的類,並在其中定義自己的SQL方法:

“`
@Component
public class UserSqlInjector extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo, SqlMethod sqlMethod) {
String sql = “select * from user where username = #{username}”;
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addSelectMappedStatementForTable(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo);
}
}
“`

然後,在mapper介面中定義查詢方法,並使用@SqlParser註解來告訴mybatisplus,該方法不需要進行自動分頁處理:

“`
@SqlParser(filter = true)
List selectUserByUsername(@Param(“username”) String username);
“`

最後,在Service中調用mapper方法即可:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List selectUserByUsername(String username) {
return userMapper.selectUserByUsername(username);
}
}
“`

四、mybatisplus多表分頁

在進行多表分頁查詢時,我們可以使用mybatisplus提供的Page對象和Wrapper條件來進行分頁操作。

首先,我們需要定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;

}

@Data
public class Role implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String roleName;

@TableField(exist = false)
private List users;

}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select u.*, r.role_name\n” +
“from user u\n” +
“left join user_role ur on u.id = ur.user_id\n” +
“left join role r on ur.role_id = r.id\n” +
“${ew.customSqlSegment}”)
List getUserPageList(@Param(Constants.WRAPPER) Wrapper wrapper);
“`

最後,在Service中調用mapper方法,將條件設置到Wrapper中,並使用PageHelper進行分頁操作:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List getUserPageList(Integer pageNum, Integer pageSize, String roleName) {
Page page = PageHelper.startPage(pageNum, pageSize);
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
wrapper.nested(i -> i.eq(Role::getRoleName, roleName));
List userList = userMapper.getUserPageList(wrapper);
userList.forEach(user -> user.setRoles(userMapper.getUserRoles(user.getId())));
return userList;
}
}
“`

五、mybatisplus多表連接查詢

在進行多表連接查詢時,我們可以使用mybatisplus提供的Wrapper來進行設置多個表的條件查詢。在Wrapper中,我們可以使用innerJoin、leftJoin和rightJoin方法來進行聯表查詢操作。

首先,我們需要定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;

}

@Data
public class Role implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String roleName;

@TableField(exist = false)
private List users;

}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select u.*, r.role_name\n” +
“from user u\n” +
“left join user_role ur on u.id = ur.user_id\n” +
“left join role r on ur.role_id = r.id\n” +
“${ew.customSqlSegment}”)
List getUserList(@Param(Constants.WRAPPER) Wrapper wrapper);
“`

最後,在Service中調用mapper方法,將條件設置到Wrapper中,即可獲取到對應的查詢結果:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List getUserList(String roleName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
wrapper.nested(i -> i.eq(Role::getRoleName, roleName));
List userList = userMapper.getUserList(wrapper);
userList.forEach(user -> user.setRoles(userMapper.getUserRoles(user.getId())));
return userList;
}
}
“`

六、mybatisplus聯表查詢

在進行聯表查詢時,我們可以使用mybatisplus提供的Wrapper來進行設置多個表的條件查詢。在Wrapper中,我們可以使用innerJoin、leftJoin和rightJoin方法來進行聯表查詢操作。

首先,我們需要定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;

}

@Data
public class Role implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String roleName;

@TableField(exist = false)
private List users;

}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select * from user u left join user_role ur on u.id = ur.user_id left join role r on ur.role_id = r.id ${ew.customSqlSegment}”)
List getUserList(@Param(Constants.WRAPPER) Wrapper wrapper);
“`

最後,在Service中調用mapper方法,將條件設置到Wrapper中,即可獲取到對應的查詢結果:

“`
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public List getUserList(String roleName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
wrapper.nested(i -> i.eq(Role::getRoleName, roleName));
List userList = userMapper.getUserList(wrapper);
userList.forEach(user -> user.setRoles(userMapper.getUserRoles(user.getId())));
return userList;
}
}
“`

七、mybatisplus多表id查詢

在有些情況下,我們需要根據多個id進行查詢操作。這時,我們可以使用mybatisplus提供的Wrapper來進行條件查詢。

首先,我們需要定義實體類,並在實體類中使用@TableField註解來指定映射關係:

“`
@Data
public class User implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String username;
private String password;

@TableField(exist = false)
private List roles;

}

@Data
public class Role implements Serializable {
@TableId(value = “id”, type = IdType.AUTO)
private Long id;
private String roleName;

@TableField(exist = false)
private List users;

}
“`

接著,在mapper介面中定義查詢方法,使用@Select註解,並將查詢條件通過Wrapper進行設置:

“`
@Select(“select * from user where id in (${ids})”)
List getUserListByIds(@Param(“ids”) String ids);
“`

最後,在Service中調用mapper方法

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/185242.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-26 12:16
下一篇 2024-11-26 12:17

相關推薦

  • jQuery Datatable分頁中文

    jQuery Datatable是一個非常流行的數據表插件,它可以幫助您快速地在頁面上創建搜索、過濾、排序和分頁的數據表格。不過,它的默認設置是英文的,今天我們就來探討如何將jQu…

    編程 2025-04-29
  • uniapp分頁第二次請求用法介紹

    本文將從多個方面對uniapp分頁第二次請求進行詳細闡述,並給出對應的代碼示例。 一、請求參數的構造 在進行分頁請求時,需要傳遞的參數體包含當前頁碼以及每頁顯示的數據量。對於第二次…

    編程 2025-04-27
  • Mybatisplus面試題詳解

    Mybatisplus是在Mybatis的基礎上進行的封裝,它為我們簡化了開發操作,提供了自動生成常用SQL,自動分頁,及其他一些常用操作的功能,大大提高了開發的效率。在本篇文章中…

    編程 2025-04-25
  • Mybatisplus官網詳解

    一、簡介 Mybatisplus是一個基於mybatis的增強工具,可快速、安全的開發mybatis相關項目。它提供了許多實用的功能,如分頁、條件構造器、性能分析、代碼生成等。 M…

    編程 2025-04-24
  • Mybatis Plus分頁失效問題及解決方案

    一、分頁失效的原因 Mybatis Plus是一款優秀的ORM框架,使用簡單方便。但是,在使用它進行分頁時,有時會出現分頁失效的問題,原因可能有以下幾個方面: 1、Mybatis …

    編程 2025-04-24
  • MybatisPlus聯合主鍵註解

    一、@TableId註解的用途 MybatisPlus是基於Mybatis的增強工具,提供了很多便捷的註解和方法。@TableId註解是其中的一個。它的作用是標註實體類的主鍵欄位,…

    編程 2025-04-24
  • Mybatis分頁查詢SQL詳解

    一、Mybatis分頁查詢介紹 Mybatis是一款優秀的持久層框架,支持動態SQL和參數映射等功能,而分頁查詢也是其中非常重要的功能之一。 分頁查詢是指將大量的數據按照需要的記錄…

    編程 2025-04-23
  • Java 手動分頁的實現

    一、什麼是手動分頁 手動分頁是指在對一份文檔進行分頁時需要手動進行制定分頁標記的過程。在Java中,手動分頁可以通過計算、遍歷等方式實現,而實現手動分頁的主要目的是方便用戶對數據進…

    編程 2025-04-23
  • MyBatisPlus Exists詳解

    MyBatisPlus是一個強大的ORM框架,它通過簡化開發和提高資料庫訪問性能來提供強大的功能。其中,Exists查詢是常用的數據查詢方式之一。本文將從多個角度詳細闡述MyBat…

    編程 2025-04-12
  • C# List分頁 – 實現數據分頁的方法

    一、什麼是數據分頁 數據分頁是指將一定量的數據分成多個頁面來顯示,從而降低頁面的載入時間和用戶對數據的閱讀難度。在Web開發中,數據分頁是常用的數據處理方式之一。 而C#語言中的L…

    編程 2025-03-12

發表回復

登錄後才能評論