zoukankan      html  css  js  c++  java
  • MyBatis入参方式

    1.传递单参数的形式

      MyBatis会自动进行参数的赋值

      如:

    public  void  deletePerson(Integer id);  //接口
    <!-- mapper配置文件-->
      <delete id="deletePerson" parameterType="int">
    
          delete from person where id=#{parameter1} <!-- #{}里面写任何值都能取到值-->
        
      </delete>

    2.传递多个参数(mapper不会自动赋值)

      a.使用Map接口(不推荐)

    public Person getPersonByNameAndGender(Map<String,Object> param);//接口
      <select id="getPersonByNameAndGender" resultType="person">
    
        select * from person where username=#{username} and gender=#{gender}
        
        <!--#{}里面填写Map的key值-->
      </select>

      b.使用javaBean(封装pojo类)传递参数(传递的参数多于5)

    //将要传递的参数封装在一个对象中
    public Person getPersonByNameAndGender(Person person);
      <select id="getPersonByNameAndGender" resultType="person">
    
        select * from person where username=#{username} and gender=#{gender}
    
        <!--#{}里填写person对象里面的属性-->
      </select>

      c.使用注解@param传递参数(传递的参数少于5)

    public Person getPersonByNameAndGender(@Param("username") String username, @Param("gender") String gender);
      <select id="getPersonByNameAndGender" resultType="person">
    
        select * from person where username=#{username} and gender=#{gender}
    
      </select>

    3.传递的参数是个collection、list、array

      使用注解@param

     public Person getPersonByCollection(@Param("test") int[] ids);
    <select id="getPersonByCollection" resultType="person">
    
        select * from person where id=#{test[0]}
      </select>

      可使用foreach标签读取参数

    public List<Person> getPersonsByIds(@Param("test")int[] ids);
      <select id="getPersonsByIds" resultType="person">
    
        select * from person where id in
    
        <foreach collection="test" item="id" index="i" open="(" close=")" separator=",">
          #{id}
        </foreach>
    
    <!--collection表示选取的集合,item表示当前遍历的对象,index表示当前遍历的索引-->
    <!--若test这个数组里面为test[]{1,2,3},则上面的sql语句相当于select * from person id in (1,2,3,4)-->
      </select>
  • 相关阅读:
    Updatepanel 中使用 Timer 控件 失去焦点问题
    获取周一还是周日作为首日
    存储过程
    ASP.NEt ajax 弹出窗口在页面无法关闭
    Gridview 每秒刷新数据
    SharePoint 2013 set site mailbox
    SharePoint 2010 将带有工作流的模板移动到另一个站点集
    Infoapth 使用拼写 并加载web part 在Infopath的页面上
    ECMA 上传文件到SHarePoint 文档库
    数据库如何高效率处理百万以上的查询
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12791309.html
Copyright © 2011-2022 走看看