Three ways of passing parameters in mybatis

Reprinted from https://blog.csdn.net/auly2017/article/details/73205831

The main work of the mybatis framework is the data layer, which focuses on the writing of SQL statements that deal with databases, and requires more proficiency in SQL.


There are three main ways for mybatis to pass parameters:


1. How to pass multiple parameters

   In the form of: 

   GoodMapper.java:

      public Good selectGood(String id, String name);

   GoodMapper.xml :

     <select id="selectGood" resultMap="GoodMap">

           select * from good where id = #{0} and name=#{1}

     </select>


  Note: #{0} represents the first parameter, #{1} represents the second parameter, and so on


 

2. The way of passing fixed parameters

    In the form of:

     GoodMapper.java:

       public Good selectGood(@param("id")String id,@param("name")String name);

    GoodMapper.xml :

     <select id="selectGood" resultMap="GoodMap">

          select * from good where id = #{id} and name=#{name}

     </select>

   

     Note: This method is more intuitive for parameters


3. Transfer in the form of map

   In the form of:

    GoodMapper.java:

        public Good selectGood(Map map);

   GoodMapper.xml :

    <select id="selectGood" resultMap="GoodMap">

             select * from good where id = #{id} and name=#{name}

     </select>

 GoodService.java :

  public Good selectGood(Map map);

 GoodServiceImpl.java :

  public Good selectGood(){

     Map map = new HashMap();

     map.put("id",1);

     map.put("name",zhangsan);

     Good good = goodService.selectGood(map);

     return good;

   }

  

        注:此种方式以map的形式来传入需要的参数,当参数较多时,使用此种方式比较方便。

                在mybatis相关的实际项目开发中使用此种方式比较多。建议使用此种方式。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326131246&siteId=291194637