使用mybatis 连接Oracle 数据库 xml 文件中需要注意的问题

1:首先使用模糊查询的时候

1: 在mysql 中
 select <include refid="Base_Column_List" />
        FROM a_ll_dist_g_reas_record
        <where>
            <if test="null != orgName and '' != orgName">
                org_no like concat('%',#{orgName},'%')
            </if>
            <if test="null != pReasCode and '' != pReasCode">
                and orgName  like concat('%',#{orgName},'%')
            </if>
            <if test="null != reasCode and '' != reasCode">
                and reasCode  like concat('%',#{reasCode },'%')
            </if>
            <if test="null != timeFlag and '' != timeFlag">
                and timeFlag like concat('%',#{timeFlag },'%')
            </if>
            

2:使用Orcale的时候:   需要 多包一个  concat(concat)

 select <include refid="Base_Column_List" />
        FROM a_ll_dist_g_reas_record
        <where>
            <if test="null != orgName and '' != orgName">
                orgName like concat(concat('%',#{orgName}),'%')
            </if>
            <if test="null != pReasCode and '' != pReasCode">
                and pReasCode  like concat(concat('%',#{pReasCode }),'%')
            </if>
            <if test="null != reasCode and '' != reasCode">
                and reasCode like concat(concat('%',#{reasCode }),'%')
            </if>
            <if test="null != timeFlag and '' != timeFlag">
                and timeFlag like concat(concat('%',#{timeFlag }),'%')
            </if>
         

使用${} 

            <if test="parameter.indexName != null and parameter.indexName != ''">
                and INDEX_NAME like '%${parameter.indexName}%'
            </if>

时间范围: 将字符串类型的时间转换为date 类型

       <![CDATA[]]>  中的--> []  的写什么就是什么

           <![CDATA[
                and LOGIN_TIME >= to_date(#{startLoginTime},'yyyy-MM-dd')
            ]]>
            <![CDATA[
                and LOGIN_TIME <= to_date(#{endLoginTime},'yyyy-MM-dd')
            ]]>

3:使用Oracle 连接mybatis  时, 因为有一些业务场景规定,数据库中有一下字段,在前端为非必填字段  ,所以在插入数据的时候,会少携带一下参数,但是Oracle 数据库在使用mybatis  的时候,不允许插入数据为空,就会抛出报错信息。这时我们需要在sql 语句中添加 类型字段

例子如下:

  UPDATE a_ll_dist_g_reas_record
       set
           org_no = #{orgNo,jdbcType=VARCHAR},
           org_name = #{orgName,jdbcType=VARCHAR},
           time_flag = #{timeFlag,jdbcType=VARCHAR},
           rep_flag = #{repFlag,jdbcType=VARCHAR},
           p_reas_code = #{pReasCode,jdbcType=VARCHAR},
           reas_code = #{reasCode,jdbcType=VARCHAR},
           reas_desc = #{reasDesc,jdbcType=VARCHAR},
           rep_code = #{repCode,jdbcType=VARCHAR},
           oper_name = #{operName,jdbcType=VARCHAR}
        WHERE id = #{id}

4:在判断时间类型的时候 需要注意不要把时间字段判断  空字符串

           关于时间范围的搜索条件
           <if test="startTime != null">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time &lt;= #{endTime}
            </if>

           不要判断是否为空字符串
                <if test="null != createTime">
                    create_time,
                </if>

posted @ 2023-01-08 17:33  wode林夕  阅读(16)  评论(0编辑  收藏  举报  来源