MyBatis映射文件4(参数获取#{}和${}/select标签详解[返回类型为list])

参数获取

    之前我们都是采用#{}的方式进行参数传递,其实MyBatis还有另外的参数传递方式${}

使用方法相同,但是还是有很大区别的

这里做一个测试:

  1. <select id="getEmpByMap" resultType="com.figsprite.bean.Employee">  
  2.     select id,last_name lastName,gender,email from tb_employee where id = ${id} and last_name = #{lastName}  
  3. </select>  

查看log4j中的打印内容:

select id,last_name lastName,gender,email from tb_employee where id = 3 and last_name = ?

使用$取出来的值会直接拼装在sql语句中,而使用#取得值在sql语句中是个?号,可以防止sql注入,这个区别就类似于原生JDBC Statement和PrepareStatement的最大区别,即预编译。

$也不是一无是处,原生JDBC不支持占位符的地方我们可以使用${}进行取值,比如分表信息,还有order by 之后的内容,下面演示一下分表信息中${}的使用:

假设,一个公司有很多部门,每个部门存一张员工表,如果我们为每个部门都写一句相同的查询语句,就十分麻烦,因此,编写如下映射语句

<select id="getEmpByMap" resultType="com.figsprite.bean.Employee">
select id,last_name lastName,gender,email from #{table_name} where id = #{id} and last_name = #{lastName}
</select>

我们先用#{}试试

public void test7() throws IOException {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    SqlSession openSession = sqlSessionFactory.openSession();

	try {
        EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
        Map<String,Object> map = new HashMap<>();
        map.put("id",3);
        map.put("lastName","Hello");
        map.put("table_name","tb_employee");
        Employee e = mapper.getEmpByMap(map);
        System.out.println(e);
    }finally {
        openSession.close();
    }
}

  

select id,last_name lastName,gender,email from ? where id = ? and last_name = ?

运行发现报错,我们再换成${}发现就没有问题了

#{}取值时指定参数相关规则

    规定参数规则的一些规则

    javaType,jdbcType,mode(存储过程),numbericScale,resultMap,typeHandler…….

我们主要说说jdbcType,它在某种特定条件下被设置,比如数据库之间的差异,在我们数据为null的时候,有些数据库可能无法识别mybatis对null的默认处理,比如oracle(报错)

如果我们执意要用null传给Oracle会报,JdbcType OTHER:无效类型,的错误,因为Mybatis对所有null都映射的是原生JDBC的 OTHER,Oracle无法识别,所以需要更改

insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL)

    values (#{id},#{lastName},#{email,jdbcType=NULL})

全局配置文件setting中有一项jdbcTypeForNull的属性进行更改,改为NULL

 

 

select标签

返回list类型对象

 

    List<Employee> getEmpByLastNameLike(String lastName);

 

<select id="getEmpByLastNameLike" resultType="com.figsprite.bean.Employee">
select * from tb_employee where last_name like #{lastName}
</select>

 

这里需要注意的是,resultType填写的是List中的元素类型,

@Test
public void test8() throws IOException {
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    SqlSession openSession = sqlSessionFactory.openSession();

	try {
        EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
        List<Employee> list = mapper.getEmpByLastNameLike("%吴%");

	for (Employee e : list) {
            System.out.println(e);
        }
    } finally {
        openSession.close();
    }
}

  

 

转载于:https://www.cnblogs.com/figsprite/p/10736976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值