MyBatis传入参数与parameterType
参考:http://openwares.net/database/mybatis_parametertype.html
Mybatis的Mapper文件中的select、insert、update、delete元素中有一个parameterType属性,用于对应的mapper接口方法接受的参数类型。
可以接受的参数类型有基本类型和复杂类型。
mapper接口方法一般接受一个参数,可以通过使用@Param注释将多个参数绑定到一个map做为输入参数。
简单数据类型
mapper接口方法:
|
1
|
User selectByPrimaryKey(Integer id); |
sql映射:
|
1
2
3
4
5
6
|
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from base.tb_user where id = #{id,jdbcType=INTEGER}</select> |
对于简单数据类型,sql映射语句中直接#{变量名}这种方式引用就行了,其实这里的"变量名"可以是任意的。mapper接口方法传递过来的值,至于其叫什么名字其实是不可考也没必要知道的。 而且JAVA反射只能获取方法参数的类型,是无从得知方法参数的名字的。
比如上面这个示例中,使用#{id}来引用只是比较直观而已,使用其他名字来引用也是一样的。所以当在if元素中test传递的参数时,就必须要用_parameter来引用这个参数了。像这样:
|
1
2
3
4
5
6
7
8
|
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from tb_user <if test="_parameter != 0"> where id = #{id,jdbcType=INTEGER} </if></select> |
如果test测试条件中使用id就会提示错误,因为这个参数其实没有名字,只是一个值或引用而已,只能使用_parameter来引用。
- 对象类型
传入JAVA复杂对象类型的话,sql映射语句中就可以直接引用对象的属性名了,这里的属性名是实实在在的真实的名字,不是随意指定的。 mapper接口方法:
|
1
|
int insert(User user); |
sql映射:
|
1
2
3
|
<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> insert into tb_user (name, sex) values (#{name,jdbcType=CHAR}, #{sex,jdbcType=CHAR}) |
虽然可以明确的引用对象的属性名了,但如果要在if元素中测试传入的user参数,仍然要使用_parameter来引用传递进来的实际参数,因为传递进来的User对象的名字是不可考的。如果测试对象的属性,则直接引用属性名字就可以了。
测试user对象:
|
1
|
<if test="_parameter != null"> |
测试user对象的属性:
|
1
|
<if test="name != null"> |
- map类型
传入map类型,直接通过#{keyname}就可以引用到键对应的值。使用@param注释的多个参数值也会组装成一个map数据结构,和直接传递map进来没有区别。
mapper接口:
|
1
|
int updateByExample(@Param("user") User user, @Param("example") UserExample example); |
sql映射:
|
1
2
3
4
5
6
7
|
<update id="updateByExample" parameterType="map" > update tb_user set id = #{user.id,jdbcType=INTEGER}, ... <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> |
注意这里测试传递进来的map是否为空,仍然使用_parameter
- 集合类型
You can pass a List instance or an Array to MyBatis as a parameter object. When you do, MyBatis will automatically wrap it in a Map, and key it by name. List instances will be keyed to the name "list" and array instances will be keyed to the name "array".
可以传递一个List或Array类型的对象作为参数,MyBatis会自动的将List或Array对象包装到一个Map对象中,List类型对象会使用list作为键名,而Array对象会用array作为键名。
集合类型通常用于构造IN条件,sql映射文件中使用foreach元素来遍历List或Array元素。
mapper接口:
|
1
|
User selectUserInList(List<Interger> idlist); |
sql动态语句映射:
|
1
2
3
4
5
6
7
8
9
|
<select id="selectUserInList" resultType="User"> SELECT * FROM USER WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach></select> |
- 对象类型中的集合属性
对于单独传递的List或Array,在SQL映射文件中映射时,只能通过list或array来引用。但是如果对象类型有属性的类型为List或Array,则在sql映射文件的foreach元素中,可以直接使用属性名字来引用。 mapper接口:
|
1
|
List<User> selectByExample(UserExample example); |
sql映射文件:
|
1
2
3
|
<where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > |
在这里,UserExample有一个属性叫oredCriteria,其类型为List,所以在foreach元素里直接用属性名oredCriteria引用这个List即可。
item="criteria"表示使用criteria这个名字引用每一个集合中的每一个List或Array元素
MyBatis传入参数与parameterType的更多相关文章
- MyBatis 传入参数之parameterType
在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...
- mybatis传入参数类型parameterType和输出结果类型resultType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...
- mybatis传入参数类型parameterType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...
- MyBatis传入参数为list、数组、map写法
1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item,index,collection,open,sep ...
- MyBatis传入参数为list、数组、map写法(转载)
MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...
- MyBatis传入参数
在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和Ja ...
- 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传入参数类型为Map
mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...
随机推荐
- Response.Redirect()、Server.Execute和Server.Transfer的区别
1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...
- Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 常见计算机基础笔试题总结quickstart
[本文链接] 1. 以下是一颗平衡二叉树,请画出插入键值3以后的这颗平衡二叉树. 分析:考察平衡二叉树的基本操作,插入3变成不平衡,需要节点5右旋一次,节点2左旋一次.. 2. 表达式X=A+(B*( ...
- HTTP 错误 500.19- Internal Server Error 错误解决方法
1.点击发布的文件夹,选择属性 2.选择安全,添加一个用户就可以了,设置为完全 --今天公司网页打开出现Server Error in '/' Application.怎么样解决. 解决方法:控制面板 ...
- Python字符编码
http://www.runoob.com/python/python-strings.html ASCII Unicode UTF-8 # -*- coding: utf-8 -*- 格式化 %运算 ...
- Filezilla无法确定拖放操作目标,由于shell未正确安装__解决办法
开始--运行--输入regsvr32空格 然后将filezila安装目录下的fzshellext.dll拖拽到[regsvr32空格]之后 注:64位电脑注意拖拽的文件为fzshellext_64 ...
- Effective C++ -----条款44:将与参数无关的代码抽离templates
Templates生成多个classes和多个函数,所以任何template代码都不该与某个造成膨胀的template参数产生相依关系. 因非类型模板参数(non-type template para ...
- jquery this 与javascript的this
<div class="list"> <table> <thead> <tr> <th width="110&quo ...
- [Android Pro] Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
方法一.在Activity的onCreate中添加如下代码 getWindow().setFormat(PixelFormat.TRANSLUCENT); reference to : http:/ ...