MyBatis基础配置

2020-01-13  本文已影响0人  夜空中乄最亮的星

yml文件配置

依赖引入
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
</dependency>
MySQL连接配置
spring:
  datasource:
    url: jdbc:mysql://localhost/test?useAffectedRows=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

注意:

MyBatis的xml文件路径配置
mybatis:
  mapper-locations: classpath*:com/study/mybaits/mapper/xml/*.xml

注意:

仅仅配置完mapper的xml路径还不够,还有2点需要注意,否则会出问题;

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>
添加注解添加注解

在springboot启动类中添加注解 @MapperScan,指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类,其中包名是 dao接口的实现类所在的包名

@SpringBootApplication
@MapperScan(basePackages = "com.study.mybaits.mapper")
上一篇下一篇

猜你喜欢

热点阅读