
每天玩转3分钟 MyBatis-Plus - 1. 配置环境
每天玩转3分钟 MyBatis-Plus - 2. 普通查询
mybatis-plus的查询功能非常强大, 这一篇,我们来看下mybatis-plus的普通查询功能。后续文章再介绍高级查询功能。
User 表结构如下:
id | name | age | |
|---|---|---|---|
1 | Jone | 18 | test1@baomidou.com |
2 | Jack | 20 | test2@baomidou.com |
3 | Tom | 28 | test3@baomidou.com |
4 | Sandy | 21 | test4@baomidou.com |
5 | Billie | 24 | test5@baomidou.com |
其对应的数据库 Schema 脚本如下:
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);其对应的数据库 Data 脚本如下:
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com'); 1 package com.example.demo.entity;
2
3 import lombok.Data;
4
5 @Data
6 public class User {
7 private Long id;
8 private String name;
9 private Integer age;
10 private String email;
11 }1 package com.example.demo.mapper;
2
3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.example.demo.entity.User;
5
6 public interface UserMapper extends BaseMapper<User> {
7 } 1 /*
2 * 描述:单表查询所有记录
3 * 作者:博客园-悟空聊架构
4 * 时间:2019-01-16
5 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
6 * 博客园:https://www.cnblogs.com/jackson0714
7 * */
8 @Test
9 public void testSelect() {
10 System.out.println(("----- 单表查询所有记录------"));
11 List<User> userList = userMapper.selectList(null);
12 Assert.assertEquals(6, userList.size()); //表里面的记录总条数是否等于6,如果等于6,则测试通过
13 userList.forEach(System.out::println);
14 }userMapper.selectList(null);
对应的SQL语句是:
SELECT id,name,age,email FROM user
如果找不到这些日志,可以清理日志后,再执行一遍看下。
数据库有6条记录

查询出总条数6条,测试通过

1 /*
2 * 描述:单表根据主键id查询单条记录
3 * 作者:博客园-悟空聊架构
4 * 时间:2019-01-16
5 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
6 * 博客园:https://www.cnblogs.com/jackson0714
7 * */
8 @Test
9 public void testSelectById() {
10 System.out.println(("----- 单表根据主键id查询单条记录 ------"));
11 User user = userMapper.selectById(2);
12 System.out.println(user);
13 }1 userMapper.selectById(2)
2 对应的SQL语句为
3 SELECT id,name,age,email FROM user WHERE id=? 
数据库中有一条记录

1 /*
2 * 描述:单表根据 id list 批量查询
3 * 作者:博客园-悟空聊架构
4 * 时间:2019-01-16
5 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
6 * 博客园:https://www.cnblogs.com/jackson0714
7 * */
8 @Test
9 public void testSelectByIds() {
10 System.out.println(("----- 单表根据 id list 批量查询 ------"));
11 List<Long> idsList = Arrays.asList(2L,4L,6L);
12 List<User> userList= userMapper.selectBatchIds(idsList);
13 userList.forEach(System.out::println);
14 }
userMapper.selectBatchIds(idsList);
对应的SQL语句为
SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? ) 查询结果,id=2,id=4 的查询出来了,没有 id=6 的记录

1 /*
2 * 描述:单表根据条件查询
3 * 作者:博客园-悟空聊架构
4 * 时间:2019-01-19
5 * Github:https://github.com/Jackson0714/study-mybatis-plus.git
6 * 博客园:https://www.cnblogs.com/jackson0714
7 * */
8 @Test
9 public void testSelectByMap() {
10 System.out.println(("----- 单表根据条件查询 ------"));
11 Map<String, Object> conditions = new HashMap<>();
12 conditions.put("name", "Jack");
13 conditions.put("age", 20);
14 List<User> userList= userMapper.selectByMap(conditions);
15 userList.forEach(System.out::println);
16 }userMapper.selectByMap(conditions);
对应的SQL语句为
SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
数据库有一条记录,name="Jack",age=20

注意:使用slectByMap时,条件里面的字段名需要和数据库保持一致。
不然会出现以下错误:

每天玩转3分钟 MyBatis-Plus - 1. 配置环境
每天玩转3分钟 MyBatis-Plus - 2. 普通查询
作 者:悟空聊架构
出 处:http://www.cnblogs.com/jackson0714/