mybatis(三)动态sql与多表查询对resultMap的封装

mybatis基础讲解:
mybatis(一)数据库事务与环境搭建
mybatis(二)mybatis的执行流程
mybatis(三)动态sql与多表查询
mybatis(四)多参传递、延时加载、缓存及注解开发


在这里插入图片描述
一、动态sql

1.< if > < /if >

//dao.xml文件中的部分代码
<select id="findByUser" resultType="user" parameterType="user">
 select * from user where 1=1

<if test="username!=null and username != '' ">
 and username like #{username}
</if>

<if test="address != null">
 and address like #{address}
</if>
</select>

注意:标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。另外要注意 where 1=1 的作用~!

  1. < where >< where >
//为了简化上面 where 1=1 的条件拼装,我们可以采用<where>标签来简化开发。

<select id="findByUser" resultType="user" parameterType="user">

select * from user

<include refid="defaultSql"></include>

<where>
<if test="username!=null and username != '' ">
 and username like #{username}
</if>

<if test="address != null">
 and address like #{address}
</if>
</where>
</select>


  1. < foreach >< /froeach >
<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">

<include refid="defaultSql"></include>
<where>
<if test="ids != null and ids.size() > 0">
<foreach	collection="ids"	open="id	in	(	"	close=")"	item="uid"
separator=",">
#{uid}
</foreach>
</if>
</where>
</select>

SQL 语句:
select 字段 from user where id in (?)
<foreach>标签用于遍历集合,它的属性:
collection:代表要遍历的集合元素,注意编写时不要写#{} 
open:代表语句的开始部分
close:代表结束部分
item:代表遍历集合的每个元素,生成的变量名
sperator:代表分隔符
  1. < sql>< /sql> (< include>)
Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。

<!-- 抽取重复的语句代码片段 -->
<sql id="defaultSql"> select * from user
</sql>

<!-- 配置查询所有操作 -->
<select id="findAll" resultType="user">

<include refid="defaultSql"></include>
</select>

5、 choose, when, otherwise

有些时候,我们不想用到所有的条件语句,而只想从中择其一二。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>

    <otherwise>
      AND featured = 1
    </otherwise>

  </choose>
</select>

6、set

<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
    
    </set>
  where id=#{id}
</update>

这里,set 元素会动态前置 SET 关键字,同时也会消除无关的逗号,因为用了条件语句之后很可能就会在生成的赋值语句的后面留下这些逗号。

7、trim

常用的属性: 
prefix=”where”//给第一符合条件的语句 加上前缀where 
prefixOverrides=”and” //将最后一条语句的 前缀and 覆盖 
suffix=”and” //给第一符合条件的语句 加上后缀 and 
suffixOverrides=”and”//将最后一条语句的后缀 and 覆盖 
--------------------- 
  <select id="selectEmployeeList" >
        select
        *
        from t_emp e
        //表示给第一个符合条件的语句前加 where,把最后一个语句的suffixOverrides="and" 指定的and 覆盖掉
          <trim  prefix="where" suffixOverrides="and">
            <if test="name!=null and name!=''">
                  e.emp_name=#{name,jdbcType=VARCHAR} and
            </if>
            <if test="dep!=null">
                  e.emp_dep=#{dep.id,jdbcType=INTEGER} and
            </if>
          </trim>
    </select>

二、多表查询,对结果resultMap的封装

  1. 一对一查询 (返回值为一个对象)
  • 方式1:定义实体类(继承)作为输出类型
    比如说:查询的结果是表1和表2的整合表,那么就新建一个实体类,使其属性与该整合表相对应。

  • 方式2:定义专门的 resultMap 用于映射查询结果

type 属性:指定实体类的全限定类名

id 属性:给定一个唯一标识,是给查询 select 标签引用用的。
-->
返回值是一个集合
<resultMap type="com.xmcc.domain.User" id="userMap">

<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
<result column="birthday" property="userBirthday"/>
</resultMap>

id 标签:用于指定主键字段

result 标签:用于指定非主键字段

column 属性:用于指定数据库列名

property 属性:用于指定实体类属性名称
  1. 一对多查询(返回值为一个对象,该对象有一个属性是另一个对象)
<!-- 建立对应关系 -->
返回值是一个Account对象
Account实体类中有属性User类

<resultMap type="account" id="accountMap">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
<!-- 它是用于指定从表方的引用实体属性的 -->
<association property="user" javaType="user">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
</association>
</resultMap>

<select id="findAll" resultMap="accountMap">
select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid =u.id;
</select>

3、返回结果 (一个对象有一个属性List<对象>)

查询结果为:对象中包含list<Account> 集合 

<resultMap type="user" id="userMap">
<id column="id" property="id"></id>
<result column="username" property="username"/>
<result column="address" property="address"/>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
-->
<collection property="accounts" ofType="account">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/></collection>
</resultMap>

<!-- 配置查询所有操作,-->
<select id="findAll" resultMap="userMap">
select u.*,a.id as aid ,a.uid,a.money from user u left outer join account a on u.id =a.uid
</select>
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值