美文网首页
接口绑定及多参数传递

接口绑定及多参数传递

作者: Yanl__ | 来源:发表于2019-12-26 15:59 被阅读0次

1.接口绑定
2.多参数传递

1.接口绑定

作用:创建一个接口后,由mapper.xml把mybatis生成的接口的实现,通过调用接口对象就可以获取mapper.xml中编写的sql

实现步骤:
1.创建接口
2.编写mapper.xml
3.调用接口来获取mapper.xml的sql

实现细节:

  1. 对mybatis的配置文件mybatis.xml进行配置
<mappers>
    <package name="com.steer.mapper" />
</mappers>
  1. 新建接口
    根据配置文件所配置的包位置,(上面配置的是com.steer.mapper)在该包下新建接口
public interface LogMapper{
    List<Log> selAll();
}
  1. 新建对应的映射文件
    在配置文件对配置的包下,新建对应的映射文件LogMapper.xml
<mapper namespace="com.steer.mapper.LogMapper">
    <select id="selAll" resultType="log">
        select * from log
    </select>
</mapper>
  1. 调用

2.多参数传递

当多参数时,不需要写parameterType

  1. 按位置
    接口中有两个参数,xml中直接按位置接收#{0} #{1} 或者 #{param1} #{param2}
List<Log> selByAccInAccout(String accin,String accout);

<select id="selByAccInAccout" resultType="log" >
    select * from log where accin=#{0} and accout=#{1}
</select>
  1. 使用注解
    在接口声明方法中参数名称随便起,与xml文件对应的参数名是注解中的名称
    #{} 里面写@Param(“内容”)参数中内容
List<Log> selByAccInAccout(@Param("accin") String accin666,@Param("accout") String accout666);

<select id="selByAccInAccout" resultType="log" >
    select * from log where accin=#{accin} and accout=#{accout}
</select>

相关文章

网友评论

      本文标题:接口绑定及多参数传递

      本文链接:https://www.haomeiwen.com/subject/hffloctx.html