参考资料:

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. DIOCP-V5发布

    DIOCP-v5开源项目 https://github.com/ymofen/diocp-v5   DIOCP-v5 0.0.1   2015-02-22 08:40:40 1.规范单元文件的命名,和 ...

  2. 《Essential C++》读书笔记 之 泛型编程风格

    <Essential C++>读书笔记 之 泛型编程风格 2014-07-07 3.1 指针的算术运算(The Arithmetic of Pointer) 新需求1 新需求2 新需求3 ...

  3. 15款基于 jQuery模态对话框

    在数字世界的竞争已大大增加.这就是为什么要确保网络设计的各个方面都是一流的,这是很重要的.从布局到一些非常小的东西,比如对话框,每一件都需要设计得很好.对话框通常被忽视,但它们可能对访问者有很大的影响 ...

  4. js绝对地址图片转换成base64的方法

    //将图片转换成base64 function getBase64Image(url, callback){ var canvas = document.createElement('canvas') ...

  5. Python3解《剑指》问题:“遇到奇数移至最前,遇到偶数移至最后”

    [本文出自天外归云的博客园] 看到一个<剑指Offer>上的问题:“遇到奇数移至最前,遇到偶数移至最后.” 我做了两种解法.一种是利用python内置函数,移动过程用了插入法,很简单.另一 ...

  6. 灯箱效果插件Magnific Popup详解

    Magnific Popup 是一个非常优秀的弹出对话框或者灯箱效果插件.它基于jQuery(zepto)开发,使用非常简单,特点就是:非常好用. 官网地址: http://dimsemenov.co ...

  7. 【iCore1S 双核心板_FPGA】例程九:锁相环实验——锁相环的使用

    实验现象: 利用Quartus内部组件生成锁相环,用SignalTap II进行校验. 核心代码: //--------------------Module_PLL------------------ ...

  8. 【GMT43智能液晶模块】例程八:ADC实验——电源监控

    实验原理: STM32内部集成三个12位ADC,GMT43的所有电源经过电阻分压接 入到ADC的输入通道内,输入电流经过高端电流检测芯片ZXCT1009F输入 到ADC的输入通道内,从而实现电源监控功 ...

  9. [微信开发] 微信JSAPI - 获取用户地理位置信息

    参考博客 http://blog.csdn.net/u013142781/article/details/50503299 主要JS 方法 wx.getLocation 获取地理位置信息传递参数 成功 ...

  10. JVM学习(4)——全面总结Java的GC算法和回收机制---转载自http://www.cnblogs.com/kubixuesheng/p/5208647.html

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 一些JVM的跟踪参数的设置 Java堆的分配参数 -Xmx 和 –Xms 应该保持一个什么关系,可以让系统的 ...