Mybatis:传入参数方式以及#{}与${}的区别
一、在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:传入参数方式以及#{}与${}的区别的更多相关文章
- Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别
一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...
- MyBatis传入参数为list、数组、map写法(转载)
MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...
- MyBatis传入参数为list、数组、map写法
1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...
- mybatis传入参数类型parameterType和输出结果类型resultType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...
- mybatis传入参数类型parameterType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...
- MyBatis传入参数为集合、数组SQL写法
参考:http://blog.csdn.net/small____fish/article/details/8029030 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合 ...
- MyBatis传入参数为集合 list 数组 map写法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- MyBatis 传入参数之parameterType
在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...
- MyBatis传入参数与parameterType
参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...
随机推荐
- configure: error: jpeglib.h not found.
编译出现错误: configure: error: jpeglib.h not found. 解决方法:yum install libjpeg libjpeg-devel -y libjpeg-dev ...
- hadoop map任务Combiner被调用的源码逻辑简要分析
从MapTask类中分析下去,看一下map任务是如何被调用并执行的. 入口方法是MapTask的run方法,看一下run方法的相关介绍: org.apache.hadoop.mapred. ...
- [C++]复制构造函数、赋值操作符与隐式类类型转换
问题:现有类A定义如下: class A{public: A(int a) //构造函数 { ...
- socket编程之select()
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...
- IO模型之非阻塞IO
1. IO模型非阻塞 IO Linux下,可以通过设置socket使其变为 non-blocking.当对一个non-blocking socket执行读操作时,流程是这个样子: 从图中可以看出,当用 ...
- IOZONE测试工具使用方法(转载)
IOZONE主要用来测试操作系统文件系统性能的测试工具,该工具所测试的范围主要有,write , Re-write, Read, Re-Read, Random Read, Random Write, ...
- 使用java获取自己的机器网卡
package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...
- nginx 1.12 配置解析php
server { listen 80; server_name foo.com; root /path; index index.html index.htm index.php; location ...
- Python模块部分
模块初识及正则表达式 Python re模块与正则表达式的运用 Python中常用模块一 模块和包
- 【Oracle】Oracle透明网关访问MSSQLServer
Oracle 数据库的透明网关 ( transparent gateway )是这样的一个接口:通过它,我们可以 sqlplus 操纵其他数据库,如 MS SQLServer . s ...