Mybatis输入输出映射及动态SQL Review
一、输入映射
通过parameterType指定输入参数的类型,可以是简单类型、pojo包装类、HashMap等
1、输入简单类型
<selectid="findUserById"parameterType="int"resultType="com.mybatis.po.User">
select*fromuserwhereid=#{id}
</select>
2、输入pojo包装类
<selectid="findUserById"parameterType="om.mybatis.po.User"resultType="com.mybatis.po.User">
select*fromuserwhereusernamelike‘%{user.username}%'
</select>
Pojo类可根据业务需求,创建某单一实体的扩展实体,User类的扩展类-User和订单实体的综合实体。
3、输入HashMap类型
<selectid="findUserById"parameterType="hashmap"resultType="com.mybatis.po.User">
select*fromuserwhereid=#{id}andusernamelike‘%{username}%'
</select>
参数id和username对应hashmap中的key-value
二、输出映射
1、resultType类型输出
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。适用于单表查询,级联查询时使用该类型输出需要重新创建关联pojo扩展类进行映射。
如果查询出来的列名和pojo中的属性名全部不一致,就不会创建该pojo对象。只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。映射失败的查询字段返回为空。
2、resultMap类型输出
如果查询出来的列名和pojo的属性名不一致时,可使用resultMap,通过定义一个resultMap列名和pojo属性名之间作一个映射关系。得以映射输出结果。
1)定义resultMap
<!--定义resultMap 将SELECTidid_,usernameusername_FROMUSER和User类中的属性作一个映射关系 type:resultMap最终映射的java对象类型,可以使用别名 id:对resultMap的唯一标识 --> <resultMaptype="user"id="userResultMap"> <!--id表示查询结果集中唯一标识 column:查询出来的列名 property:type指定的pojo类型中的属性名 最终resultMap对column和property作一个映射关系(对应关系) --> <idcolumn="id_"property="id"/> <!--result:对普通名映射定义 column:查询出来的列名 property:type指定的pojo类型中的属性名 最终resultMap对column和property作一个映射关系(对应关系) --> <resultcolumn="username_"property="username"/> </resultMap>
2)使用resultMap作为statement的输出映射类型
<!--使用resultMap进行输出映射
resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace
-->
<selectid="findUserByIdResultMap"parameterType="int"resultMap="userResultMap">
SELECTidid_,usernameusername_FROMUSERWHEREid=#{value}
</select>
三、动态SQL
Mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。
1、动态SQL示例
首先创建pojo类,提供该pojo对应的mapper映射文件,对crud方法分别配置
<updateid="updateByExampleSelective"parameterType="map">
updateitems
<set>
<iftest="record.id!=null">
id=#{record.id,jdbcType=INTEGER},
</if>
<iftest="record.name!=null">
name=#{record.name,jdbcType=VARCHAR},
</if>
<iftest="record.price!=null">
price=#{record.price,jdbcType=REAL},
</if>
<iftest="record.pic!=null">
pic=#{record.pic,jdbcType=VARCHAR},
</if>
<iftest="record.createtime!=null">
createtime=#{record.createtime,jdbcType=TIMESTAMP},
</if>
<iftest="record.detail!=null">
detail=#{record.detail,jdbcType=LONGVARCHAR},
</if>
</set>
<iftest="_parameter!=null">
<includerefid="Update_By_Example_Where_Clause"/>
</if>
</update>
2、SQL片段
将SQL中的判断条件进行封装,提高复用
1)定义sql片段
<!--定义sql片段
id:sql片段的唯一标识
最好基于单表来定义sql片段,这样话这个sql片段可重用性才高
在sql片段中不要包括where
-->
<sqlid="query_user_where">
<iftest="userCustom!=null">
<iftest="userCustom.sex!=nullanduserCustom.sex!=''">
anduser.sex=#{userCustom.sex}
</if>
<iftest="userCustom.username!=nullanduserCustom.username!=''">
anduser.usernameLIKE'%${userCustom.username}%'
</if>
<iftest="ids!=null">
<!--使用foreach遍历传入ids
collection:指定输入对象中集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!--使用实现下边的sql拼接:
AND(id=1ORid=10ORid=16)
-->
<foreachcollection="ids"item="user_id"open="AND("close=")"separator="or">
<!--每个遍历需要拼接的串-->
id=#{user_id}
</foreach>
<!--实现“andidIN(1,10,16)”拼接-->
<!--<foreachcollection="ids"item="user_id"open="andidIN("close=")"separator=",">
每个遍历需要拼接的串
#{user_id}
</foreach>-->
</if>
</if>
</sql>
2)引用sql片段
<!--用户信息综合查询
#{userCustom.sex}:取出pojo包装对象中性别值
${userCustom.username}:取出pojo包装对象中用户名称
-->
<selectid="findUserList"parameterType="cn.itcast.mybatis.po.UserQueryVo"resultType="cn.itcast.mybatis.po.UserCustom">
SELECT*FROMUSER
<!--
where可以自动去掉条件中的第一个and
-->
<where>
<!--引用sql片段的id,如果refid指定的id不在本mapper文件中,需要前边加namespace-->
<includerefid="query_user_where"></include>
<!--在这里还要引用其它的sql片段-->
</where>
</select>
注:在使用动态sql时注意
1、#{}和${}的不同
#{}表示一个占位符号,#{}接收输入参数,类型可以是简单类型,pojo、hashmap。当使用#{}接收简单类型参数时,#{}中可以写成value或其它名称。当接收pojo对象值,写入对象的属性值,形如对象.属性.属性.属性...的方式获取。
${}${}表示一个拼接符号,接收输入参数,类型可以是简单类型,pojo、hashmap。接收简单类型,${}中只能写成value。接受pojo对象时,与#{}相同。注意使用$拼接,会引用sql注入,所以不建议使用${}。
2、使用where标签,第一个and条件为空时,自动跳过。所以可以不添加where1=1保证sql语法正确。
3、使用sql执行insert之后返回主键id-selectKey元素的使用&SELECTLAST_INSERT_ID()函数的使用
1)id为自增类型
<!--添加用户
parameterType:指定输入参数类型是pojo(包括用户信息)
#{}中指定pojo的属性名,接收到pojo对象的属性值,mybatis通过OGNL获取对象的属性值
-->
<insertid="insertUser"parameterType="cn.itcast.mybatis.po.User">
<!--
将插入数据的主键返回,返回到user对象中
SELECTLAST_INSERT_ID():得到刚insert进去记录的主键值,只适用与自增主键
keyProperty:将查询到主键值设置到parameterType指定的对象的哪个属性
order:SELECTLAST_INSERT_ID()执行顺序,相对于insert语句来说它的执行顺序
resultType:指定SELECTLAST_INSERT_ID()的结果类型
-->
<selectKeykeyProperty="id"order="AFTER"resultType="java.lang.Integer">
SELECTLAST_INSERT_ID()
</selectKey>
insertintouser(username,birthday,sex,address)value(#{username},#{birthday},#{sex},#{address})
</insert>
2)id非自增,uuid类型-mysqlselectuuid()函数
<!--
使用mysql的uuid()生成主键
执行过程:
首先通过uuid()得到主键,将主键设置到user对象的id属性中
其次在insert执行时,从user对象中取出id属性值
-->
<selectKeykeyProperty="id"order="BEFORE"resultType="java.lang.String">
SELECTuuid()
</selectKey>
insertintouser(id,username,birthday,sex,address)value(#{id},#{username},#{birthday},#{sex},#{address})
以上所述是小编给大家介绍的Mybatis输入输出映射及动态SQLReview,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!