SpringBoot整合Mybatis遇到的問題

初學SpringBoot,在集成Mybatis的過程中遇到了點問題:
出現問題之前的操作:使用Mybatis的generator自動生成dao層和entity層中的類和接口
在UserController中注入userMapper:
代碼如下:
@Autowired
private UserMapper userMapper;
運行SpringBoot應用,報錯!!!
問題如下:
Field userMapper in com.coder520.mamabike.user.controller.UserController required a
 bean of type 'com.coder520.mamabike.user.dao.UserMapper' that could not be found.
問題說注入失敗,找不到UserMapper。
解決問題步驟:
步驟一:.查看pom.xml文件中是否加了如下代碼:

<resource>
       <directory>src/main/java</directory>
           <includes>
              <include>**/*.xml</include>
           </includes>
    </resource>
無效;
步驟二:很多人說在SpringBoot主程序中添加@MapperScan("路徑"),或許這樣可以解決一些人的問題,但我的還是沒有解決。
步驟三:在UserMapper.java 中添加註解:@Mapper;
問題解決;
import org.apache.ibatis.annotations.Mapper;

      @Mapper
public interface UserMapper {
    int deleteByPrimaryKey(Long id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}
有人說是SpringBoot版本過高的原因,需要添加@Mapper,不知道所以然,但問題解決了。
運行成功圖:






發佈了38 篇原創文章 · 獲贊 4 · 訪問量 5881
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章