参考资料:

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. vue模板的讲解

    1.项目目录 2.入口文件index.html 3.index.html默认调用的main.js 3.调用的组件app.vue 4.调用组件hello.vue

  2. layui表单验证

    layui表单元素的校验只需在元素上加入lay-verify,layui提供了以下值. required(必填项) phone(手机号) email(邮箱) url(网址) number(数字) da ...

  3. VS2013 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    全是2012版本的  没找到2013的! 控制面板>程序>程序和功能 找到如下选中软件右击修复 即可 需关闭VS2013 参考:https://blog.csdn.net/zhaoyun9 ...

  4. 【转】Ubuntu FireFox无法播放网页视频音乐的解决办法

    原文:http://www.codeweblog.com/%E8%A7%A3%E5%86%B3qq%E9%9F%B3%E4%B9%90%E7%BD%91%E9%A1%B5%E7%89%88%E5%9C ...

  5. 【MongoDB】MongoDb的“not master and slaveok=false”错误及解决方法 mongo连接从库出现问题

    链接mongodb报错如下 2016-03-14T16:26:00.912+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok& ...

  6. javascript form提交 不执行onsubmit事件解决方案

    转载自:https://www.cnblogs.com/lorgine/archive/2011/03/30/2000284.html 今天做项目过程中,需要用到javascript提交form到后台 ...

  7. Java知多少(2)虚拟机(JVM)以及跨平台原理

    相信大家已经了解到Java具有跨平台的特性,可以“一次编译,到处运行”,在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的. 那么,跨平台是怎样实现的呢?这就 ...

  8. rqalpha探究 2 接入mod

    程序的目的是尽可能用mod扩展功能,所以接下来需要接入mod模块

  9. EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法

    结论: ViewData 适用于 在一次请求中 传递数据  . 比如我们从 主Action 到 主视图, 然后在 主视图中  用 RenderAction 请求子Action的时候,就是算作 一次请求 ...

  10. iOS开发-- 使用TFHpple解析html

    占坑 http://www.raywenderlich.com/14172/how-to-parse-html-on-ios