解讀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ù)庫中的字段進行匹配。
而在查詢中我們選擇 使用結(jié)果集resultMap來進行數(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; }
這里我們重點解釋一下
@Param注解
這里我們應(yīng)該,采用#{}的方式把@Param注解括號內(nèi)的參數(shù)進行引用
對應(yīng)的是使用方法,上述代碼中明確標(biāo)出。
這里我們也可以不使用@Param注解,前提是數(shù)據(jù)庫字段與實體類字段一一對應(yīng),例如 數(shù)據(jù)庫命名為user_name,而我們的實體類就應(yīng)該使用 駝峰命名 userName,這樣就可以使mybatis將查詢的結(jié)果自發(fā)的進行一一對應(yīng)填充置user 對象中。
但是由于此案例,數(shù)據(jù)庫字段和實體類字段之間的命名方式,使得mybatis 從數(shù)據(jù)庫中查出的字段,不能正常的識別和對應(yīng)正確的實體類字段,于是,我們需要使用@Param注解來指明,我們所要引用的參數(shù)的名稱,方便再mapper.xml文件中,采用#{}的方式把@Param注解括號內(nèi)的參數(shù)進行引用,
這樣會更方便代碼的閱讀,也便于避免一些未知錯誤的發(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 的方式來對 對象進行判空。
而當(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 進行匹配
- 2. mapper 文件中 參數(shù)類型,與xml 文件中的參數(shù)類型匹配
- 3. mapper 文件中 返回值類型,與xml文件中的 返回值類型匹配
- 4. XML 文件的 namespace 指定的路徑與 相應(yīng)的實體類對應(yīng)
- 5. mapper 文件 和xml文件不在同一個包下的,要在配置文件中,將兩個文件的包同時進行掃描
還有一些較為簡單的異常不一一例舉,但說明相應(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接口,面向接口編程的思想還是很重要的。也是本次博文最重要的部分。
接口定義有以下特點:
- 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)文章
spring boot + jpa + kotlin入門實例詳解
這篇文章主要介紹了spring boot + jpa + kotlin入門實例詳解 ,需要的朋友可以參考下2017-07-07Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉(zhuǎn)
這篇文章主要介紹了Spring?Cloud?Gateway編碼實現(xiàn)任意地址跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2023-06-06