参考资料:

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. TCP/IP协议理解

    TCP/IP协议理解 一.    协议分层 由于ISO标准中的网络模型是个7层模型,但是由于7层模型对于当时的网络厂商来说太过复杂,很多厂家采用了简化的4层模型来实现网络设备,4层模型也就成了事实的网 ...

  2. 教你一招:win10下JDK的安装与环境变量配置

    1.到官网下载最新版本的JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html 2.安装JDK,同安装其他软件一样 ...

  3. MQTT 学习记录

    学习mqtt协议,从网上找demo验证一下. 参考链接 https://www.jianshu.com/p/ebbe25d1c4b2 https://blog.csdn.net/xxmonstor/a ...

  4. json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

    前台传到后台的json字符串 前台实现这种格式json字符串方式: function contentFun(){ respType = respTypeFun(); return "{\&q ...

  5. SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器

    SpringBoot切换Tomcat容器, SpringBoot修改为Jetty容器, SpringBoot使用undertow容器, SpringBoot使用Jetty容器 ============ ...

  6. css3整理--box-sizing

    box-sizing语法: box-sizing : content-box || border-box || inherit 参数取值: content-box:此值为其默认值,其让元素维持W3C的 ...

  7. POJ 2018 Best Cow Fences(二分+最大连续子段和)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...

  8. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二:按键模块① - 消抖

    实验二:按键模块① - 消抖 按键消抖实验可谓是经典中的经典,按键消抖实验虽曾在<建模篇>出现过,而且还惹来一堆麻烦.事实上,笔者这是在刁难各位同学,好让对方的惯性思维短路一下,但是惨遭口 ...

  9. 【Java并发编程六】线程池

    一.概述 在执行并发任务时,我们可以把任务传递给一个线程池,来替代为每个并发执行的任务都启动一个新的线程,只要池里有空闲的线程,任务就会分配一个线程执行.在线程池的内部,任务被插入一个阻塞队列(Blo ...

  10. 关于SMI、MSI、SCI、INTx各种中断小结【转】

    转载自http://blog.csdn.net/huangkangying/article/details/11178425 目录(?)[-] MSI VS INTxPin-based interru ...