MyBatis Plus简介与搭建

简介

国产的开源框架,基于 MyBatis
核⼼功能就是简化 MyBatis 的开发,提⾼效率。

搭建

Spring Boot(2.3.0) + MyBatis Plus(国产的开源框架,并没有接⼊到 Spring 官⽅孵化器中)
1、创建 Maven ⼯程
MyBatis Plus简介与搭建
2、pom.xml 引⼊ MyBatis Plus 的依赖

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

3、创建实体类

package com.jq.mybatisplus.entity;

import lombok.Data;

@Data
public class Employee {
    private Long id;
    private String name;
    private Integer gender;
    private String pwd;
    private String email;
    private String company;
}

4、创建 Mapper 接⼝

package com.jq.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jq.mybatisplus.entity.Employee;

public interface EmployeeMapper extends BaseMapper<Employee> {
    
}

5、application.yml

spring:
 datasource:
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: jdbc:mysql://localhost:3306/qt?useUnicode=true&characterEncoding=UTF-8
 username: root
 password: root
 
#打印日志
mybatis-plus:
 configuration:
 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

6、启动类需要添加 @MapperScan(“mapper所在的包”),否则⽆法加载 Mppaer bean。

package com.jq.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.jq.mybatisplus.mapper")
public class MybatisplusApplication {

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

}

7、测试

package com.jq.mybatisplus.mapper;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class EmployeeMapperTest {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void test() {
        employeeMapper.selectList(null).forEach(System.out::println);
    }
}
上一篇:MybatisPlus相关个人笔记


下一篇:MyBatisPlus 中的 resultMap 使用