参考资料:

MyBatis学习笔记(三)——parameterType为基本类型时的使用方法

MyBatis的传入参数parameterType类型

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,那么只能写成以下两种

 <select id="selectByAge2" resultType="stu">
select * from table1 order by ${value}
</select> <select id="selectByAge3" resultType="stu">
select * from table1 order by ${_parameter}
</select>

另外,当传入类型为基本类型,没有用${_parameter} 或 ${value}这样取值时,会报类似以下错误(根据传入类型的基本类型的不同有所不同)。

例如:传入类型为 string,取值写成  ${id}

 <select id="selectByAge4" resultType="stu">
select * from table1 order by ${id}
</select>

报错:

 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 文件内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.maven.mapper.StudentMapper"> <!-- 需要传参的sql语句 #{} 和 ${} 取值的区别--> <select id="selectByAge1" resultType="stu">
select * from table1 order by #{id}
</select> <select id="selectByAge2" resultType="stu">
select * from table1 order by ${value}
</select> <select id="selectByAge3" resultType="stu">
select * from table1 order by ${_parameter}
</select> <select id="selectByAge4" resultType="stu">
select * from table1 order by ${id}
</select> </mapper>

对应测试类中调用方法的代码:

 //<!-- 需要传参的sql语句  #{} 和 ${} 取值的区别-->
// #{id}
@Test
public void selectByAge1() {
List<Student> list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge1","id");
for(Student student:list){
System.out.println(student);
}
}
// ${value}
@Test
public void selectByAge2() {
List<Student> list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge2","id");
for(Student student:list){
System.out.println(student);
}
}
// ${_parameter}
@Test
public void selectByAge3() {
List<Student> list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge3","id");
for(Student student:list){
System.out.println(student);
}
}
// #{id}
@Test
public void selectByAge4() { List<Student> list = session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge4","id");
for(Student student:list){
System.out.println(student);
}
}
测试方法  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'

MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)的更多相关文章

  1. Mybatis映射文件中#取值时指定参数相关规则

    Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...

  2. 从cookie中取值$.cookie()

    从cookie中取值: var userid = $.cookie("remoteuserid");例子: function delUser() {     var table = ...

  3. layui从url中取值 ajax获取当前链接中的变量

    在使用layui(javascript)的时候,  需要从当前页面的url地址中取值, 例如: http://localhost:8081/html/fund-purchase.html?fundID ...

  4. struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值

    在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...

  5. 在properties.xml中定义变量,在application.xml中取值问题

    如果为application.xml中的变量赋默认值,同时又在properties.xml中变量赋值,而加载后是取不到properties.xml中的值的问题. 解决这个问题需要加上黑体部分配置: & ...

  6. java中从实体类中取值会忽略的的问题

    在我们java Map中通过get来取值时会忽略的问题是:如果取得一个空值null时,那么.toString()时就会出错,而且不知道是什么原因. 现在我给的具体方法是用条件表达式先判断一下. 例: ...

  7. Spring MVC-从零开始-@RequestMapping结合@PathVariable (从URL路径中取值,作用于函数参数)

    1.可以直接在RequestMapping中value元素中使用{key}描述属性键 2.也可以在{key}中使用正则限定key的取值范围,从而限定url的变化范围 package com.jt; i ...

  8. oracle定时器,调用存储过程,定时从n张表中取值新增到本地一张表中

    --创建新增本地数据库的存储过程create or replaceprocedure pro_electric_record as  begin    insert into electric_met ...

  9. java从c struct传来的字节数组中取值

    public int getInt(byte[] array,int index) { return (array[index]  & 0xff)  | (array[index + 1] & ...

随机推荐

  1. vs get set快捷键

    vs get set快捷键 光标放在空白处输入prop,然后tab两次,修改类型和名称即可

  2. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  3. idea 自动根据屏幕代码换行

    勾上即可 -- 建议不开启 有些时候从其他来源网站直接copy的代码文件会出现不勾选还会出现自动换行的情况 解决方法  新建文件  代码全部选剪过去

  4. [转] BootStrap table增加一列显示序号

    原文地址:https://blog.csdn.net/aboboo5200/article/details/78839208 最近由于项目需要,使用BootStrap table做数据展示,其中要在第 ...

  5. 【iCore4 双核心板_ARM】例程一:ARM驱动三色LED

    实验原理: 通过STM32的三个GPIO驱动一个三色LED,引脚PB2接红色LED(ARM_LEDR), 引脚PA9接蓝色LED(ARM_LEDB),引脚PA10接绿色LED(ARM_LEDG),   ...

  6. Java知多少(63)线程间通信

    上述例题无条件的阻塞了其他线程异步访问某个方法.Java对象中隐式管程的应用是很强大的,但是你可以通过进程间通信达到更微妙的境界.这在Java中是尤为简单的. 像前面所讨论过的,多线程通过把任务分成离 ...

  7. one-to-all及all-to-all网络通信模式

    在这两种模式下,因为 占用的通信通道非常高,形成了一个一对多的通道 甚至是多对多的通道,导致现有的fattree网络结构负载太大.

  8. Java8学习笔记(八)--方法引入的补充

    在Java8学习笔记(三)--方法引入中,简要总结了方法引入时的使用规则,但不够完善.这里补充下几种情况: 从形参到实例方法的实参 示例 public class Example { static L ...

  9. Android进阶:ListView性能优化异步加载图片 使滑动效果流畅

    ListView 是一种可以显示一系列项目并能进行滚动显示的 View,每一行的Item可能包含复杂的结构,可能会从网络上获取icon等的一些图标信息,就现在的网络速度要想保持ListView运行的很 ...

  10. 如何用Baas快速在腾讯云上开发小程序-系列3 :实现腾讯云COS API调用

    版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/640268001487425627 来源:腾云阁 h ...