zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    

      

  • 相关阅读:
    线段树专辑—— pku 1436 Horizontally Visible Segments
    线段树专辑——pku 3667 Hotel
    线段树专辑——hdu 1540 Tunnel Warfare
    线段树专辑—— hdu 1828 Picture
    线段树专辑—— hdu 1542 Atlantis
    线段树专辑 —— pku 2482 Stars in Your Window
    线段树专辑 —— pku 3225 Help with Intervals
    线段树专辑—— hdu 1255 覆盖的面积
    线段树专辑—— hdu 3016 Man Down
    Ajax跨域访问
  • 原文地址:https://www.cnblogs.com/figsprite/p/10736976.html
Copyright © 2011-2022 走看看