zoukankan      html  css  js  c++  java
  • <MyBatis>入门四 传入的参数处理

    1.单个参数

      传入单个参数时,mapper文件中 #{}里可以写任意值

        /**
         * 传入单个参数
         */
        Employee getEmpById(Integer id);
        <!--单个参数 #{} 里可以写任意值-->
        <select id="getEmpById" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{abc}
        </select>

    2.多个参数

        /**
         * 传入多个参数
         */
        Employee getEmpByIdAndName(Integer id,String name);
        <select id="getEmpByIdAndName" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{id}
              AND last_name = #{name}
        </select>

      此时会报错 Caused by: org.apache.ibatis.binding.BindingException:Parameter 'id' not found. Available parameters are [arg1, arg0, param1, param2]

      传入多个参数时,mybatis会做特殊处理,

      将多个参数封装成一个map,key是param1...paramN#{},从map中获取指定的key值

    解决办法

      1、使用param1..paramN,

        <select id="getEmpByIdAndName" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{param1}
              AND last_name = #{param2}
        </select>

      2.使用方法1,并不能见名知意,所以推荐第二种方法

        命名参数,明确指定封装参数的map,

            key:使用@Param注解指定的值

            这样就可以通过#{}来取值

        /**
         * 传入多个参数
         */
        Employee getEmpByIdAndGender(@Param("id") Integer id, @Param("gender") Character gender);
        <select id="getEmpByIdAndGender" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{id}
              AND gender = ${gender}
        </select>

    3 .传入pojo

      如果多个参数正好是我们业务逻辑的数据模型,直接传入POJO,#{属性名}:取出POJO属性值

        /**
         * 传入pojo
         */
        Employee getEmpByIdAndNamePojo(Employee employee);
        <!--传入pojo对象-->
        <select id="getEmpByIdAndNamePojo" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{id}
              AND last_name = #{name}
        </select>

    4.传入Map

      如果多个参数不是业务模型中的数据,没有对应的POJO,不经常使用,为了方便,我们传入map

        /**
         * 传入map
         */
        Employee getEmpByMap(Map<String,Object> map);
        <!--传入map-->
        <select id="getEmpByMap" resultType="org.maple.pojo.Employee">
            SELECT id,last_name name,email,gender
            FROM tbl_employee
            WHERE id = #{id}
              AND last_name = #{name}
        </select>
              Map<String, Object> map = new HashMap<>();
                map.put("id",1);
                map.put("name","tom");
                Employee emp = mapper.getEmpByMap(map);

    5.传入TO(transfer Object)

      多个参数不是业务中的模型,但是要经常使用,推荐写一个TO,数据传输对象。

    6.思考

      

     7.${},#{}区别

      #{}:是以预编译的形式,将参数设置到sql语句中,PreparedStatement;防止sql注入

      ${}:取出的值直接拼装在sql语句中;会有安全问题

      大多情况下,取参数使用#{}

      比如分表,select * from ${year}_salary where xxx;

      原生不支持占位符的:select * from tbl_employee order by ${name} ${order} 

    8.#{}的用法

      

  • 相关阅读:
    接Oracle11g_client精简版安装步骤(2019年11月18日)之Plsql配置
    Oracle11g_client精简版安装步骤(2019年11月18日)
    PC端微信版本过低问题
    Windows下Nginx无法启动且进程里没有?
    Eclipse中复制项目后,怎么更改项目名等相关配置?(2019年10月17日)
    tomcat改端口号
    java基础
    数据库
    数组相关
    Linux系统实战
  • 原文地址:https://www.cnblogs.com/mapleins/p/10113343.html
Copyright © 2011-2022 走看看