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
映射接口
函數的參數傳遞方式有幾種,???????以查詢為例,通過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>
???????以按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開始的。
???????這次以插入一條數據為例,插入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集合的鍵就可以獲取相對應的值,注意${}需要手動加單引號
???????以插入一個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接口中的方法參數為實體類對象時,此時可以使用#{},通過訪問實體類對象中的屬性名獲取屬性值,注意${}需要手動加單引號。
刪除年齡為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集合的鍵就可以獲取相對應的值,注意${}需要手動加單引號。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态