🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
传递两个,或两个以上个数的参数可以将 DAO 层中的接口方法的参数列表声明为 Map、@Param、实体类之一来达到传递参数的目的。 [TOC] # 1. 使用`java.util.Map`声明参数列表 **1. DAO层接口** ```java public interface StudentDao { /** * 查询性别相同,并且出生日期相同的所有学生 */ List<Student> queryByBornAndGender(Map<String, Object> params); } ``` **2. XML映射文件** ```xml <!-- #{born}、#{gender} 中的 born、gender 为 Map 中的 key --> <select id="queryByBornAndGender" parameterType="Map" resultType="Student"> select id, `name`, born, gender from student where born=#{born} and gender=#{gender} </select> ``` **3. 测试** ```java @Test public void queryByBornAndGender() { try { Map<String, Object> params = new HashMap<>(1); params.put("gender", "男"); params.put("born", new Date()); sqlSession = MybatisUtils.createConnetion(); List<Student> students = sqlSession.getMapper(StudentDao.class).queryByBornAndGender(params); System.out.println(students); } catch (Exception e) { e.printStackTrace(); } finally { MybatisUtils.close(sqlSession); } } ``` <br/> # 2. 使用注解`@Param`声明参数列表 **1. DAO层接口** ```java public interface StudentDao { /** * 查询性别相同,并且出生日期相同的所有学生 */ List<Student> queryByBornAndGender2(@Param("gender") String gender, @Param("born") Date born); } ``` **2. XML映射文件** ```xml <select id="queryByBornAndGender2" resultType="Student"> select id, `name`, born, gender from student where born=#{born} and gender=#{gender} </select> ``` **3. 测试** ```java @Test public void queryByBornAndGender2() { try { sqlSession = MybatisUtils.createConnetion(); List<Student> students = sqlSession.getMapper(StudentDao.class).queryByBornAndGender2("张三", new Date()); System.out.println(students); } catch (Exception e) { e.printStackTrace(); } finally { MybatisUtils.close(sqlSession); } } ``` <br/> # 3. 使用实体类声明参数列表 **1. DAO层接口** ```java public interface StudentDao { /** * 查询性别相同,并且出生日期相同的所有学生 */ List<Student> queryByBornAndGender3(Student student); } ``` **2. XML映射文件** ```xml <!-- #{keyName}: keyName就是Student类的相关属性名 --> <select id="queryByBornAndGender3" parameterType="student" resultType="student"> select id, `name`, born, gender from student where born=#{born} and gender=#{gender} </select> ``` **3. 测试** ```java @Test public void queryByBornAndGender3() { try { Student student = new Student(); student.setName("张三"); student.setBorn(new Date()); sqlSession = MybatisUtils.createConnetion(); List<Student> students = sqlSession.getMapper(StudentDao.class).queryByBornAndGender3(student); System.out.println(students); } catch (Exception e) { e.printStackTrace(); } finally { MybatisUtils.close(sqlSession); } } ``` <br/> # 4. 实体类与注解`@Param`配合使用 **1. DAO层接口** ```java public interface StudentDao { /** * 查询性别相同,并且出生日期相同的所有学生 */ List<Student> queryByBornAndGender4(@Param("stu") Student student); } ``` **2. XML映射文件** ```xml <!-- stu.born 中的 stu 为 @Param 中的 stu,born为实体类 Student 中的属性名 --> <select id="queryByBornAndGender4" parameterType="student" resultType="student"> select id, `name`, born, gender from student where born=#{stu.born} and gender=#{stu.gender} </select> ``` **3. 测试** ```java @Test public void queryByBornAndGender4() { try { Student student = new Student(); student.setName("张三"); student.setBorn(new Date()); sqlSession = MybatisUtils.createConnetion(); List<Student> students = sqlSession.getMapper(StudentDao.class).queryByBornAndGender4(student); System.out.println(students); } catch (Exception e) { e.printStackTrace(); } finally { MybatisUtils.close(sqlSession); } } ``` **** 参考文档:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#Parameters