詳解Mybatis(五)Mapper接口
(1)Mapper接口和原理
Mapper組建
- 1、Mapper文件和Mapper接口應(yīng)該放在同一個接口中
- 2、Mapper文件中的namespace應(yīng)該設(shè)置為Mapper接口的全限定名稱
- 3、Mapper文件中的操作元素ID對應(yīng)Mapper接口的方法名稱
Mapper原理:
動態(tài)代理
(2)配置文件
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,namespace的值習(xí)慣上設(shè)置成包名+sql映射文件名,這樣就能夠保證namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后綴)
-->
<mapper namespace="Mybatis.domain.Mapper.UserMapper">
<resultMap type="User" id="BaseResultMap">
<result column="t_id" property="id"/>
<result column="t_name" property="name"/>
<result column="t_salary" property="salary"/>
</resultMap>
<!-- 保存操作 -->
<insert id="save" useGeneratedKeys="true" keyProperty="id">
INSERT INTO t_user (name , salary) VALUES (#{name},#{salary})
</insert>
<!-- 更改操作 -->
<update id="update">
update t_user where name=#{name},salary=#{salary} where id=#{id}
</update>
<!-- 刪除操作 -->
<delete id="delete" >
delete from t_user where id=#{id}
</delete>
<!-- 查詢單個操作 -->
<select id="select" parameterMap="java.lang.Long" resultType="Mybatis.domain.User">
select * from t_user where id = #{id}
</select>
<!-- 查詢多個操作 -->
<select id="selectAll" resultType="User">
select id,name,salary from t_user
</select>
</mapper>
UserMapper.java
import java.util.List;
import Mybatis.domain.User;
public interface UserMapper {
void save(User u);
void update(User u);
void delete(Long id);
User select(User u);
List<User> selectAll();
}
以上所述是小編給大家介紹的Mybatis Mapper接口詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Mybatis Mapper接口工作原理實例解析
- Mybatis MapperScannerConfigurer自動掃描Mapper接口生成代理注入到Spring的方法
- 詳解mybatis通過mapper接口加載映射文件
- mybatis如何通過接口查找對應(yīng)的mapper.xml及方法執(zhí)行詳解
- 詳解MyBatis的getMapper()接口、resultMap標簽、Alias別名、 盡量提取sql列、動態(tài)操作
- 在IDEA中安裝MyBatis Log Plugin插件,執(zhí)行mybatis的sql語句(推薦)
- mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實現(xiàn)
- Mybatis mapper接口動態(tài)代理開發(fā)步驟解析
相關(guān)文章
java遍歷途中修改數(shù)據(jù)及刪除數(shù)據(jù)的方法總結(jié)
在使用java的集合類遍歷數(shù)據(jù)的時候,在某些情況下可能需要對某些數(shù)據(jù)進行刪除,下面這篇文章主要給大家介紹了關(guān)于java遍歷途中修改數(shù)據(jù)及刪除數(shù)據(jù)的方法總結(jié),需要的朋友可以參考下2023-10-10
簡單了解Java synchronized關(guān)鍵字同步
這篇文章主要介紹了簡單了解Java synchronized關(guān)鍵字同步,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09
springboot配置請求超時時間(Http會話和接口訪問)
本文主要介紹了springboot配置請求超時時間,包含Http會話和接口訪問兩種,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Java工作環(huán)境的配置與Eclipse的安裝過程
這篇文章主要介紹了Java工作環(huán)境的配置與Eclipse的安裝過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
通過FeignClient調(diào)用微服務(wù)提供的分頁對象IPage報錯的解決
這篇文章主要介紹了通過FeignClient調(diào)用微服務(wù)提供的分頁對象IPage報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
消息隊列 RabbitMQ 與 Spring 整合使用的實例代碼
本篇文章主要介紹了消息隊列 RabbitMQ 與 Spring 整合使用的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Spring Cloud Gateway重試機制的實現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway重試機制的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

