在做小程序的时候,经理让创建基于时间的分区表,由于以前没弄过,所以做起来做的磕磕碰碰的。现在总结下:
首先需要知道的 是:
Oracle分区表分为四类:范围分区表;列表分区表;哈希分区表;组合分区表
创建一个按字段数据范围分区的表,分区置于指定的不同表空间中
可以先创建一些分区,然后在把表和分区结合,也可以直接在创建表的时候结合分区名(分区会自动创建),我是直接创建的表然后结合的分区名:
CREATE TABLE "JMTDBA"."W_LOG"(
"GUID" VARCHAR2(64) NOT NULL ENABLE PRIMARY KEY,
"TYPE" CHAR(1) DEFAULT '1',
"REPORT_GUID" NUMBER,
"CREATE_TIME" DATE )
PARTITION BY RANGE("CREATE_TIME")
(
partition p1 values less than(to_date('2017-04-01 00:00:00','yyyy-mm-ddhh24:mi:ss')),
partition p2 values less than(to_date('2017-07-01 00:00:00','yyyy-mm-ddhh24:mi:ss')),
partition p3 values less than(to_date('2017-10-01 00:00:00','yyyy-mm-ddhh24:mi:ss')),
partition p4 values less than(to_date('2018-01-01 00:00:00','yyyy-mm-ddhh24:mi:ss'))
);
然后插入的时候用的语句:
INSERT INTO W_LOG (
GUID,
TYPE,
REPORT_GUID,
CREATE_TIME
)
VALUES
(
'',
'',
'',
"TO_DATE" (
'2017-09-13 :14:23:00','yyyy-mm-dd hh24:mi:ss'
)
)
select * from W_LOG
另一种是在表分区上进行查询:
select * from W_LOG
partition(p3)
首先是先创建一个实体类,
package com.jmt.wx.model;
import java.io.Serializable;
import java.util.Date;
/**
* 日志实体类
* Created by T430 on 2017/9/13.
*/
public class WLog implements Serializable{
private String guid;//日志唯一标识
private String type;//小程序类型,1,我要报警2,户政
private long reportGuid;//登录人的标识,用登录人的userId
private String createTime;//登录时间
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getReportGuid() {
return reportGuid;
}
public void setReportGuid(long reportGuid) {
this.reportGuid = reportGuid;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "{" +
"guid:'" + guid + '\'' +
", type:'" + type + '\'' +
", reportGuid:'" + reportGuid + '\'' +
", createTime:" + createTime +
'}';
}
}
下一步就是mybatis中添加日志的sql语句了:
INSERT INTO W_LOG (
GUID,
TYPE,
REPORT_GUID,
CREATE_TIME
)
VALUES
(
#{guid,jdbcType=VARCHAR},
#{type,jdbcType=VARCHAR},
#{reportGuid,jdbcType=BIGINT},
"TO_DATE" (
#{createTime,jdbcType=VARCHAR},'yyyy-mm-dd hh24:mi:ss'
)
)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
WLog wLog=new WLog();
wLog.setGuid(GUID);
wLog.setType(type);
wLog.setReportGuid(userId);
wLog.setCreateTime(sdf.format(new Date()));
logService.insertLog(wLog);
这些都写好后,就可以开始运行了,下面把我成功保存的截图给大家发一下:
插入成功!到此为止,分区表插入信息已经做完!
每天进步一点点,多学习!!!