@param

param

1、关于param

@Param是MyBatis所提供的(org.apache.ibatis.annotations.Param),作为Dao层的注解,作用是用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳。

2、作用

  1. 多个参数,写

  2. 给参数起个别名,写

  3. XML 中的 sql 语句使用了 $ 符号,写

  4. 动态 sql 语句中有参数作为变量,该参数就要写

3、例子

查找数据库lei_xing表中id为1,leixing为动画的数据

实体类:

package com.jcl.pojo;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LeiXing {
    private Integer id;//int 不可以表示null,而包装类可以
    private String leiXing;

}

接口中:

//使用@param
   void selectByIdAndLeixing(@Param("idddddd")int id,@Param("leixinggggg")String leiXing);

Mapper.xml中:

 <select id="selectByIdAndLeixing" resultType="com.jcl.pojo.LeiXing"  >
        select * from lei_xing where id = #{idddddd} and leixing = #{leixinggggg}
 </select>

单元测试:

 @Test
    public void Test4(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        LeiXingMapper leiXingMapper = sqlSession.getMapper(LeiXingMapper.class);

        leiXingMapper.selectByIdAndLeixing(1,"动画");
        sqlSession.commit();
        sqlSession.close();
    }

@param在启动时生成一个idddddd的属性,把参数id的值赋给idddddd,然后在Mapper.xml中使用#{idddddd}获取变量的值

ps:也可以在@param写实体类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值