Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别
一、在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)————传入参数方式以及#{}与${}的区别的更多相关文章
- Mybatis:传入参数方式以及#{}与${}的区别
一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...
- mybatis入门系列二之输入与输出参数
mybatis入门系列二之详解输入与输出参数 基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...
- JavaScript进阶系列01,函数的声明,函数参数,函数闭包
本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...
- MyBatis 中传递多个参数的 4 种方式
方式 1 :封装成对象入参 #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...
- 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中传入参数是list或map
原文地址:http://blog.csdn.net/aya19880214/article/details/41961235 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...
- MyBatis的传入参数parameterType类型
1. MyBatis的传入参数parameterType类型分两种 1. 1. 基本数据类型:int,string,long,Date; 1. 2. 复杂数据类型:类和Map 2. 如何获取参数中的值 ...
随机推荐
- PHP自学,第一阶段,基础学习
环境搭建OS X系统上 Win7执行OS X虚拟机 在 OS X上安装MAMP执行环境 IDE使用 Netbeans PHP版本号开发 数据库使用mysql 自学资料使用:PHP从入门到精通.pdf ...
- maven 私服中央库使用阿里云库
1.admin登录 进入remote repositories management 2. 设置地址
- Apache、Tomcat负载均衡与集群
一. 环境准备 1.软件下载 a) apache_2.0.55-win32-x86-no_ssl.msi: b) apache-tomcat-5.5.17.rar c) mod_jk-apache-2 ...
- android.animation(7) - android:animateLayoutChanges属性和LayoutTransition
前篇给大家讲了LayoutAnimation的知识,LayoutAnimation虽能实现ViewGroup的进入动画,但只能在创建时有效.在创建后,再往里添加控件就不会再有动画.在API 11后,又 ...
- TIME_WAIT详解
1.TCP四次挥手关闭链接过程 2.TIME_WAIT的产生条件主动关闭方在发送四次挥手的最后一个ACK会变为TIME_WAIT状态,保留此状态的时间为两个MSL 3.TIME_WAIT两个MSL的作 ...
- lucene4.7学习总结 (zhuan)
http://blog.csdn.NET/mdcmy/article/details/38167955?utm_source=tuicool&utm_medium=referral ***** ...
- 在eclipse中执行sql的编码问题
症状-分析: 刚才在eclipse中执行sql文件,发现数据进入数据库的时候总是乱码 后来查看MySQL的编码设置,全是UTF8,没问题,sql文件本身也是UTF8的编码 并且,使用MySQL的CMD ...
- Unix系统编程()信号:概念和概述
这篇将一口气学完信号的基本概念,但是有很多的细节,所以篇幅较长,请做好心理准备. (他大爷的,一口气没有学完,太懒了) 有以下主题: 各种不同信号及其用途 内核可能为进程产生信号的环境,以及某一进程向 ...
- Java反射机制的基本概念与使用
本篇文章分为以下几个部分: 1.认识反射 2.反射的源头(Class类) 3.利用反射操作构造方法 4.利用反射调用类中的方法 5.反射中的invoke方法 6.利用反射调用类中的属性 反射在我们普通 ...
- https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL_转
转自:https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL 因为项目中要用到TLS + SASL 来做安全认证层. 所以看了一些网上的资料, 这里做一个总结. 1. 首 ...