java獲取post請求的請求體,MyBatis獲取參數值的兩種方式以及傳參情況

 2023-10-17 阅读 25 评论 0

摘要:MyBatis獲取參數值的兩種方式MyBatis獲取參數值的兩種方式:${}和#{}傳參情況演示環境1.單個字面量類型的參數2.多個字面量類型的參數3.map集合類型的參數4.實體類類型的參數5.使用@Param標識參數 MyBatis獲取參數值的兩種方式:${}和#{} java獲取post請求的

MyBatis獲取參數值的兩種方式

  • MyBatis獲取參數值的兩種方式:${}和#{}
  • 傳參情況
    • 演示環境
    • 1.單個字面量類型的參數
    • 2.多個字面量類型的參數
    • 3.map集合類型的參數
    • 4.實體類類型的參數
    • 5.使用@Param標識參數

MyBatis獲取參數值的兩種方式:${}和#{}

java獲取post請求的請求體、(1) ${}的本質就是字符串拼接,#{}的本質就是占位符賦值。
(2) ${}使用字符串拼接的方式拼接sql,若為字符串類型或日期類型的字段進行賦值時,需要手動加單引號。

<select id="selectPersonByName" resultType="Person">select * from t_person where name = '${name}'
</select>

(3) #{}使用占位符賦值的方式拼接sql,此時為字符串類型或日期類型的字段進行賦值時,可以自動添加單引號。

    <select id="selectPersonByName" resultType="Person"><!--select * from t_person where name = '${name}'-->select * from t_person where name = #{name}</select>

傳參情況

演示環境

???????以下演示均已#{}方式獲取參數。
數據庫表
在這里插入圖片描述
pojo
在這里插入圖片描述
映射接口
在這里插入圖片描述

1.單個字面量類型的參數

函數的參數傳遞方式有幾種,???????以查詢為例,通過name查出一條數據。
接口方法

public interface PersonMapper {//按name查詢Person selectPersonByName(String name);
}

映射文件

<select id="selectPersonByName" resultType="Person">select * from t_person where name = #{name}
</select>

測試方法

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);mapper.selectPersonByName("zhangsan");

java怎么傳遞參數?說明
???????當參數是一個的時候#{}中可以填寫任意值,比如下方的寫法也是可以獲取的,但最好寫的見名知意。

<select id="selectPersonByName" resultType="Person">select * from t_person where name = #{aaa}
</select>

2.多個字面量類型的參數

???????以按name和age兩個參數進行查詢,當然多個參數也是類似的寫法,只是我演示所用的表只有兩個數據項。
接口方法

public interface PersonMapper {//按name和age進行查詢Person selectPersonByNameAge(String name,Integer age);
}

映射文件
(1)方式一

<select id="selectPersonByNameAge" resultType="Person">select * from t_person where name = #{arg0} and age=#{arg1}
</select>

函數參數有兩種。(2)方式二

<select id="selectPersonByNameAge" resultType="Person">select * from t_person where name = #{param1} and age=#{param2}
</select>

(3)方式三

<select id="selectPersonByNameAge" resultType="Person">select * from t_person where name = #{arg0} and age=#{param2}
</select>

測試方法

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);mapper.selectPersonByNameAge("zhangsan",20);

簡述方法參數傳遞的兩種情況、說明
???????若mapper接口中的方法參數為多個時,此時MyBatis會自動將這些參數放在一個map集合中,以arg0,arg1…為鍵,以參數為值;以param1,param2…為鍵,以參數為值;
???????因此只需要通過#{}訪問map集合的鍵就可以獲取相對應的值。
???????注意${}需要手動加單引號,param是從1開始的。

3.map集合類型的參數

???????這次以插入一條數據為例,插入name=“李四” age=23。
接口方法

public interface PersonMapper {//Mapint insertPersonMap(Map<String,Object> map);
}

映射文件

    <insert id="insertPersonMap">insert into t_person values(#{key1},#{key2})</insert>

方法的參數。測試方法

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);HashMap<String, Object> map = new HashMap<String, Object>();map.put("key1","李四");map.put("key2",23);mapper.insertPersonMap(map);

說明
???????若mapper接口中的方法需要的參數為多個時,此時可以手動創建map集合,將這些數據放在map中只需要通過和#{}訪問map集合的就可以獲取相對應的值,注意${}需要手動加單引號

4.實體類類型的參數

???????以插入一個Person對象為例,new Person(“wangwu”,66)
接口方法

public interface PersonMapper {//傳參對象int insertPerson(Person person);}

映射文件

 <insert id="insertPerson">insert into t_person values(#{name},#{age})
</insert>

測試方法

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);mapper.insertPerson(new Person("wangwu",66));

說明
???????若mapper接口中的方法參數為實體類對象時,此時可以使用#{},通過訪問實體類對象中的屬性名獲取屬性值,注意${}需要手動加單引號。

5.使用@Param標識參數

刪除年齡為66歲的。
接口方法

public interface PersonMapper {//@paramint deletePersonByAge(@Param("Abb") Integer age);}

映射文件
(1)方式一

    <delete id="deletePersonByAge"><!--最好見名知意,這里為了演示-->delete from t_person where age=#{Abb}</delete>

(2)方式二

    <delete id="deletePersonByAge">delete from t_person where age=#{param1}</delete>

測試方法

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);mapper.deletePersonByAge(66);

說明

???????可以通過@Param注解標識mapper接口中的方法參數。
(1)此時,會將這些參數放在map集合中,以@Param注解的value屬性值為鍵,以參數為值;
(2)以param1,param2…為鍵,以參數為值;只需要通過和#{}訪問map集合的鍵就可以獲取相對應的值,注意${}需要手動加單引號。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/142736.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息