日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybaits六:参数处理

發布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybaits六:参数处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單個參數和多個參數

? ??單個參數

? ? ? ? ? mybatis不多做特殊處理,通過 #{參數名},取出參數值

<selectid="getEmployeeById" resultType="emp" >select * from DEPTTEST where deptno = #{deptno}</select>

? ?多個參數

? ? ? ? mybatis會做特殊處理,多個參數會被封裝成一個map.

? ? ? ? ? ? ? ? ? ? ? 1.map中的key沒有被命名

? ? ? ? ? ? ? ? ? ? ? ? map中的key: ?param1, param2......paramN

? ? ? ? ? ? ? ? ? ? ? ?map中的value: 傳入的參數值

? ? ? ? ? ? ? ? ? ? ? #{}就是從map中獲取指定key的值

<selectid="getEmployeeByIdAnd" resultType="emp" >select * from DEPTTEST where deptno = #{param1} and dname=#{param2}</select>

? ? ? ? ? ? ? ? ? ?2. map中的key被命名(使用@Param("參數名"))

? ? ? ? ? ? ? ? ? ? ? ? ? ? 明確指定封裝參數時map的key的

import com.atChina.bean.Employee;public interface EmployeeMapper {public Employee getEmployeeByIdAnd(@Param("depno")Integer depno, @Param("dname")String dname);public Employee getEmployeeById(Integer depno);}

?#{指定的key}取出對應值

<selectid="getEmployeeByIdAnd" resultType="emp" >select * from DEPTTEST where deptno = #{depno} and dname=#{dname}</select>

? ? ? ? ? ? ? ? ? ? ?3. 傳遞pojo

? ? ? ? ? ? ? ? ? ? ?如果多個參數正好是我們業務邏輯的數據模型,我們就可以直接傳入pojo

package com.atChina.dao;import org.apache.ibatis.annotations.Param;import com.atChina.bean.Employee;public interface EmployeeMapper {public Integer updateEmp(Employee employee); }

? ? ?#{pojo的屬性名}取出參數值

<update id="updateEmp">update DEPTTEST a set dname=#{dname}, loc=#{loc}where deptno = #{deptno}</update>

? ? ?

4.直接傳入map

?如果多個參數不是業務邏輯的數據模型,如果該方法不經常使用, 為了方便,我們就可以直接傳入map

package com.atChina.dao;import java.util.Map;import org.apache.ibatis.annotations.Param;import com.atChina.bean.Employee;public interface EmployeeMapper {public Employee getEmployeeByMap(Map<String, Object> map); }

?sql映射文件

<selectid="getEmployeeByMap" resultType="emp" >select * from DEPTTEST where deptno = #{depno} and dname=#{dname}</select>

測試方法?

public SqlSessionFactory getSqlSessionFactory() throws IOException{String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);return sqlSessionFactory;}@Testpublic void test05() throws IOException{SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapper em = sqlSession.getMapper(EmployeeMapper.class);Map<String, Object> map = new HashMap<String, Object>();map.put("depno", 10);map.put("dname", "ACCOUNTING");em.getEmployeeByMap(map);}finally{sqlSession.close();}}

? ?5. 傳入TO(transfer object)

? ?如果多個參數不是業務邏輯的數據模型,如果該方法經常使用, 為了方便,我們就可以直接傳入TO(transfer object)

?

注意場景:?

?

?#{}與${} 區別

都可以獲取map中的值或者pojo對象屬性的值

? ? ??#{}:是以預編譯的形式,將參數設置到sql語句中。類似于 原生jdbc中的PreparedStatement 防止sql注入.

? ? ?${}:取出的值直接拼裝在sql語句中,會有安全問題。

? ? ?

#{} 更豐富的用法:

? 規定參數的一些規則: javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName,expression

??在Oracle中有些字段不是必填時在用戶使用的時候會出現數據null的情況。這個時候在Oracle中是無法進行插入的。因為mybatis默認對所有null都映射的是原生的Jdbc的Other類型

?可以通過 jdbcType=NULL

<insert id="addEmploy" parameterType="com.atChina.bean.Employee">insert into DEPTTEST(id, deptno, dname, loc) values(SEQU_DEPTTEST.nextval, #{deptno}, #{dname, jdbcType=NULL}, #{loc})</insert>

? ?或者 在全局配置文件中設置setting

<settings><setting name="jdbcTypeForNull" value="NULL"/></settings>

?

總結

以上是生活随笔為你收集整理的mybaits六:参数处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。