詳解mybatis通過(guò)mapper接口加載映射文件
通過(guò) mapper 接口加載映射文件,這對(duì)于后面 ssm三大框架 的整合是非常重要的。那么什么是通過(guò) mapper 接口加載映射文件呢?
我們首先看以前的做法,在全局配置文件 mybatis-configuration.xml 通過(guò) <mappers> 標(biāo)簽來(lái)加載映射文件,那么如果我們項(xiàng)目足夠大,有很多映射文件呢,難道我們每一個(gè)映射文件都這樣加載嗎,這樣肯定是不行的,那么我們就需要使用 mapper 接口來(lái)加載映射文件
以前的做法:
改進(jìn)做法:使用 mapper 接口來(lái)加載映射文件
1、定義 userMapper 接口
package com.ys.mapper; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.ys.po.User; public interface UserMapper { //根據(jù) id 查詢(xún) user 表數(shù)據(jù) public User selectUserById(int id) throws Exception; //向 user 表插入一條數(shù)據(jù) public void insertUser(User user) throws Exception; //根據(jù) id 修改 user 表數(shù)據(jù) public void updateUserById(User user) throws Exception; //根據(jù) id 刪除 user 表數(shù)據(jù) public void deleteUserById(int id) throws Exception; }
2、在全局配置文件 mybatis-configuration.xml 文件中加載 UserMapper 接口(單個(gè)加載映射文件)
3、編寫(xiě)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="com.ys.mapper.UserMapper"> <!-- 根據(jù) id 查詢(xún) user 表中的數(shù)據(jù) id:唯一標(biāo)識(shí)符,此文件中的id值不能重復(fù) resultType:返回值類(lèi)型,一條數(shù)據(jù)庫(kù)記錄也就對(duì)應(yīng)實(shí)體類(lèi)的一個(gè)對(duì)象 parameterType:參數(shù)類(lèi)型,也就是查詢(xún)條件的類(lèi)型 --> <select id="selectUserById" resultType="com.ys.po.User" parameterType="int"> <!-- 這里和普通的sql 查詢(xún)語(yǔ)句差不多,后面的 #{id}表示占位符,里面不一定要寫(xiě)id,寫(xiě)啥都可以,但是不要空著 --> select * from user where id = #{id1} </select> <!-- 根據(jù) id 更新 user 表的數(shù)據(jù) --> <update id="updateUserById" parameterType="com.ys.po.User"> update user u <!-- <set> <if test="username != null and username != ''"> u.username = #{username}, </if> <if test="sex != null and sex != ''"> u.sex = #{sex} </if> </set> --> <trim prefix="set" suffixOverrides=","> <if test="username != null and username != ''"> u.username = #{username}, </if> <if test="sex != null and sex != ''"> u.sex = #{sex}, </if> </trim> where id=#{id} </update> <!-- 向 user 表插入一條數(shù)據(jù) --> <insert id="insertUser" parameterType="com.ys.po.User"> <!-- 將插入的數(shù)據(jù)主鍵返回到 user 對(duì)象中 keyProperty:將查詢(xún)到的主鍵設(shè)置到parameterType 指定到對(duì)象的那個(gè)屬性 select LAST_INSERT_ID():查詢(xún)上一次執(zhí)行insert 操作返回的主鍵id值,只適用于自增主鍵 resultType:指定 select LAST_INSERT_ID() 的結(jié)果類(lèi)型 order:AFTER,相對(duì)于 select LAST_INSERT_ID()操作的順序 --> <selectKey keyProperty="id" resultType="int" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into user(username,sex,birthday,address) value(#{username},#{sex},#{birthday},#{address}) </insert> <!-- 根據(jù) id 刪除 user 表的數(shù)據(jù) --> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
4、測(cè)試
//根據(jù)id查詢(xún)user表數(shù)據(jù) @Test public void testSelectUserById(){ /*這個(gè)字符串由 userMapper.xml 文件中 兩個(gè)部分構(gòu)成 <mapper namespace="com.ys.po.userMapper"> 的 namespace 的值 <select id="selectUserById" > id 值*/ String statement = "com.ys.mapper.UserMapper.selectUserById"; User user = session.selectOne(statement, 1); System.out.println(user); session.close(); }
5、批量加載映射文件
<mappers> <!--批量加載mapper 指定 mapper 接口的包名,mybatis自動(dòng)掃描包下的mapper接口進(jìn)行加載 --> <package name="com.ys.mapper"/> </mappers>
6、注意
1、UserMapper 接口必須要和 UserMapper.xml 文件同名且在同一個(gè)包下,也就是說(shuō) UserMapper.xml 文件中的namespace是UserMapper接口的全類(lèi)名
2、UserMapper接口中的方法名和 UserMapper.xml 文件中定義的 id 一致
3、UserMapper接口輸入?yún)?shù)類(lèi)型要和 UserMapper.xml 中定義的 parameterType 一致
4、UserMapper接口返回?cái)?shù)據(jù)類(lèi)型要和 UserMapper.xml 中定義的 resultType 一致
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis映射文件詳解之mapper.xml文件
- Java?Mybatis的初始化之Mapper.xml映射文件的詳解
- 解決Mybatis映射文件mapper.xml中的注釋問(wèn)題
- mybatis中映射文件(mapper)中的使用規(guī)則
- mybatis整合spring實(shí)現(xiàn)開(kāi)啟mapper.xml映射文件掃描
- myBatis的mapper映射文件之批量處理方式
- mybatis映射文件mapper.xml的具體寫(xiě)法
- 解決Mybatis在IDEA中找不到mapper映射文件的問(wèn)題
- Mybatis中Mapper映射文件使用詳解
- MyBatis Mapper映射文件配置的實(shí)現(xiàn)
相關(guān)文章
springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例
本文主要介紹了springBoot+mybaties后端多層架構(gòu)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07項(xiàng)目打包成jar后包無(wú)法讀取src/main/resources下文件的解決
本文主要介紹了項(xiàng)目打包成jar后包無(wú)法讀取src/main/resources下文件的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java實(shí)現(xiàn)Map遍歷key-value的四種方法
本文主要介紹了Java實(shí)現(xiàn)Map遍歷key-value的四種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java如何實(shí)現(xiàn)HTTP斷點(diǎn)續(xù)傳功能
其實(shí)斷點(diǎn)續(xù)傳的原理很簡(jiǎn)單,就是在Http的請(qǐng)求上和一般的下載有所不同而已,本文將詳細(xì)介紹Java如何實(shí)現(xiàn)HTTP斷點(diǎn)續(xù)傳功能,需要的朋友可以參考下2012-11-11Java使用StampedLock實(shí)現(xiàn)高效讀寫(xiě)功能
StampedLock 是 Java 8 引入的高性能鎖,提供了三種鎖模式:寫(xiě)鎖、悲觀(guān)讀鎖和樂(lè)觀(guān)讀鎖,與傳統(tǒng)的 ReentrantReadWriteLock 相比,StampedLock 更注重性能,特別適合讀多寫(xiě)少的場(chǎng)景,所以本文給大家介紹了Java使用StampedLock實(shí)現(xiàn)高效讀寫(xiě)功能,需要的朋友可以參考下2025-01-01springboot項(xiàng)目如何使用切面記錄用戶(hù)操作日志
這篇文章主要介紹了springboot項(xiàng)目如何使用切面記錄用戶(hù)操作日志,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java并發(fā)編程示例(九):本地線(xiàn)程變量的使用
這篇文章主要介紹了Java并發(fā)編程示例(九):本地線(xiàn)程變量的使用,有時(shí),我們更希望能在線(xiàn)程內(nèi)單獨(dú)使用,而不和其他使用同一對(duì)象啟動(dòng)的線(xiàn)程共享,Java并發(fā)接口提供了一種很清晰的機(jī)制來(lái)滿(mǎn)足此需求,該機(jī)制稱(chēng)為本地線(xiàn)程變量,需要的朋友可以參考下2014-12-12如何通過(guò)ServletInputStream讀取http請(qǐng)求傳入的數(shù)據(jù)
這篇文章主要介紹了如何通過(guò)ServletInputStream讀取http請(qǐng)求傳入的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10