一、在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型

  • 基本数据类型:包含int,String,Date等。通过#{参数名},只能传入一个参数;通过#{0}、#{1}……索引方式,可以传入多个参数;如果通过#{参数名}传多个值,又不想使用索引方式,可以使用@param()注解。
  • 复杂数据类型:包含JAVA实体类、Map。通过#{属性名}或#{map的KeyName}即可获取传入的值

1、#{参数名},传入一个参数

DAO方法:

public List<User> selectUserByOrgId(String orgId);

Mapper.xml:

<select id="selectUserByOrgId" parameterType="java.lang.String" resultType="user">
select * from user where org_id = #{orgId}
</select>

service:

List<User> users = userDao.selectUserByOrgId("1");

但是与if标签结合使用则要注意必须用map或者对象

参照:http://www.cnblogs.com/keyi/p/8534905.html

2、#{0}、#{1}……索引方式,传入多个参数

DAO方法:

public User selectUserByNameAndAge(String name,int age);

Mapper.xml:

<select id="selectUserByNameAndAge" resultType="user">
select * from user where name = #{0} and age = #{1}
</select>

service:

User user = userDao.selectUserByNameAndAge("lucy",18);

3、#{参数名},传入多个参数,并且参数用@param注解

DAO方法:

public User selectUserByNameAndAge(@param("name")String name,@param("age")int age);

Mapper.xml:

<select id="selectUserByNameAndAge" resultType="user">
select * from user where name = #{name} and age = #{age}
</select>

service:

User user = userDao.selectUserByNameAndAge("lucy",18);

4、传入多个基本类型参数,参数用map封装,通过#{mapKey}取值

DAO方法:

public User selectUserByNameAndAge(Map map);

Mapper.xml:

<select id="selectUserByNameAndAge" parameterType="Map" resultType="user">
select * from user where name = #{name} and age = #{age}
</select>

service:

Map<String,Object> map=new HasMap<String,Object>();
map.put("name","lucy");
map.put("age",18);
User user = userDao.selectUserByNameAndAge(map);

5、使用map封装实体类,通过通过#{mapKey.attributeName}取值

DAO方法:

public User selectUserByNameAndAge(Map map);

Mapper.xml:

<select id="selectUserByNameAndAge" parameterType="Map" resultType="user">
select * from user where name = #{userParam.name} and age = #{userParam.age}
</select>

service:

User userParam = new User("lucy",18);

Map<String,Object> map=new HasMap<String,Object>();
map.put("user",userParam);
User user = userDao.selectUserByNameAndAge(map);

6、直接传入实体参数,通过#{属性名}取值

DAO方法:

public User selectUserByNameAndAge(User userParam);

Mapper.xml:

<select id="selectUserByNameAndAge" parameterType="User" resultType="user">
select * from user where name = #{userParam.name} and age = #{userParam.age}
</select>

service:

User userParam = new User("lucy",18);
User user = userDao.selectUserByNameAndAge(userParam);

二、#{}与${}的区别

#{}拿到值之后,拼装sql,会自动对值添加单引号” 
${}则把拿到的值直接拼装进sql,如果需要加单引号”,必须手动添加,一般用于动态传入表名或字段名使用,同时需要添加属性statementType=”STATEMENT”,使用非预编译模式。

注:statementType:STATEMENT(非预编译),PREPARED(预编译)或CALLABLE中的任意一个,这就告诉 MyBatis 分别使用Statement,PreparedStatement或者CallableStatement。默认:PREPARED。

使用${}传参取值实例: 
DAO方法:

public List<User> selectUserByOrgId(String orgId);

Mapper.xml:

<select id="selectUserByOrgId" parameterType="java.lang.String" resultType="user" statementType="STATEMENT">
select * from user where org_id = ${orgId}
</select>

service:

String orgId = "100";
orgId = "'" + orgId + "'";
List<User> users = userDao.selectUserByOrgId(orgId);

Mybatis:传入参数方式以及#{}与${}的区别的更多相关文章

  1. Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别

    一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...

  2. MyBatis传入参数为list、数组、map写法(转载)

    MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...

  3. MyBatis传入参数为list、数组、map写法

    1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...

  4. mybatis传入参数类型parameterType和输出结果类型resultType详解

    前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...

  5. mybatis传入参数类型parameterType详解

    前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...

  6. MyBatis传入参数为集合、数组SQL写法

    参考:http://blog.csdn.net/small____fish/article/details/8029030 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合 ...

  7. MyBatis传入参数为集合 list 数组 map写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  8. MyBatis 传入参数之parameterType

      在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...

  9. MyBatis传入参数与parameterType

    参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...

随机推荐

  1. Codeforces 982C(dfs+思维)

    C. Cut 'em all! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. jredis 客户端 使用

    redis学习及实践3---Jedis.JedisPool.Jedis分布式实例介绍 Java中使用Jedis操作Redis Redis客户端:Jedis

  3. Python模块包(pycharm右键创建文件夹和python package的区别)中__init__.py文件的作用

    在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很 ...

  4. linux下echo命令详解

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 例如: echo $JAVA_HOME /export/se ...

  5. 十七.jQuery源码解析之入口方法Sizzle(1)

    函数Sizzle(selector,context,results,seed)用于查找与选择器表达式selector匹配的元素集合.该函数是选择器引擎的入口. 函数Sizzle执行的6个关键步骤如下: ...

  6. jaxp使用笔记

    XML文件的解析技术有DOM和SAX方式,在Android中还有pull解析方式,这里不再讨论 DOM解析的方式和js中的DOM操作是一致的,DOM解析一次将文档加载入内存建立树型模型,但是如果XML ...

  7. IIS应用程序池频繁停止,任务管理器发现有多个w3wp.exe进程

    网站其中的一个应用服务器最近频繁出现IIS应用程序池停止的问题,通过任务管理器查看发现有6个w3wp.exe进程,一般一个应用程序池只占有一个w3wp.exe进程,为什么会出现多个呢,通过查看其它服务 ...

  8. Hashtable、HashMap

    JDK1.6 API public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, ...

  9. Python学习日记(一)——IDLE、运算符

    环境:win8.1+python2.7.8 一.名词解释: 1.IDLE:经常编程的同学相信对集成开发环境(Integrated Development Environment,IDE)应该非常熟悉了 ...

  10. swagger 接口文档,控制器 和 object类型的参数与返回值 的 注释不显示问题

    一.控制器的注释不显示:是因为配置swagger的时候没有将includeControllerXmlComments参数配置为true,因为其默认值为false 二.object 类型的参数和返回值 ...