解讀Mapper與Mapper.xml文件之間匹配的問題
Mapper與Mapper.xml文件之間匹配問題
這里我們做一個實例
user實體類
public class User {
private Integer id;
private String username;
private String password;
private String email;
private String phone;
數(shù)據(jù)庫user表

userMapper.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="wdc.dao.UserMapper">
<resultMap id="BaseResultMap" type="wdc.pojo.User">
<!-- column為數(shù)據(jù)庫字段名 property 為實體類字段名-->
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="user_name" property="username" jdbcType="VARCHAR"/>
<result column="user_password" property="password" jdbcType="VARCHAR"/>
<result column="user_email" property="email" jdbcType="VARCHAR"/>
<result column="user_phone" property="phone" jdbcType="VARCHAR"/>
</resultMap>
<!-- 這里 id 是數(shù)據(jù)庫字段名 #{id}是實體類字段名,sql語句后面不加封號-->
<select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select *from USER where id=#{id}
</select>
<!--此sql用于登錄查詢
#{username} 是對應(yīng)mapper接口文件中 @Param注解中的參數(shù)名(防止mapper之間不匹配)-->
<select id="login" parameterType="java.lang.String" resultMap="BaseResultMap">
select *from user where user_name=#{username} and user_password=#{password}
</select>
</mapper>
從上面不難看出,我們使用了resultMap作為映射集,目的是為了使得 實體類user 中的字段與數(shù)據(jù)庫中的字段進(jìn)行匹配。
而在查詢中我們選擇 使用結(jié)果集resultMap來進(jìn)行數(shù)據(jù)映射
<!-- 這里 id 是數(shù)據(jù)庫字段名 #{id}是實體類字段名,sql語句后面不加封號-->
<select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
而在另一個查詢中,我們在編寫sql語句時,使用了對應(yīng)userMapper文件中,@Param所對應(yīng)的參數(shù)名
<!-- #{username} 是對應(yīng)mapper接口文件中 @Param注解中的參數(shù)名(防止mapper之間不匹配)-->
<select id="login" parameterType="java.lang.String" resultMap="BaseResultMap">
select *from user where user_name=#{username} and user_password=#{password}
</select>
這里也是為了防止mapper 文件與 xml文件之間匹配出現(xiàn)異常。
userMapper
package wdc.dao;
import org.apache.ibatis.annotations.Param;
import wdc.pojo.User;
public interface UserMapper {
User selectUser(int id)throws Exception;
// 登錄
// @Param中的參數(shù)名,對應(yīng).xml文件中sql語句#{}中的參數(shù)名(防止mapper之間不匹配)
User login(@Param("username") String username, @Param("password") String password)throws Exception;
}
這里我們重點(diǎn)解釋一下
@Param注解
這里我們應(yīng)該,采用#{}的方式把@Param注解括號內(nèi)的參數(shù)進(jìn)行引用
對應(yīng)的是使用方法,上述代碼中明確標(biāo)出。
這里我們也可以不使用@Param注解,前提是數(shù)據(jù)庫字段與實體類字段一一對應(yīng),例如 數(shù)據(jù)庫命名為user_name,而我們的實體類就應(yīng)該使用 駝峰命名 userName,這樣就可以使mybatis將查詢的結(jié)果自發(fā)的進(jìn)行一一對應(yīng)填充置user 對象中。
但是由于此案例,數(shù)據(jù)庫字段和實體類字段之間的命名方式,使得mybatis 從數(shù)據(jù)庫中查出的字段,不能正常的識別和對應(yīng)正確的實體類字段,于是,我們需要使用@Param注解來指明,我們所要引用的參數(shù)的名稱,方便再mapper.xml文件中,采用#{}的方式把@Param注解括號內(nèi)的參數(shù)進(jìn)行引用,
這樣會更方便代碼的閱讀,也便于避免一些未知錯誤的發(fā)生。
測試
關(guān)于對象判空,和空指針異常(NullPointer)
@Test
public void testLogin() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:config/spring-mybatis.xml");
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
User user = null;
user = userMapper.login("張","123");
if(user == null)
System.out.println("沒數(shù)據(jù)");
else
System.out.println(user);
}
這里測試登錄接口后我們發(fā)現(xiàn),當(dāng)沒有數(shù)據(jù)返回時,我們可以直接 使用 user==null 的方式來對 對象進(jìn)行判空。
而當(dāng)我們在此代碼中,直接將異常使用 try catch 捕捉時,我們發(fā)現(xiàn),當(dāng)對象 user 為空時,控制臺并不會報出異常。
而當(dāng)我們在調(diào)用 user對象的toSring方法時,則會出現(xiàn)NullPoint的空指針異常
小結(jié):
導(dǎo)致mapper 與xml之間出現(xiàn)不匹配的原因主要有:
- 1. mapper 文件中 方法名與 xml 中查詢語句id 進(jìn)行匹配
- 2. mapper 文件中 參數(shù)類型,與xml 文件中的參數(shù)類型匹配
- 3. mapper 文件中 返回值類型,與xml文件中的 返回值類型匹配
- 4. XML 文件的 namespace 指定的路徑與 相應(yīng)的實體類對應(yīng)
- 5. mapper 文件 和xml文件不在同一個包下的,要在配置文件中,將兩個文件的包同時進(jìn)行掃描
還有一些較為簡單的異常不一一例舉,但說明相應(yīng)原因以供參考:
- 6. 數(shù)據(jù)庫密碼填寫錯誤
- 7. 數(shù)據(jù)庫服務(wù)未開啟
- 8. 數(shù)據(jù)庫路徑 以及亂碼問題
jdbc.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8
Mapper和Mapper.xml的關(guān)系
Category.java

