mybatis关于数据库的相关操作

1:resultMap 的用法

当我们在使用联合查询的时候,我们提出会遇到我们想要的结果集,在本类的映射表中不相同,对此我们可以创建一个resultMap 结果集合:
1:首先,我们先在resultMap 中填写好,我们需要映射的字段,给resultMap 一个唯一id,因为我们在后面会使用到这个结果集
2:我们创建一个映射类,方便后期接收resultMap 映射的结果
3:我们的mapper接口中书写的方法,我们就可以使用刚刚创建的那个类作为返回类型来接收

在这里插入图片描述

2:limit的用法

limit用法解释
在这里插入图片描述
mybatis中sql的写法,需要注意limit的概念,第一个参数是起始位置,第二个参数是间距

在这里插入图片描述
上图中的bind也就是他想通过直接在xml文件中实现计算,其实,在后端数据那里计算也可以,看个人习惯吧

3:foreach的用法

这里的collection可以是:array,list,map,set
示例1

<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>

示例2

<insert id="insertUserAll">
		insert into sys_user(
		username,
		create_user_id,
		create_time,
		company_id,
		browse_companies_id,
		status
		)values
		<foreach collection="list" index="index" item="item" separator=",">
			(
			#{item.username},
			#{item.createUserId},
			DATE_FORMAT(#{item.createTime},'%Y-%m-%d %H:%i:%s'),
			#{item.companyId},
			#{item.browseCompaniesId},
			0
			)
		</foreach>

示例3

  <!--批量添加业务组对应设备-->
  <insert id="addList" parameterType="java.util.List" >
    INSERT INTO group_device(group_id,device_id,area_id) VALUES
    <foreach collection="list" item="item" index="index"
             separator=",">
      (
        #{item.groupId},#{item.deviceId},#{item.areaId}
      )
    </foreach>
  </insert>

4:if的用法

4.1.1 在WHERE条件中使用if

            需求:
                实现一个用户管理高级查询功能,根据输入的条件去检索用户信息。这个功能
                还需要支持以下三种情况:当只有输入用户名时,需要根据用户名进行模糊查
                询;当只有输入邮箱时,根据邮箱进行完全匹配;当同时输入用户名与邮箱时
                用这两个条件去查询匹配的用户。

            <if>便签有一个必填的属性test,test的属性值是一个符合OGNL要求的判断表达式,
            表达式的结果可以是true或者false,初次之外所有的的非0值都为true,只有0为false。
            且有如下规则:
                1.判断条件property!=null或者property==null:适用于任何类型的字段,用于判断属性值是否为空
                2.判断条件property!=''或者property=='':仅适用于String类型的字段,用于判断是否为空字符串
                3.and和or:当有多个判断条件时,适用and或or进行连接,嵌套的判断可以适用小括号分组。
<!--不能满足需求的代码,标记下模糊匹配的写法-->
    <select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
      select
        id,
        use_name userName,
        user_password userPassword,
        user_email userEmail,
        user_info userInfo,
        head_img headImg,
        create_time createTime
      from sys_user
      where
        user_name like concat('%',#{userName},'%') and
        uer_email=#{userEmail}
    </select>

    <!--改进后的代码-->
    <select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
      select
        id,
        use_name userName,
        user_password userPassword,
        user_email userEmail,
        user_info userInfo,
        head_img headImg,
        create_time createTime
      from sys_user
      where
        1=1
        <if test="userName!=null and userName!=''">
                and user_name like concat('%',#{userName},'%')
        </if>
        <if test="userEmail!=null and userEmail!=''">
                and user_email = #{userEmail}
        </if>
    </select>

    <!--
        4.1.3 在UPDATE更新列中使用if

            需求:
                只更新有变化的字段,需要注意,更新的时候不能将原来的值
                但没有发生变化的字段更新为空或null。
    -->

    <!--需求实现的代码-->
    <update id="updateByIdSelective">
        update sys_user
        set
          <if test="userName!=null and userName!=''">
                  user_name=#{userName},
          </if>
          <if test="userEmail!=null and userEmail!=''">
                  user_email=#{userEmail},
          </if>
          <if test="userInfo!=null and userInfo!=''">
                  user_info=#{userInfo},
          </if>
          <if test="headImg!=null">
                  head_img=#{headImg},
          </if>
          <if test="createTime!=null">
                  create_time=#{createTime},
          </if>
          id=#{id}
        where id=#{id}
    </update>

    <!--
        4.1.3 在INSERT动态插入列中使用if

            需求:
                在数据库中插入数据的时候,如果某一列的参数值不为空,就使用传入的值,如果传入
                的参数为空,就使用数据库中的默认值(通常是空),而不使用传入的空值。
    -->

    <insert id="insert2" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sys_user
          (id,user_name,user_password,
          <if test="userEmail!=null and uerEmail!=''">
              user_email,
          </if>
          user_info,head_img,create_time)
        VALUES
          (#{id},#{userName},#{userPassword},
          <if test="userEmail!=null and uerEmail!=''">
              #{userEmail},
          </if>
          #{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP})
    </insert>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值