ORM Bee,如何使用Oracle的TO_DATE函数?
在Bee V2.4.0,可以这样使用:
java
LocaldatetimeTable selectBean=new LocaldatetimeTable();
Condition condition=BF.getCondition();
condition.op("localdatetime", Op.ge, new TO_DATE("2024-07-08", "YYYY-MM-DD"));
List<LocaldatetimeTable> list=suidRich.select(selectBean,condition);
Printer.printList(list);
日志:
sql
[INFO] [Bee] -------- Bee 2.4.0 --------
[INFO] [Bee] -------- Honey 2.4.0 --------
[INFO] [Bee] -------- BeeExt 2.4.0 --------
[DEBUG] [Bee] ========= Bee buildId 2.4.0.7
[DEBUG] [Bee] ========= Honey buildId 2.4.0.7
[DEBUG] [Bee] ========= BeeExt buildId 2.4.0.7
[INFO] [Bee] ========= get the dbName via url is: Oracle
[INFO] [Bee] select SQL: select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=TO_DATE(?, 'YYYY-MM-DD') [values]: 2024-07-08(String)
[INFO] [Bee] select SQL: ( ExecutableSql )
select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=TO_DATE('2024-07-08', 'YYYY-MM-DD') ;
[DEBUG] Use OriginalConn!
[INFO] [Bee] | <-- select rows: 2
[INFO] LocaldatetimeTable[id=12,name=null,datetime=null,timestamp=null,localdatetime=2024-07-08T23:55:35.534,ext=null,json=null]
[INFO] LocaldatetimeTable[id=13,name=null,datetime=null,timestamp=null,localdatetime=2024-07-08T23:56:01.521,ext=null,json=null]
condition.op("localdatetime", Op.ge, new TO_DATE("2024-07-08", "YYYY-MM-DD"));
对应SQL:
//where localdatetime>=TO_DATE('2024-07-08', 'YYYY-MM-DD') ;
sql
select * from ORDERS where total between 20 and 94
与
select * from ORDERS where total>=20 and total<=94
是等价的.
以下是Java的例子.
java
public class LocalDateTimeExam3 {
public static void main(String[] args) throws Exception {
LocaldatetimeTable bean = new LocaldatetimeTable();
bean.setId(10);
// bean.setLocaldatetime(LocalDateTime.now());
bean.setTimestamp(new Timestamp(System.currentTimeMillis()));
SuidRich suidRich = BF.getSuidRich();
// int a=suidRich.insert(bean);
// Logger.info("insert num:"+a);
LocaldatetimeTable selectBean = new LocaldatetimeTable();
Condition condition = BF.getCondition();
condition.op("localdatetime", Op.ge, new TO_DATE("2024-07-08", "YYYY-MM-DD"));
List<LocaldatetimeTable> list = suidRich.select(selectBean, condition);
Printer.printList(list);
String dateString = "2024-07-08T23:55:35.534";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date parsedDate = dateFormat.parse(dateString);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
// System.out.println("Timestamp in Java format: " + timestamp);
condition = BF.getCondition();
condition.op("localdatetime", Op.ge, timestamp);
condition.op("localdatetime", Op.le, timestamp); //改为另一个timestamp的值即可
list = suidRich.select(selectBean, condition);
Printer.printList(list);
}
使用到:
java
/**
* Add a expression condition.
* <br>This method cannot be used for SQL update set part.
* <br>When Op type is Op.in/Op.notIn,Object type can be List,Set,Number,Number[],String.
* @param field Field name
* @param op operator
* @param value Value of the field.
* @return Condition
*/
public Condition op(String field, Op op, Object value);
日志:
sql
[INFO] [Bee] -------- Bee 2.4.0 --------
[INFO] [Bee] -------- Honey 2.4.0 --------
[INFO] [Bee] -------- BeeExt 2.4.0 --------
[DEBUG] [Bee] ========= Bee buildId 2.4.0.7
[DEBUG] [Bee] ========= Honey buildId 2.4.0.7
[DEBUG] [Bee] ========= BeeExt buildId 2.4.0.7
[INFO] [Bee] ========= get the dbName via url is: Oracle
[INFO] [Bee] select SQL: select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=TO_DATE(?, 'YYYY-MM-DD') [values]: 2024-07-08(String)
[INFO] [Bee] select SQL: ( ExecutableSql )
select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=TO_DATE('2024-07-08', 'YYYY-MM-DD') ;
[DEBUG] Use OriginalConn!
[INFO] [Bee] | <-- select rows: 2
[INFO] LocaldatetimeTable[id=12,name=null,datetime=null,timestamp=null,localdatetime=2024-07-08T23:55:35.534,ext=null,json=null]
[INFO] LocaldatetimeTable[id=13,name=null,datetime=null,timestamp=null,localdatetime=2024-07-08T23:56:01.521,ext=null,json=null]
[INFO] [Bee] select SQL: select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=? and localdatetime<=? [values]: 2024-07-08 23:55:35.534(java.sql.Timestamp),2024-07-08 23:55:35.534(java.sql.Timestamp)
[INFO] [Bee] select SQL: ( ExecutableSql )
select id,name,datetime,timestamp,localdatetime,ext,json from localdatetime_table where localdatetime>=2024-07-08 23:55:35.534 and localdatetime<=2024-07-08 23:55:35.534 ;
[INFO] [Bee] | <-- select rows: 1
[INFO] LocaldatetimeTable[id=12,name=null,datetime=null,timestamp=null,localdatetime=2024-07-08T23:55:35.534,ext=null,json=null]