1、在mybatis plus 下 出现 ORA-00911: 无效字符
2、to_char( value, [ format_mask ], [ nls_language ] ) 函数
作用:将数字或日期将被转换为一个字符串。
如:select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
日期格式化参数如下:
ORACLE日期时间格式化参数详解 - shuzihua - 博客园
注意:Oracle是大小写不敏感的,所以日期格式化格式也不区分大小写。
但是Oracle大小写不敏感的前提条件:是在没有使用引号"
的前提下!!!
oracle mybatis 日期 查询例子:
1、传入参数为日期,LocalDate LocalDateTime 都可以,
public interface xxxMapper extends BaseMapper<xxx> {
List<xxx> xxx(@Param("date") LocalDate date);
}
<select id="xxx" resultType="xxx">
SELECT
*
FROM
xxx
<where>
<![CDATA[
AND create_time >= to_date(to_char(#{date},'yyyy-MM-dd')||' 00:00:00','yyyy-MM-dd HH24:MI:SS')
AND create_time <= to_date(to_char(#{date},'yyyy-MM-dd')||' 23:59:59','yyyy-MM-dd HH24:MI:SS')
]]>
</where>
</select>
注:使用<![CDATA[ ]]> 就不用转义
2、传入参数为日期字符串
public interface xxxMapper extends BaseMapper<xxx> {
#String startTime="2022-12-05 00:00:00";
#String endTime="2022-12-05 23:59:59";
List<xxx> xxx(@Param("startTime") String startTime,@Param("endTime") String endTime);
}
<select id="xxx" resultType="xxx">
SELECT
*
FROM
xxx
<where>
and create_time between to_date(#{startTime},'yyyy-MM-dd HH24:MI:SS')
and to_date(#{endTime},'yyyy-MM-dd HH24:MI:SS')
</where>
</select>
3、Oracle 等中的 group by与where 子句不能使用别名
Sql语句执行顺序为:
(7) SELECT
(8) DISTINCT <select_list>
(1) FROM <left_table>
(3) <join_type> JOIN <right_table>
(2) ON <join_condition>
(4) WHERE <where_condition>
(5) GROUP BY <group_by_list>
(6) HAVING <having_condition>
(9) ORDER BY <order_by_condition>
(10) LIMIT <limit_number>
(7)之后,比如order中,distinct中。
这是因为在SQL执行的时候,WHERE和GROUP语句在字段分类之前就已经执行了,在此期间,别名还没有生效,因此找不到指定别名的字段,报错。
注意:
在mysql中,group by中可以使用别名;where中不能使用别名;order by中可以使用别名。oracle严格遵循sql执行顺序的,groupby后面不能用别名。mysql特殊是因为mysql中对查询做了优化处理。
4、select ... for update
与mysql写法有点不同。会对查询到的结果集进行加x锁,行级锁。
语法如下:
SELECT ... FOR UPDATE [OF column_list][WAIT n|NOWAIT][SKIP LOCKED];
写法:
select for update 可以看成 select for update wait UN-LIMITED ,会永远等待,除非获得锁
select for update wait X—— 最多等待锁X秒。超时,java会报CannotAcquireLockException
select for update nowait —— 不会等待锁,得不到锁,超时会报错
select for update skip locked 则执行sql时,拿不到锁即不等待,也不报资源忙异常
5、oracle 字段类型varchar nvarchar 区别
6、oracle number 数据类型 与所占空间大小