SpringBoot整合、快速集成Mybatis-Plus

2019-06-18  本文已影响0人  _灯火阑珊处

Mybatis-Plus官网-3.0

Mybatis-Plus2.0文档

特性

1、添加依赖

Mybatis-Plus Maven中央仓库
JDK8添加3.x的依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.1</version>
</dependency>

JDK7及以下版本添加2.x依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>2.3.3</version>
</dependency>

2、application.yml添加Mysql配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mp_test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456

3、添加实体类

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

    // 省略get set方法

}

4、添加Mapper类,UserMapper继承BaseMapper,泛型为User实体类

public interface UserMapper extends BaseMapper<User> {

}

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

@SpringBootApplication
@MapperScan("com.wuxl.sync.mapper")
public class SynchronousDataApplication {

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

}

6、测试

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

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }

}
上一篇 下一篇

猜你喜欢

热点阅读