8、mybatis学习——sqlmapper配置文件参数处理(单个参数,多个参数,命名参数)

拿查询举例

  1、单个参数时

  

 此时sqlMapper中的配置

  

 或者

  

 都可以;因为参数只有一个,不会混乱,单个参数可红框中的取名可随意

  2、多个参数时

     mapper接口中的方法:

  

     sqlmapper中的配置:

    <!-- 多个参数时,mybatis会做特殊处理;多个参数会被封装成一个map
      key:param1,param2,.......paramN
      value:传入的参数值
      #{ }就是从map中获取指定key的值 -->
    <select id="selectEmpByIdAndName" resultType="employee">
        select * from employee where id = #{param1} and name = #{param2}
    </select>

  

  3、多个参数时(命名参数方式)

    mapper接口中的方法:利用@Param配置进行命名参数

     sqlmapper中的配置:

    <!-- 多个参数命名方式 -->
    <select id="selectEmpByIdAndName" resultType="employee">
        select * from employee where id = #{id} and name = #{name}
    </select>

猜你喜欢

转载自www.cnblogs.com/lyh233/p/12342472.html