在使用mapper接口开发的时候,因为Mapper接口中的方法参数只能有一个,当我们需要传多个值时候会遇到问题,该怎么办?
有三种解决办法:
第一种:封装成javabean
第二种:封装到Map
第三种:使用@Param注解
例子:员工类里有多个属性,想要传递多个属性值
public class Employee {
//属性名
private String name ;
private String password ;
//属性的get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
需要封装到一个类中,例如封装到EmployeeVo类中
public class EmployeeVo {
private Employee employee ;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
在service层把EmployeeVo当做参数传递
public class EmployeeServiceImpl implements EmployeeService {
public Employee login(String name, String password) throws Exception {
//Mybatis主配置文件名
//加载主配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//建立连接
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
//封装Employee对象
Employee employee = new Employee();
employee.setName(name);
employee.setPassword(password);
//创建值对象 将封装好的Employee传入值对象
EmployeeVo employeeVo = new EmployeeVo();
employeeVo.setEmployee(employee);
//调用Mapper接口中的方法并返回返回值
return employeeMapper.login(employeeVo);
}
}
Mapper接口中参数为值对象
public interface EmployeeMapper {
public Employee login(EmployeeVo employeeVo) throws Exception ;
}
在映射文件中
<mapper namespace="需要填写Mapper接口的全限定名">
<select id="login" resultType="Employee的全限定名" parameterType="EmployeeVo类的全限定名">
SELECT name,
password
FROM employee
WHERE name = #{
employee.name} AND password = #{
employee.password}
</select>
</mapper>
在service层需要把参数放在一个Map集合中,然后传递Map集合
public Employee login2(String name , String password) throws Exception
{
//Mybatis主配置文件名
//加载主配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//建立连接
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
//创建一个Map集合,把属性放在Map集合中
Map<String,String> map = new HashMap<String,String>();
map.put("name",name);
map.put("password" , password);
return employeeMapper.login2(map);
}
Mapper接口中参数为Map集合
public interface EmployeeMapper {
public Employee login2(Map map) throws Exception ;
}
在映射文件中
<mapper namespace="需要填写Mapper接口的全限定名">
<select id="login2" resultType="Employee的全限定名">
SELECT name,
password
FROM t_employee
WHERE name = #{
name} AND password = #{
password}
</select>
</mapper>
在service层直接传递多个参数
public class EmployeeServiceImpl implements EmployeeService {
public Employee login(String name, String password) throws Exception {
//Mybatis主配置文件名
//加载主配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//建立连接
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
return employeeMapper.login3(name , age);
}
}
Mapper接口中使用注解方式传递参数
public interface EmployeeMapper {
public Employee login3(@Param("name") String name, @Param("password") String password) throws Exception ;
}
在映射文件中
<mapper namespace="需要填写Mapper接口的全限定名">
<select id="login3" resultType="Employee的全限定名">
SELECT name,
password
FROM employee
WHERE name = #{
name} AND age = ${
password}
</select>
</mapper>
三种方法中@Param注解方式是最提倡的,第一种太繁琐,第二种使用的人很多,但是Map集合中的键值不同人之间理解有障碍.