@Param(“自定义参数名”) 主要解决: 多个参数,且多个参数数据类型不同的问题。
当 Dao 接口方法多个参数,需要通过名称使用参数。在方法形参前面加入@Param(“自定义参数名”),mapper 文件使用#{自定义参数名}。
例如定义:
List selectStudent( @Param(“personName”) String name ) { … }
mapper 文件
select * from student where name = #{personName}
接口方法:**
public abstract Student selectWhere(@Param("id") Integer id, @Param("name") String name);

<select id="selectWhere" resultType="com.wzy.pojo.Student">select id,name,gender,age,birthday,files from student where id = #{id} or name =#{name}</select>
测试方法:
package com.wzy.dao;
import com.wzy.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.*;
public class StudentDaoTest3 {
@Test
public void selectWhere() {
SqlSession sqlSession = null;
try {
//1.mybatis 主配置文件
String config = "MyBatis.xml";
//2.读取主配置文件
InputStream resourceAsStream = Resources.getResourceAsStream(config);
//3.创建 SqlSessionFactory 对象,用于获取 sqlSession sql语句持久化对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//4.获取 sqlSession 语句持久化对象
sqlSession = sqlSessionFactory.openSession();
//5.动态代理,获取接口对象
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
//6.出入参数,调用查询方法.查询数据
Student student = studentDao.selectWhere(1, "李四");
//7.输出结果
System.out.println(student.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//8.释放资源
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
