mybaits六:参数处理

单个参数和多个参数

    单个参数

          mybatis不多做特殊处理,通过 #{参数名},取出参数值

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

   多个参数

        mybatis会做特殊处理,多个参数会被封装成一个map.

                      1.map中的key没有被命名

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

                       map中的value: 传入的参数值

                      #{}就是从map中获取指定key的值

<select
		id="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}取出对应值

<select
		id="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映射文件

扫描二维码关注公众号,回复: 5722434 查看本文章
<select
		id="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;
	}

@Test
	public 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>

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/88913110