一、在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);
  • 1

Mapper.xml:

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

service:

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

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

DAO方法:

public User selectUserByNameAndAge(String name,int age);
  • 1

Mapper.xml:

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

service:

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

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

DAO方法:

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

Mapper.xml:

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

service:

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

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

DAO方法:

public User selectUserByNameAndAge(Map map);
  • 1

Mapper.xml:

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

service:

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

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

DAO方法:

public User selectUserByNameAndAge(Map map);
  • 1

Mapper.xml:

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

service:

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

Map<String,Object> map=new HasMap<String,Object>();
map.put("user",userParam);
User user = userDao.selectUserByNameAndAge(map);
  • 1
  • 2
  • 3
  • 4
  • 5

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

DAO方法:

public User selectUserByNameAndAge(User userParam);
  • 1

Mapper.xml:

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

service:

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

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

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

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

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

public List<User> selectUserByOrgId(String orgId);
  • 1

Mapper.xml:

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

service:

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

Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别的更多相关文章

  1. Mybatis:传入参数方式以及#{}与${}的区别

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

  2. mybatis入门系列二之输入与输出参数

    mybatis入门系列二之详解输入与输出参数   基础知识   mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...

  3. JavaScript进阶系列01,函数的声明,函数参数,函数闭包

    本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...

  4. MyBatis 中传递多个参数的 4 种方式

    方式 1 :封装成对象入参  #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...

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

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

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

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

  7. MyBatis 传入参数之parameterType

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

  8. (转载)mybatis中传入参数是list或map

    原文地址:http://blog.csdn.net/aya19880214/article/details/41961235 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  9. MyBatis的传入参数parameterType类型

    1. MyBatis的传入参数parameterType类型分两种 1. 1. 基本数据类型:int,string,long,Date; 1. 2. 复杂数据类型:类和Map 2. 如何获取参数中的值 ...

随机推荐

  1. HDU 4927 Series 1 ( 组合+高精度)

    pid=4927">Series 1 大意: 题意不好翻译,英文看懂也不是非常麻烦,就不翻译了. Problem Description Let A be an integral se ...

  2. DataGridView 的单元格的边框、 网格线样式的设定

    1) DataGridView 的边框线样式的设定DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的. BorderStyle 属性设定值是 ...

  3. C#设置文件夹用户权限

    var security = new DirectorySecurity();   string path=@"C:\temp" //设置权限的应用为文件夹本身.子文件夹及文件,所 ...

  4. shell脚本中执行mysql命令

    1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...

  5. python中sorted方法和列表的sort方法使用详解

    一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...

  6. iTunes历史各个版本下载地址

    地址:http://www.oldapps.com/itunes.php

  7. Redis JAVA客户端 Jedis常用方法

    Jedis 是 Redis 官方首选的 Java 客户端开发包 (redis的java版本的客户端实现) #MAVEN配置 <dependency> <groupId>redi ...

  8. Spider Studio 界面功能布局

    SS是Spider Studio (采集工作站) 的简称, 这是由GDT团队开发的一款互联网数据采集开发工具. 它以浏览器为基础, 运用JQuery技术, 结合脚本化C#的强大功能, 能够轻松解决各类 ...

  9. imx lcd HV和DE模式转换

    有些时候拿到的lcd手册中关于芯片的时序使用的DE模式的,而imx6内核中使用的参数设置趋势HV模式,应此就需要将DE模式的参数转化为HV模式. 参考链接: https://community.nxp ...

  10. android system.img 解压和打包

    system.img重新编译的时间太长,添加和更改的文件系统内容,往往通过对system.img加压再打包的方式. 参考链接 http://blog.csdn.net/whu_zhangmin/art ...