CategoryMapper.java
這是mapper接口,面向接口編程的思想還是很重要的。也是本次博文最重要的部分。
接口定義有以下特點(diǎn):
- Mapper 接口方法名和 CategoryMapper.xml 中定義的每個 sql 的 id 同名。
- Mapper 接口方法的輸入?yún)?shù)類型和 CategoryMapper.xml 中定義的 sql 的parameterType 類型相同。
- Mapper 接口的返回類型和 CategoryMapper.xml 中定義的 sql 的 resultType 類型相同
CategoryMapper.xml
1.xml文件的namespace要寫成mapper接口的路徑。

2.sql的id和mapper中的方法名要對應(yīng)起來,比如下面,mapper中方法名為add,insert的sql標(biāo)簽id也要為add

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合shiro實現(xiàn)記住我功能
這篇文章主要介紹了springboot整合shiro實現(xiàn)記住我功能,配置類 ShiroConfig,通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
解決java.util.zip.ZipException: Not in GZIP&nbs
這篇文章主要介紹了解決java.util.zip.ZipException: Not in GZIP format報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
JavaWeb三大組件之監(jiān)聽器Listener詳解
這篇文章主要介紹了JavaWeb三大組件之監(jiān)聽器Listener詳解,在JavaWeb應(yīng)用程序中,Listener監(jiān)聽器是一種機(jī)制,用于監(jiān)聽和響應(yīng)特定的事件,它可以感知并響應(yīng)與應(yīng)用程序相關(guān)的事件,從而執(zhí)行相應(yīng)的邏輯處理,需要的朋友可以參考下2023-10-10
Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解
這篇文章主要為大家介紹了Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
SpringBoot測試之@SpringBootTest與MockMvc的實戰(zhàn)應(yīng)用小結(jié)
本文將深入探討SpringBoot測試中兩個核心工具:@SpringBootTest注解與MockMvc測試框架的實戰(zhàn)應(yīng)用,幫助開發(fā)者構(gòu)建更穩(wěn)健的測試體系,提高代碼質(zhì)量與可維護(hù)性,感興趣的朋友一起看看吧2025-03-03
jstack報錯Unable to open socket file解決
這篇文章主要為大家介紹了jstack報錯Unable to open socket file的解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02

