MyBatis


1.时间范围查询与模糊字查询

数据库:时间类型为datetime或date Date类型字段格式化注解: @JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”)

  1. -- 案例一 '%Y-%m-%d %H:%i:%s'
  2. <if test="beginTime!=null and beginTime!=''">
  3. and DATE_FORMAT(tr.add_time, '%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{beginTime}, '%Y-%m-%d')
  4. </if>
  5. <if test="endTime!=null and endTime!=''">
  6. and DATE_FORMAT(tr.add_time, '%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endTime}, '%Y-%m-%d')
  7. </if>
  8. -- 案例二
  9. <if test="beginTime!=null and beginTime!=''">
  10.   AND tm.add_time &gt;= #{beginTime}
  11. </if>
  12. <if test="endTime!=null and endTime!=''">
  13.   AND tm.add_time &lt;= #{endTime}
  14. </if>
<select id="selectUsersByKeyword" resultType="user">
        SELECT * FROM t_users 
          WHERE name LIKE concat('%',#{keyword},'%') <!-- 拼接'%' -->
</select>

<![CDATA[>=]]> 代替 大于等于

MyBatis编程学习 - 图1

3.Map封装返回结果

/**
         * 判断简历是否处于释放状态
         * @param resumeIds 简历主键ID集合
         * @return 条件匹配数据行数
         */
List<Map<String , Object>> judgmentFreedStatus(@Param("resumeIds") List<Long> resumeIds);
<select id="judgmentFreedStatus" resultType="java.util.Map">
    SELECT
        basic.id resumeId,
        basic.resume_code resumeCode,
        count(1) resumeNum
    FROM
        odrm.odrm_personnel_resume_basic_t basic,
        odrm.odrm_resume_status_sla_t sla
    WHERE
            basic.id = sla.resume_id
        AND sla.resume_status = 0
        AND basic.id in
      <foreach collection="resumeIds" item="resumeId" open="(" separator="," close=")">
          #{resumeId}
      </foreach>
      group by basic.id;
  </select>
List<Map<String , Object>> usageList = resumeSlaMapper.judgmentFreedStatus(resumeIds);
Integer resumeNum = (Integer)usageResume.get("resumeNum");
(Long)usageResume.get("resumeId");
(String)usageResume.get("resumeCode")

4、常见Mybatis

<if test="queryParam.interviewLanguages != null and queryParam.interviewLanguages.length > 0">
  and ( 1 = 1
  <foreach collection="queryParam.interviewLanguages" item="interviewLanguage">
    and interview_language like CONCAT('%',#{interviewLanguage},'%')
  </foreach>
  )
</if>

<if test="queryParam.resumeIdsByLink != null and queryParam.resumeIdsByLink.size > 0">
  AND basic.id in
  <foreach collection="queryParam.resumeIdsByLink" item="resumeId" separator="," open="(" close=")">
    #{resumeId}
  </foreach>
</if>

<if test="queryParam.education != null and '' != queryParam.education">
  and basic.education = #{queryParam.education}
</if>

只修改date的年月日,不修改时分秒!

<update id="updateStarAndEndTime">
  UPDATE odrm_interviewer_scheduling_t scheduling
  SET
  interview_start_time = ADDTIME( date( #{dateString} ) + INTERVAL 0 HOUR, time( interview_start_time ) ),
  interview_end_time = ADDTIME( date( #{dateString} ) + INTERVAL 0 HOUR, time( interview_end_time ) )
  WHERE
  scheduling.is_scheduling_effectiveness = 0
  AND scheduling.is_cache = 0
  AND scheduling.resume_id = #{resumeId}
  AND scheduling.phase_type = #{phaseType}
</update>

5、批量操作

<insert id="insertList" parameterType="java.util.List">
        INSERT INTO t_user
        (user_name,mobile)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.userName},#{item.mobile})
        </foreach>
</insert>
<update id="updateList" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE t_user  
            SET update_time = now(),user_name= #{item.userName} ,mobile = #{item.mobile}
            WHERE id = #{item.id}
        </foreach>
</update>

批量修改需要开启批命令操作,allowMultiQueries=true,因为默认是不支持批处理的。 jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true

6、主键回填

自增性主键回填 int insertSelective(UserT user); // user实体主键为空,其它填充属性存在 这样执行后,user实体的id属性就会被填充自增性id值

<insert id="insertSelective" useGeneratedKeys="true" keyProperty="id">
        insert into t_user
        <trim prefix="(" suffix=")" suffixOverrides=","> <!-- 增加(前缀,)后缀,自动忽略多余后缀 -->
            <if test="userName != null and '' != userName">
                user_name ,
            </if>
            <if test="phone != null and '' != phone">
                phone,
            </if>
            <if test="creatTime != null">
                creat_time,
            </if>
            <if test="isDelect != null">
                is_delect,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null and '' != userName">
                #{userName} ,
            </if>
            <if test="phone != null and '' != phone">
                #{phone} ,
            </if>
            <if test="creatTime != null">
                #{author} ,
            </if>
            <if test="isDelect != null">
                #{isDelect} ,
            </if>
        </trim>
    </insert>


🚀资料学习与引用