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>
  • 相关阅读:
    Convert Date between LocalDateTime
    Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format
    Enable HTTPS in Spring Boot
    设定范围和步长的递增数验证器Validator
    Codeforces Round #392(div 2) 758D (贪心)
    机器学习:决策树算法(简单尝试)
    Codeforces Round #388 (Div. 2) 749E(巧妙的概率dp思想)
    Codeforces Round #364 (Div. 1) 700B(树)
    Codeforces Round #389 (Div. 2) 752F(树的权值重心)
    Codeforces Round #389 (Div. 2) 752E(二分答案)
  • 原文地址:https://www.cnblogs.com/shouyaya/p/12791309.html
Copyright © 2011-2022 走看看