SpringBoot项目整合Mybatis时XXXMapper.xml文件存放位置区别

211 阅读2分钟

本篇文章使用java8,SpringBoot2.7.6版本

创建mapper接口:

package com.blog.mapper;

import com.blog.entity.Category;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface CategoryMapper {
    public List<Category> findAll();
} 

我们一般在创建XXXMapper.java接口后,需要在接口上面添加@Mapper注解,用于Spring容器扫描我们编写的接口,但随着我们每创建一个Mapper接口,就要添加一次注解,稍微有一点麻烦,这里我们使用一个快捷的方式,如下:
在我们项目启动入口,XXXApplication.java中添加一个@MapperScan注解:

package com.blog;

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

@MapperScan("com.blog.mapper") //在项目启动时扫描这个路径下的Mapper文件
@SpringBootApplication
public class BlogApplication {

    public static void main(String[] args) {
        try {
            SpringApplication.run(BlogApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

使用@MapperScan注解之后,无需在接口处使用@Mapper注解了。

创建控制器

package com.blog.controller;

import com.blog.entity.Category;
import com.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    public List<Category> findAll() {
        return categoryService.findAll();
    }
} 

创建service

package com.blog.service.impl;

import com.blog.entity.Category;
import com.blog.mapper.CategoryMapper;
import com.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    public CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }
} 

service接口:

package com.blog.service;

import com.blog.entity.Category;

import java.util.List;

public interface CategoryService {
    public List<Category> findAll();
} 

方式一:在resources创建Mapper接口同名文件夹用来存放Mapper.xml文件

运行代码,发现可以正常运行,我们查看target目录,发现mapper接口和xml在同一个目录下:

方式二:放在与Mapper接口同级目录

其他不变,运行代码发现报错:

报错提示没有找到com.blog.mapper.CategoryMapper路径下的findAll方法可以进行绑定

原因可能是findAll方法名没有对应到,但是我们demo是对应到的,所以不是这个原因。

第二个原因就是没有找到对应CategoryMapper.xml文件,我们看target文件:

我们发现target路径下没有找到CategoryMapper.xml文件,解决方法是在pom.xml里面新增如下代码:

 <build>
    <resources>
        <!-- 扫描src/main/java下所有xx.xml文件 -->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <!-- 扫描resources下所有资源 -->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

加了之后,一定记得刷新maven

然后运行就正常了

方式三【推荐】:在resources目录下创建mapper文件夹存放mapper.xml

只要在配置文件application.yml里新增xml位置就行(方式二pom.xml的配置可以去掉):

mybatis:
  mapper-locations: classpath:mapper/*.xml

运行代码正常