mybatis中的一些记录

本文深入探讨了SQL查询中的高级用法,包括如何利用`IF`测试语句实现复杂的筛选逻辑,以及如何巧妙地处理特殊字符符号,确保查询语句的正确性和效率。通过实例演示了获取特定字段的最大ID、按父级名称和数量筛选标签数据的方法,并展示了如何在SQL查询中灵活运用`DISTINCT`关键字和条件判断。同时,文章特别提到了解决SQL语句中特殊符号冲突的技巧,确保代码的正确编译和执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DimOemtagMapper.xml

1.直接返回一个值,

<select id="getMaxOemTagId" resultType="java.lang.Integer">

select max(oemtag_id)   from DIM_OEMTAG

</select>

2.返回一个字段和返回全部字段都一个写法


<mapper namespace="com.feinno.fdc.report.report.persistence.mapper.tag.DimOemtagMapper" >
  <resultMap id="BaseResultMap" type="com.feinno.fdc.report.report.module.tag.DimOemtag" >
    <id column="OEMTAG" property="oemtag" jdbcType="VARCHAR" />
    <id column="CLIENT_TYPE_ID" property="clientTypeId" jdbcType="INTEGER" />
    <result column="OEMTAG_ID" property="oemtagId" jdbcType="INTEGER" />
    <result column="OEMTAG_NAME" property="oemtagName" jdbcType="VARCHAR" />
    <result column="TAG_FLAG" property="tagFlag" jdbcType="INTEGER" />
    <result column="TAG_FLAG_NAME" property="tagFlagName" jdbcType="VARCHAR" />
    <result column="OEMTAG_PARENT_ID" property="oemtagParentId" jdbcType="INTEGER" />
    <result column="OEMTAG_PARENT_NAME" property="oemtagParentName" jdbcType="VARCHAR" />
    <result column="BD" property="bd" jdbcType="VARCHAR" />
    <result column="EFF_DATE" property="effDate" jdbcType="TIMESTAMP" />
    <result column="EXP_DATE" property="expDate" jdbcType="TIMESTAMP" />
    <result column="UPD_DATE" property="updDate" jdbcType="TIMESTAMP" />
  </resultMap>

<select id="getTagSuggestionByParentName"resultMap="BaseResultMap">
select distinct(OEMTAG_PARENT_NAME) from DIM_OEMTAG
<if test="oemTagParentName!= null" >
      where OEMTAG_PARENT_NAME like ${oemTagParentName}
      </if>
      <if test="number!= null" >
      limit ${number};
      </if>
</select>


<select id="getTagSuggestionByOemTag" resultMap="BaseResultMap">
select * from DIM_OEMTAG
<if test="oemTag!= null" >
      where OEMTAG like ${oemTag}
      </if>
      <if test="number!= null" >
      limit ${number};
      </if>
</select>

3.对于sql语句里面有大于小于号之类的特殊符号,需要用<![CDATA[    xxxxxx   ]]>来解决,否则编译就通不过,

<select id="getLastweek" resultType="java.lang.String">
  <![CDATA[
  SELECT MAX(TIME_ID) 
  FROM D_TIME 
  WHERE TIME_TYPE= 4 AND 
   TIME_ID < 
   (select TIME_ID
    from D_TIME 
    WHERE TIME_TYPE= 4 AND START_TIME < CURRENT_TIMESTAMP AND END_TIME > CURRENT_TIMESTAMP);
  ]]>
  </select>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值