2021/10/19 北京 myBatis动态sql与between

AND BEGNTIME <![CDATA[ >= ]]> #{stopTrtBeginTime}
AND (ENDTIME <![CDATA[ <= ]]> #{stopTrtEndTime} or ENDTIME is null)

查询单个用户和所有用户

 <select id="getUser" parameterType="int" resultType="me.gacl.domain.User">
     select * from users where id=#{id}    
 </select>

<select id="getAllUsers" resultType="me.gacl.domain.User">
     select * from users
 </select>

if-where,动态sql很关键

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

if-set,用到过,会自动去除“,”,好用的

    update user u
        <set>
            <if test="username != null and username != ''">
                u.username = #{username},
            </if>
            <if test="sex != null and sex != ''">
                u.sex = #{sex}
            </if>
        </set>
     where id=#{id}

between查询一个时间段的数据

     SELECT COUNT(*) FROM tbl_report_info
        <where>
            ActionTime BETWEEN #{startTime} AND #{endTime}
        </where>

resultMap作用:

可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。

原谅我,太香了下面这个。

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
  <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
  <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
  <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
  </association>
  <!-- 集合中的property须为oftype定义的pojo对象的属性-->
  <collection property="pojo的集合属性" ofType="集合中的pojo对象">
    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />  
  </collection>
</resultMap>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值