当前位置: 移动技术网 > IT编程>开发语言>Java > Mybatis输入输出映射及动态SQL Review

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、输入映射

    通过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,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网