参考资料:

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. Public key for ambari-server-2.4.2.0-136.x86_64.rpm is not installed 安装ambari报错总结

    提示;# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release 就是导入这个安装包的key 可以使用http的协议 比如我用的就是 rpm ...

  2. 最详尽的 JS 原型与原型链终极详解,没有「可能是」。(一)

    最详尽的 JS 原型与原型链终极详解,没有「可能是」.(一) 第二篇已更新,点击进入第三篇已更新,点击进入

  3. CentOS 添加 Oracle YUM 源

    最新文章:Virson's Blog 文章来自:Oracle 官方 YUM 源 Introduction The Oracle public yum server offers a free and ...

  4. Go语言_range(范围)理解

    一.Go语言中的range Go 语言中 range 关键字用于 for循环中迭代数组(array).切片(slice).链表(channel)或集合(map)的元素: 在数组和切片中它返回元素的索引 ...

  5. 什么是对象:EVERYTHING IS OBJECT(万物皆对象)

      所有的事物都有两个方面: 有什么(属性):用来描述对象. 能够做什么(方法):告诉外界对象有那些功能. 后者以前者为基础. 大的对象的属性也可以是一个对象.

  6. Spring源码学习:day2

    前言: 我还是太懒了,连截图都懒得粘贴,故直接用书上说的话的截图吧. 代码的编写过程都是应该有一个入口的,所有的代码最终都是为了那个入口更加方便更加简单而产生的. 看代码的过程,就应该抓住主线,顺着主 ...

  7. Angular4学习笔记(一)-环境搭建

    下载nodejs 下载地址 在命令行输入:npm -v 如果出现如下画面即安装成功 安装Angular的cli命令行工具 命令:sudo npm install -g @angular/cli 输入n ...

  8. Tensorflow 使用slim框架下的分类模型进行分类

    Tensorflow的slim框架可以写出像keras一样简单的代码来实现网络结构(虽然现在keras也已经集成在tf.contrib中了),而且models/slim提供了类似之前说过的object ...

  9. [IR] Arithmetic Coding

    Statistical methods的除了huffman外的另一种常见压缩方式. Huffman coding的非连续数值特性成为了无法达到香农极限的先天无法弥补的缺陷,但Arithmetic co ...

  10. [AWS] User management

    IAM用户管理 Ref: AWS系列-创建 IAM 用户 Ref: AWS系列:深入了解IAM和访问控制 是什么? IAM enables you to control who can do what ...