SpringBoot詳解整合MyBatis過程中可能遇到的問題
盡量不要用 jUnit 提供的單元測試
提一個要求盡量使用SpringBoot 提供的測試類進行測試,能夠自動掃描組件以及使用容器中的bean對象
還有如果有組件 中存在注入對象的話,那么必須在SpringBoot容器中取出 這個組件,進而使用注入的對象的功能?。?!
今天有個錯誤,花了很長時間來解決,最后發(fā)現(xiàn)是一個很低級很基礎的錯誤!
這是mapper接口,使用@mapper 相當于將接口的代理對象注冊進入bean中,但是上下文中找不到(其實是正常)
因為 @Mapper 這個注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是卻和 Mybatis 關聯(lián)不上。而且我們可以根據(jù) @Autowried 源碼看到,默認情況下,@Autowried 要求依賴對象必須存在,那么此時 IDEA 只能給個紅色警告了。
package com.bit.mapper;
import com.bit.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
User selectById(@Param("userid") Integer id);
}這是與mapper接口對應的xml文件,同樣也沒有問題
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bit.mapper.UserMapper">
<select id="selectById" resultType="com.bit.pojo.User">
select * from users where id = #{userid}
</select>
</mapper>將java目錄下的xml文件加入resource資源中,在build 標簽中嵌套,同樣沒有問題
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
然后我們寫service層,寫了一個UserService接口,有些了一個UserServiceImpl 接口的實現(xiàn)類
在這個實現(xiàn)類中,注入UserMapper 一直提示無法注入,我一直認為有問題(但是最后發(fā)現(xiàn)沒問題)

把service實現(xiàn)類寫完了,也沒問題
package com.bit.service;
import com.bit.mapper.UserMapper;
import com.bit.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public User queryById(Integer id) {
System.out.println("進入了service");
return userMapper.selectById(id);
}
}然后我直接去測試了,我測試的呢?
實例化了UserService,new了一個對象,然后直接調用方法,看是否能夠調用UserMapper查詢到數(shù)據(jù)庫。然后就不斷的包 空指針異常的錯誤
@SpringBootTest
class BitApplicationTests {
@Test
void contextLoads() {
UserService userService = new UserServiceImpl();
userService.queryById(13);
System.out.println(userService);
System.out.println(userService.queryById(15));
System.out.println(userService.queryById(13));
}
}
我一度以為是mapper接口沒有注入到UserServcie中,導致調用UserServcie的方法 就是調用 UserMapper的方法是空的,以為是Mapper接口的問題,各種搜索怎么解決,經(jīng)過幾個小時之后,在他人的博客中找到了答案
我們的UserMapper 注入到了 UserServiceImpl ,我們不能直接使用 UserServcieIml, 如果在其他的類中進行使用其功能,必須將這個類注入到 當前類中,從容器中拿到這個UserService,才能正確的進行調用,不會發(fā)生空指針異常,我一直沒有發(fā)現(xiàn),這是也該非常低級的錯誤。
正確做法: 先裝配到當前對象中,再從容器中拿到bean進行使用
@SpringBootTest
class BitApplicationTests {
@Autowired
private UserService userService;
@Test
void contextLoads() {
System.out.println(userService.queryById(15));
System.out.println(userService.queryById(13));
}
}到此這篇關于SpringBoot詳解整合MyBatis過程中可能遇到的問題的文章就介紹到這了,更多相關SpringBoot MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot調用第三方WebService接口的兩種方法
本文主要介紹了SpringBoot調用第三方WebService接口的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
Maven管理多模塊應用的統(tǒng)一版本號實現(xiàn)
本文主要介紹了Maven管理多模塊應用的統(tǒng)一版本號實現(xiàn),使用versions-maven-plugin插件和占位符結合flatten-maven-plugin插件來實現(xiàn),感興趣的可以了解一下2024-12-12
startJVM錯誤Unable to load native library: libjvm.so解決方法
這篇文章主要介紹了startJVM錯誤Unable to load native library: libjvm.so解決方法,需要的朋友可以參考下2014-07-07
Java實現(xiàn)獲取銀行卡所屬銀行,驗證銀行卡號是否正確的方法詳解
這篇文章主要介紹了Java實現(xiàn)獲取銀行卡所屬銀行,驗證銀行卡號是否正確的方法,結合實例形式詳細分析了java判斷銀行卡歸屬地及有效性的原理與相關實現(xiàn)技巧,需要的朋友可以參考下2019-09-09

