zoukankan      html  css  js  c++  java
  • 关于Mybatis参数传值问题(常用) 个人比较推荐第二种哦,可以减少代码量,唯一要注意的是自己传递的参数个数个顺序就好

     第一种方式:注解也是开发中最常见的一种方式(明显看出自己所传递的参数,)

      DAO类中的方法函数

      Public User selectUser(@param("userName")Stringname,@param("userArea")String area); 

      对应的Mapper.xml语句

      <select id=" selectUser" resultMap="BaseResultMap"> 
        select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} 
      </select>

      也可以是这样

      <select id=" selectUser" resultMap="BaseResultMap"> 
        select * from user_user_t where user_name = #{userName} and user_area=#{userArea} 
      </select>

      根据你所拥有的Mybatis版本而去 老版本需要添加参数类型

     第二种方式:多个参数传值(索引方式)(简便,减少代码量)

       DAO类中的方法函数

      public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

      对应Mapper.xml语句      <select id="getXXXBeanList" resultType="XXBean">     select t.* from tableName where id = #{0} and name = #{1}   </select>
      由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始


    第三种方式:Map封装多参数
       DAO类中的方法函数 
       public List<XXXBean> getXXXBeanList(HashMap map);
       对应Mapper.xml语句   
    <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">     
         select 字段... from XXX where id=#{xxId} code = #{xxCode}    
       </select>   

    其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。


    第四种方式:List封装in
          DAO类中的方法函数
      public List<XXXBean> getXXXBeanList(List<String> list);  
         对应的Mapper.xml文件
      <select id="getXXXBeanList" resultType="XXBean">     select 字段... from XXX where id in     <foreach item="item" index="index" collection="list" open="(" separator="," close=")">       #{item}     </foreach>   </select>   foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')





  • 相关阅读:
    学生管理系统报错(一)
    POJ3264 Balanced Lineup
    MySQL主从复制和读写分离
    身边的同事辞职去旅行
    怎样查看eclipse是32位还是64位
    Mule ESB-3.Build a webservice proxy
    《Head First 设计模式》学习笔记——复合模式
    DecimalFormat格式化输出带小数的数字类型
    黑马day01 笔记
    [Swift]LeetCode835. 图像重叠 | Image Overlap
  • 原文地址:https://www.cnblogs.com/mingqi/p/6495524.html
Copyright © 2011-2022 走看看