mybatis参数传递方式

单个参数

mapper接口

User selectById(Integer id);

映射文件

<select id="selectById" resultType="com.example.demo.domain.User" parameterType="int">
    select  username,display_name  from user where id= #{id}
</select>

多个参数

  1. map方式传参

mapper接口

User selectByMap(Map map);

映射文件
sql中的参数名对应与map中的key

 <select id="selectByMap" resultType="com.example.demo.domain.User">
        select * from user where username=#{username} and display_name=#{displayName}
 </select>
 
  1. mybatis默认参数方式

mapper接口

User selectByUserNameAndDisplayName(String username,String displayName);

映射文件
param1 param2 param3 …以此类推 对应与mapper中的参数列表

 <select id="selectByUserNameAndDisplayName" resultType="com.example.demo.domain.User">
     select * from user where username=#{param1} and display_name=#{param2}
 </select>

3.@param注解方式

mapper接口

 User selectByParam(@Param("name") String name,@Param("age") int age);

映射文件

<select id="selectByParam" resultType="com.example.demo.domain.User">
    select * from user where name=#{name} and age =#{age}
</select>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值