SpringBoot添加對Mybatis的支持

一、修改maven配置文件pom.xml,添加對mybatis的支持:java

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

我鏈接的是mysql數據庫,還須要添加mysql驅動的支持:mysql

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

 

二、修改springboot配置文件application.properties,添加以下內容:spring

#mybatis configuration
mybatis.mapper-locations=classpath:com/example/mapping/*Mapper.xml 
mybatis.type-aliases-package=com.example.dao

mybatis.mapper-locations是指的Mapper資源文件存放的路徑sql

mybatis.type-aliases-package是指的dao接口存放的路徑數據庫

 

三、修改springboot的入口程序:springboot

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.example.dao*")
@SpringBootApplication
public class DemoApplication  {

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

紅色內容是要添加的內容,com.example.dao是個人項目dao接口存放的路徑mybatis

這個註解內容也能夠不添加,不添加的話,就須要在對應的dao接口前添加@Mapper註解,以下圖如示:app

不過通常不建議這麼用,最好仍是直接修改springboot的入口文件,統一掃描maven

 

經過以上三步,就完成了在SpringBoot中對Mybatis的支持了。spring-boot

相關文章
相關標籤/搜索