Mybatis Oracle
ROW_NUMBER() OVER()函数 :增加行号列
语法格式:row_number() over(partition by 分组列 order by 排序列 desc)
例子:分页功能中,用来排序号,行序号作用
row_number()over(order by a.sj desc) rn
意思是根据a表id列来排序,rn为拍完序后的序号
java中String形式的时间与Oracle数据库中DATE格式的时间之间的转化-获取
获取数据库时间并转化成JAVA的String类型获取:to_char()
select *, to_char(a.sj,'yyyy-mm-dd hh24:mi:ss') sj from table
输入以String格式的时间区间(2个时间)到数据库中查:to_date()
select * from table
where 1=1
<if test="sj != null">
and
<foreach collection="sj" index=index" item="item" separator="and">
<if test="index == 0">
<![CDATA [ sj >= to_date(#{item,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss') ]]>
</if>
<if test="index == 1">
<![CDATA [ sj < to_date(#{item,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss')+1 ]]>
</if>
</foreach>
</if>
综合实战:分页功能:使用上面2个功能实现
(根据age排序,条件查询,出生日期区间查询
select
a.id id,
a.name name,
a.age age,
to_char(a.sj,'yyyy-mm-dd hh24:mi:ss') sj,
row_number()over(order by a.sj desc) rn
from table_User a
where 1=1
<if test="name!= null and name!= ''">
and name like #{name,jdbcType=VARCHAR}||% --条件模糊查询
</if>
<if test="sj != null"> --根据输入的2个时间进行区间搜索,查找在这期间出生的学生
and
<foreach collection="sj" index=index" item="item" separator="and">
<if test="index == 0">
<![CDATA [ sj >= to_date(#{item,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss') ]]>
</if>
<if test="index == 1">
<![CDATA [ sj < to_date(#{item,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss')+1 ]]>
</if>
</foreach>
</if>
--分页查询
<if test="pageNum != null and pageNum != ''">
and rownum <= #{pageNum,jdbcType=INTEGER}
</if>
<if test="pageNum != null and pageNum != ''">
<![CDATA [ and rownum <= #{pageNum,jdbcType=INTEGER} * #{pageSize,jdbcType=INTEGER} ] ]>
</if>
where <![CDATA [ rn > #{pageNum,jdbcType=INTEGER} * (#{pageSize,jdbcType=INTEGER}-1) ] ]>
MyBatis的update语句中直接插入当前时间:sysdate
update set
sj = sysdate
where id=#{id,jdbcType=INTEGER}