参考资料:
1. MyBatis的传入参数parameterType类型分两种
1.1 基本数据类型:int,string,long,Date;
1.2 复杂数据类型:类和Map
2. 如何获取参数值:
2.1 基本数据类型:#{随意起个名字} 或 ${_parameter} 或 ${value} 注意这里的区别
2.2 复杂数据类型:#{属性名} 或 #{属性名} ,map中则是#{key} 或 ${key}
特殊情况:
order by 后面只能用 ${}取值。
例如,传入类型为 string ,并且order by,那么只能写成以下两种
1 4 5
另外,当传入类型为基本类型,没有用${_parameter} 或 ${value}这样取值时,会报类似以下错误(根据传入类型的基本类型的不同有所不同)。
例如:传入类型为 string,取值写成 ${id}
1
报错:
1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'
传入类型为 string ,并且order by的实例代码:
mapper.xml 文件内容:
对应测试类中调用方法的代码:
1 // 2 // #{id} 3 @Test 4 public void selectByAge1() { 5 Listlist = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge1","id"); 6 for(Student student:list){ 7 System.out.println(student); 8 } 9 }10 // ${value} 11 @Test12 public void selectByAge2() {13 List list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge2","id");14 for(Student student:list){15 System.out.println(student);16 }17 }18 // ${_parameter} 19 @Test20 public void selectByAge3() {21 List list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge3","id");22 for(Student student:list){23 System.out.println(student);24 }25 }26 // #{id} 27 @Test28 public void selectByAge4() {29 30 List list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge4","id");31 for(Student student:list){32 System.out.println(student);33 }34 }
测试方法 selectByAge1 返回查询结果,但是order by不会起作用
测试方法 selectByAge2 返回查询结果,order by会起作用
测试方法 selectByAge3 返回查询结果,order by会起作用
测试方法 selectByAge4 报错
1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'