01.快速开始

快速开始

数据库表

现有一张 User 表,其对应的数据库 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');

依赖配置

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>Latest Version</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

在 application.yml 配置文件中添加 H2 数据库的相关配置:

# DataSource Config
spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema-h2.sql
    data: classpath:db/data-h2.sql
    url: jdbc:h2:mem:test
    username: root
    password: test

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }

}

编码

编写实体类 User.java:

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

编写 Mapper 类 UserMapper.java:

public interface UserMapper extends BaseMapper<User> {

}

添加测试类,进行功能测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,所以不填写就是无任何条件。

Service 案例

mp 框架同样提供了 service 层的封装支持,让我们能够简化 service 层的开发;service 接口继承 IService,service 实现类继承 ServiceImpl,IService 提供了所有通用常用的方法,包括批处理添加更新支持,以及 lambda 支持;DepartmentService 继承 IService:

public interface DepartmentService extends IService<Department> {
}

@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper,Department> implements DepartmentService {
}

@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加测试
     */
    @Test
    public void save(){
        Department department=new Department();
        department.setName("牛逼");
        department.setRemark("xxx");
        boolean save = departmentService.save(department);
        System.out.println(save);
    }

    /**
     * 修改测试
     */
    @Test
    public void updateById(){
        Department department=new Department();
        department.setId(9);
        department.setName("牛逼2");
        department.setRemark("xxx2");
        boolean save = departmentService.updateById(department);
        System.out.println(save);
    }

    /**
     * 删除
     */
    @Test
    public void removeById(){
        boolean b = departmentService.removeById(9);
        System.out.println(b);
    }

    /**
     * 查询
     */
    @Test
    public void list(){
        List<Department> list = departmentService.list();
        System.out.println(list);
    }

    /**
     * 批量添加
     */
    @Test
    public void saveBatch(){
        Department department1=new Department();
        department1.setName("牛逼1");
        department1.setRemark("xxx1");

        Department department2=new Department();
        department2.setName("牛逼2");
        department2.setRemark("xxx2");

        Department department3=new Department();
        department3.setName("牛逼3");
        department3.setRemark("xxx3");

        boolean b = departmentService.saveBatch(Arrays.asList(department1, department2, department3));
        System.out.println(b);
    }

    /**
     * 批量添加或者更新
     */
    @Test
    public void saveOrUpdateBatch(){
        Department department1=new Department();
        department1.setName("牛逼4");
        department1.setRemark("xxx4");

        Department department2=new Department();
        department2.setId(11);
        department2.setName("牛逼2x");
        department2.setRemark("xxx2x");

        boolean b = departmentService.saveOrUpdateBatch(Arrays.asList(department1, department2));
        System.out.println(b);
    }


}
下一页