SpringBoot整合Mybatis-Plus

目录

(1)引入依赖

(2)yml文件配置

(3)编写Mapper接口,继承BaseMapper

(4)添加#MapperSan注解,扫描Mapper文件夹

 (5)测试方法


Mybatis-Plus官方推荐在SpringBoot工程中使用,Spring过程自然也是可以用的,这里我只介绍SpringBoot的使用        

(1)引入依赖

    
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.0
        
        
        
            mysql
            mysql-connector-java
            8.0.26
        
        
        
            org.projectlombok
            lombok
            true
        

(2)yml文件配置



# 数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///mybatis?serverTimezone=UTC
    username: root
    password: root


#日志格式
logging:
  pattern:
    console: '%d{HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'


(3)编写Mapper接口,继承BaseMapper

public interface UserMapper extends BaseMapper {

}

(4)添加#MapperSan注解,扫描Mapper文件夹

@SpringBootApplication
@MapperScan("com.gq.springnootdemo.mapper")
public class SpringnootdemoApplication {

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

}

 (5)测试方法


@SpringBootTest
class SpringnootdemoApplicationTests {

    @Autowired
    private UserMapper userMapper;
    @Test
    void contextLoads() {
        user users = userMapper.selectById(1);
        System.out.println(users);
    }

}

你可能感兴趣的:(Spring类型框架,spring,boot,mybatis,java)