MyBatis Plus(一)~从零开始实现表的增删改查

前提:有一张User表 ,结构如下:

id name age email
1 Jone 18 [email protected]
2 Jack 20 [email protected]
3 Tom 28 [email protected]
4 Sandy 21 [email protected]
5 Billie 24 [email protected]

其对应的数据库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, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

》》以下是实现User表的CRUD功能步骤,甚至连XML文件都不用编写!

1.创建一个空的 Spring Boot 工程(工程将以 H2 作为默认数据库进行演示)

    说明:可以使用 Spring Initializr 快速初始化一个 Spring Boot 工程

2.添加依赖

    引入 Spring Boot Starter 父工程:

    
            org.springframework.boot
            spring-boot-starter-parent
            2.0.3.RELEASE
            
    

    引入 spring-boot-starterspring-boot-starter-testmybatis-plus-boot-starterlombokh2 依赖:

    
        
                org.springframework.boot
                spring-boot-starter
        

        
                org.springframework.boot
                spring-boot-starter-test
                test
        

        
                org.projectlombok
                lombok
                true
        

        
                com.baomidou
                mybatis-plus-boot-starter
                3.0.6
        

        
                com.h2database
                h2
                runtime
        

    

3.配置

    在 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);
            }
      }

4.编码

    编写实体类 User.java(此处使用了 Lombok 简化代码)

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

    编写Mapper类 UserMapper.java
    public interface UserMapper extends BaseMapper {

    }

5.开始使用

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

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

            @Autowired
            private UserMapper userMapper;

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

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

    控制台输出:

            User(id=1, name=Jone, age=18, [email protected])
            User(id=2, name=Jack, age=20, [email protected])
            User(id=3, name=Tom, age=28, [email protected])
            User(id=4, name=Sandy, age=21, [email protected])
            User(id=5, name=Billie, age=24, [email protected])

    

    

转载于:https://my.oschina.net/tobeexpertismydream/blog/2982576

你可能感兴趣的:(MyBatis Plus(一)~从零开始实现表的增删改查)