您的位置:首页 > 数据库

Mybatis输入输出映射及动态SQL Review

2017-02-14 13:58 786 查看

一、输入映射

    通过parameterType指定输入参数的类型,可以是简单类型、pojo包装类、HashMap等

1、输入简单类型

<select id="findUserById" parameterType="int" resultType="com.mybatis.po.User">
select * from user where id=#{id}
</select>

2、输入pojo包装类

<select id="findUserById" parameterType="om.mybatis.po.User" resultType="com.mybatis.po.User">
select * from user where username like ‘%{user.username}%'
</select>

     Pojo类可根据业务需求,创建某单一实体的扩展实体,User类的扩展类-User和订单实体的综合实体。

3、输入HashMap类型

<select id="findUserById" parameterType="hashmap" resultType="com.mybatis.po.User">
select * from user where id=#{id} and username like ‘%{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
将SELECT id id_,username username_ FROM USER 和User类中的属性作一个映射关系
type:resultMap最终映射的java对象类型,可以使用别名
id:对resultMap的唯一标识
-->
<resultMap type="user" id="userResultMap">
<!-- id表示查询结果集中唯一标识
column:查询出来的列名
property:type指定的pojo类型中的属性名
最终resultMap对column和property作一个映射关系 (对应关系)
-->
<id column="id_" property="id"/>
<!-- result:对普通名映射定义
column:查询出来的列名
property:type指定的pojo类型中的属性名
最终resultMap对column和property作一个映射关系 (对应关系)
-->
<result column="username_" property="username"/>
</resultMap>

2)使用resultMap作为statement的输出映射类型

<!-- 使用resultMap进行输出映射
resultMap:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace
-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
SELECT id id_,username username_ FROM USER WHERE id=#{value}
</select>

三、动态SQL

     Mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

 1、动态SQL示例

     首先创建pojo类,提供该pojo对应的mapper映射文件,对crud方法分别配置

<update id="updateByExampleSelective" parameterType="map" >
update items
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null" >
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.price != null" >
price = #{record.price,jdbcType=REAL},
</if>
<if test="record.pic != null" >
pic = #{record.pic,jdbcType=VARCHAR},
</if>
<if test="record.createtime != null" >
createtime = #{record.createtime,jdbcType=TIMESTAMP},
</if>
<if test="record.detail != null" >
detail = #{record.detail,jdbcType=LONGVARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>

2、SQL片段

     将SQL中的判断条件进行封装,提高复用

1)定义sql片段

<!-- 定义sql片段
id:sql片段的唯 一标识
最好基于单表来定义sql片段,这样话这个sql片段可重用性才高
在sql片段中不要包括 where
-->
<sql id="query_user_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
and user.sex = #{userCustom.sex}
</if>
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username LIKE '%${userCustom.username}%'
</if>
<if test="ids!=null">
<!-- 使用 foreach遍历传入ids
collection:指定输入 对象中集合属性
item:每个遍历生成对象中
open:开始遍历时拼接的串
close:结束遍历时拼接的串
separator:遍历的两个对象中需要拼接的串
-->
<!-- 使用实现下边的sql拼接:
AND (id=1 OR id=10 OR id=16)
-->
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
<!-- 每个遍历需要拼接的串 -->
id=#{user_id}
</foreach>
<!-- 实现 “ and id IN(1,10,16)”拼接 -->
<!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
每个遍历需要拼接的串
#{user_id}
</foreach> -->
</if>
</if>
</sql>

2)引用sql片段

<!-- 用户信息综合查询
#{userCustom.sex}:取出pojo包装对象中性别值
${userCustom.username}:取出pojo包装对象中用户名称
-->
<select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" resultType="cn.itcast.mybatis.po.UserCustom">
SELECT * FROM USER
<!--
where可以自动去掉条件中的第一个and
-->
<where>
<!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前边加namespace -->
<include refid="query_user_where"></include>
<!-- 在这里还要引用其它的sql片段 -->
</where>
</select>

注:在使用动态sql时注意

1、#{}和${}的不同

     #{}表示一个占位符号,#{}接收输入参数,类型可以是简单类型,pojo、hashmap。当使用#{}接收简单类型参数时,#{}中可以写成value或其它名称。当接收pojo对象值,写入对象的属性值,形如对象.属性.属性.属性...的方式获取。

     ${}${}表示一个拼接符号,接收输入参数,类型可以是简单类型,pojo、hashmap。接收简单类型,${}中只能写成value。接受pojo对象时,与#{}相同。注意使用$拼接,会引用sql注入,所以不建议使用${}。

2、使用where 标签,第一个and 条件为空时,自动跳过。所以可以不添加where 1=1 保证sql语法正确。

3、使用sql执行insert之后返回主键id-selectKey元素的使用&SELECT LAST_INSERT_ID() 函数的使用

1)id为自增类型

<!-- 添加用户
parameterType:指定输入 参数类型是pojo(包括 用户信息)
#{}中指定pojo的属性名,接收到pojo对象的属性值,mybatis通过OGNL获取对象的属性值
-->
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<!--
将插入数据的主键返回,返回到user对象中
SELECT LAST_INSERT_ID():得到刚insert进去记录的主键值,只适用与自增主键
keyProperty:将查询到主键值设置到parameterType指定的对象的哪个属性
order:SELECT LAST_INSERT_ID()执行顺序,相对于insert语句来说它的执行顺序
resultType:指定SELECT LAST_INSERT_ID()的结果类型
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>

2)id非自增,uuid类型-mysql select uuid()函数

<!--
使用mysql的uuid()生成主键
执行过程:
首先通过uuid()得到主键,将主键设置到user对象的id属性中
其次在insert执行时,从user对象中取出id属性值
-->
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
SELECT uuid()
</selectKey>
insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address})

以上所述是小编给大家介绍的Mybatis输入输出映射及动态SQL Review,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 映射 sql review