mybaits六:参数处理
單個(gè)參數(shù)和多個(gè)參數(shù)
? ??單個(gè)參數(shù)
? ? ? ? ? mybatis不多做特殊處理,通過 #{參數(shù)名},取出參數(shù)值
<selectid="getEmployeeById" resultType="emp" >select * from DEPTTEST where deptno = #{deptno}</select>? ?多個(gè)參數(shù)
? ? ? ? mybatis會做特殊處理,多個(gè)參數(shù)會被封裝成一個(gè)map.
? ? ? ? ? ? ? ? ? ? ? 1.map中的key沒有被命名
? ? ? ? ? ? ? ? ? ? ? ? map中的key: ?param1, param2......paramN
? ? ? ? ? ? ? ? ? ? ? ?map中的value: 傳入的參數(shù)值
? ? ? ? ? ? ? ? ? ? ? #{}就是從map中獲取指定key的值
<selectid="getEmployeeByIdAnd" resultType="emp" >select * from DEPTTEST where deptno = #{param1} and dname=#{param2}</select>? ? ? ? ? ? ? ? ? ?2. map中的key被命名(使用@Param("參數(shù)名"))
? ? ? ? ? ? ? ? ? ? ? ? ? ? 明確指定封裝參數(shù)時(shí)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}取出對應(yīng)值
<selectid="getEmployeeByIdAnd" resultType="emp" >select * from DEPTTEST where deptno = #{depno} and dname=#{dname}</select>? ? ? ? ? ? ? ? ? ? ?3. 傳遞pojo
? ? ? ? ? ? ? ? ? ? ?如果多個(gè)參數(shù)正好是我們業(yè)務(wù)邏輯的數(shù)據(jù)模型,我們就可以直接傳入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的屬性名}取出參數(shù)值
<update id="updateEmp">update DEPTTEST a set dname=#{dname}, loc=#{loc}where deptno = #{deptno}</update>? ? ?
4.直接傳入map
?如果多個(gè)參數(shù)不是業(yè)務(wù)邏輯的數(shù)據(jù)模型,如果該方法不經(jīng)常使用, 為了方便,我們就可以直接傳入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)
? ?如果多個(gè)參數(shù)不是業(yè)務(wù)邏輯的數(shù)據(jù)模型,如果該方法經(jīng)常使用, 為了方便,我們就可以直接傳入TO(transfer object)
?
注意場景:?
?
?#{}與${} 區(qū)別
都可以獲取map中的值或者pojo對象屬性的值
? ? ??#{}:是以預(yù)編譯的形式,將參數(shù)設(shè)置到sql語句中。類似于 原生jdbc中的PreparedStatement 防止sql注入.
? ? ?${}:取出的值直接拼裝在sql語句中,會有安全問題。
? ? ?
#{} 更豐富的用法:
? 規(guī)定參數(shù)的一些規(guī)則: javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName,expression
??在Oracle中有些字段不是必填時(shí)在用戶使用的時(shí)候會出現(xiàn)數(shù)據(jù)null的情況。這個(gè)時(shí)候在Oracle中是無法進(jìn)行插入的。因?yàn)閙ybatis默認(rèn)對所有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>? ?或者 在全局配置文件中設(shè)置setting
<settings><setting name="jdbcTypeForNull" value="NULL"/></settings>?
總結(jié)
以上是生活随笔為你收集整理的mybaits六:参数处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybaits五:主键的自增
- 下一篇: mybaits七:select查询返回l