param
1、关于param
@Param是MyBatis所提供的(org.apache.ibatis.annotations.Param),作为Dao层的注解,作用是用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳。
2、作用
-
多个参数,写
-
给参数起个别名,写
-
XML 中的 sql 语句使用了 $ 符号,写
-
动态 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写实体类