mybatis-plus集成

集成步骤

1、只需要引入的包

引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</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-generator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<!--mybatis-plus -->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.2.0</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

<!--lombok -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.18</version>
  <scope>provided</scope>
</dependency>

<!--swagger注解 -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>
View Code

2、application.yml配置

server:
  port: 8081

spring:
  datasource:
    username: root
    password: 111111
    url: jdbc:mysql://10.3.13.250:3306/service_center?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&allowMultiQueries=true
    driver-class-name: com.mysql.cj.jdbc.Driver

  #showSql
logging:
  level:
    com:
      uih:
        servicecenter:
           user:
             mapper: debug
View Code

3、启动处需要配置

package com.example.demo;

import com.example.demo.entity.Account;
import com.example.demo.service.IAccountService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
class DemoApplicationTests {

    @Autowired
    IAccountService accountService;

    @Test
    void contextLoads() {
        Account account=accountService.getById("1");
        System.out.println(account.getAge());
    }

}
View Code

4、需要注意的地方

Mapper层不需要再加@Mapper注解

Service层的实现接口的类处需要添加@Service注解,不然spring不知道该注入哪一个类

5、参考

http://www.ityouknow.com/springboot/2019/05/14/spring-boot-mybatis-plus.html

https://mp.baomidou.com/guide/install.html#release

 

posted @ 2019-11-28 21:34  弱水三千12138  阅读(207)  评论(0编辑  收藏  举报