mybatis传递参数

传递一个参数

Dao 接口中方法的参数只有一个简单类型(java 基本类型和 String),占位符 #{ 任意字符 },和方
法的参数名无关。

接口方法

Student selectStuId(int id)

mapper文件

<select id="selectStuId" resultType="com.hgzy.domian.Student">
	select id,name,emial,age from student where id=#{
   studentId}
	//#{studentId} , studentId 是自定义的变量名称,和方法参数名无关。
</select>

测试方法

@Test
public void testSelectById(){
   
 //一个参数 
 Student student = studentDao.selectById(1005);
 System.out.println("查询 id 是 1005 的学生:"+student);
}
传入多个参数 使用@param

当 Dao 接口方法多个参数,需要通过名称使用参数。在方法形参前面加入@Param(“自定义参数名”),
mapper 文件使用#{自定义参数名}。
例如定义 List selectStudent( @Param(“personName”) String name ) { … }
mapper 文件 select * from student where name = #{ personName}


接口方法

List<Student> selectMultiParam(@Param("personName") String name,
							   @Param("personAge") int age);

mapper文件

<select id="selectMultiParam" resultType="com.bjpowernode.domain.Student">
 select id,name,email,age from student where name=#{
   personName} or age 
=#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值