Mybatis-Plus select不列出全部字段的方法

mybatis-plus select查詢語句默認是查全部字段,有兩種方法可以指定要查詢的字段

CREATE TABLE `user` (
 `id` bigint(20) NOT NULL COMMENT '主鍵',
 `name` varchar(30) DEFAULT NULL COMMENT '姓名',
 `age` int(11) DEFAULT NULL COMMENT '年齡',
 `email` varchar(50) DEFAULT NULL COMMENT '郵箱',
 `manager_id` bigint(20) DEFAULT NULL COMMENT '直屬上級id',
 `create_time` datetime DEFAULT NULL COMMENT '創建時間',
 PRIMARY KEY (`id`),
 KEY `manager_fk` (`manager_id`),
 CONSTRAINT `manager_fk` FOREIGN KEY (`manager_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1、user表隻需要查詢出name和age兩個字段的數據,可以使用queryWrapper的select()方法指定要查詢的字段

 @Test
 public void selectByWrapper10() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.select("name", "age").like("name", "雨");
  List<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
 }

日志輸出sql:

DEBUG==> Preparing: SELECT name,age FROM user WHERE name LIKE ?
DEBUG==> Parameters: %雨%(String)
TRACE<== Columns: name, age
TRACE<== Row: 張雨琪, 31
TRACE<== Row: 劉紅雨, 31
DEBUG<== Total: 2

2、user表要查詢出除manager_id和create_time外其它所有字段的數據,同樣也是使用queryWrapper的select()方法

 @Test
 public void selectByWrapper11() {
  QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  queryWrapper.select(User.class, info -> !info.getColumn().equals("manager_id")
    && !info.getColumn().equals("create_time"));
  List<User> users = userMapper.selectList(queryWrapper);
  users.forEach(System.out::println);
 }

日志輸出sql:

DEBUG==> Preparing: SELECT id,name,age,email FROM user
DEBUG==> Parameters:
TRACE<== Columns: id, name, age, email
TRACE<== Row: 1087982257332887553, 大boss, 40, [email protected]
TRACE<== Row: 1088248166370832385, 王天風, 25, [email protected]
TRACE<== Row: 1088250446457389058, 李藝偉, 28, [email protected]
TRACE<== Row: 1094590409767661570, 張雨琪, 31, [email protected]
TRACE<== Row: 1094592041087729666, 劉紅雨, 31, [email protected]
DEBUG<== Total: 5

到此這篇關於Mybatis-Plus select不列出全部字段的方法的文章就介紹到這瞭,更多相關Mybatis-Plus select不列出全部字段內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: