1. 程式人生 > >springboot整合mybatis步驟

springboot整合mybatis步驟

1.匯入pom.xml依賴:

(1) 連結mysql的驅動

        <!--mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

(2) jdbc

        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

(3) mybatis的依賴

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

2.在springboot的預設配置檔案中配置:

(1) datasource

spring:
  datasource:
      url: jdbc:mysql://localhost:3306/witkey
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver

(2) mybatis的相關配置

#mybatis相關配置
mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.example.demo6.pojo
  configuration:
    map-underscore-to-camel-case: true

我的mapper.xml宣告在這兒:

    1) mapping配置X件

    2) 如果起別名

    3) 駝峰對映開關

3.Springboot的啟動類上添加註解@Mapperscan

4.dao層介面上添加註解@Repository

5·如果想列印mybatis的sql語句,怎麼設定?

在yml配置檔案裡設定:

#列印mybatis的sql語句
logging:
  level:
    com.example.demo6.mapper: debug

打印出的sql語句如圖:

over!