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!

猜你喜欢

转载自blog.csdn.net/qq_43154385/article/details/85260501
今日推荐