From b4633897334ea01f4540bbbbeb7f97a43f1d3e6b Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Thu, 2 Mar 2017 15:43:34 +0800 Subject: [PATCH] --- .../source/DistributeTableStrategy.java | 8 +- test/org/redkale/test/source/LoginRecord.java | 14 +- .../org/redkale/test/source/LoginRecord2.java | 158 ------------------ 3 files changed, 12 insertions(+), 168 deletions(-) delete mode 100644 test/org/redkale/test/source/LoginRecord2.java diff --git a/src/org/redkale/source/DistributeTableStrategy.java b/src/org/redkale/source/DistributeTableStrategy.java index ad8fe70d0..9fdd58ae0 100644 --- a/src/org/redkale/source/DistributeTableStrategy.java +++ b/src/org/redkale/source/DistributeTableStrategy.java @@ -29,9 +29,7 @@ public interface DistributeTableStrategy { * * @return 带库名的全表名 */ - default String getTable(String table, Serializable primary) { - return null; - } + public String getTable(String table, Serializable primary); /** * 获取对象的表名 @@ -43,9 +41,7 @@ public interface DistributeTableStrategy { * * @return 带库名的全表名 */ - default String getTable(String table, FilterNode node) { - return null; - } + public String getTable(String table, FilterNode node); /** * 获取对象的表名 diff --git a/test/org/redkale/test/source/LoginRecord.java b/test/org/redkale/test/source/LoginRecord.java index 89f5d1da0..105e62394 100644 --- a/test/org/redkale/test/source/LoginRecord.java +++ b/test/org/redkale/test/source/LoginRecord.java @@ -17,9 +17,8 @@ import org.redkale.source.*; public class LoginRecord extends BaseEntity { @Id - @GeneratedValue - @Column(comment = "UUID") - private String loginid = ""; //UUID + @Column(comment = "主键ID; 值=create36time(9位)+UUID(32位)") + private String loginid = ""; //主键ID; 值=create36time(9位)+UUID(32位) @Column(updatable = false, comment = "C端用户ID") private long userid; //C端用户ID @@ -129,9 +128,16 @@ public class LoginRecord extends BaseEntity { return getTable(table, 0, bean.getCreatetime()); } + //根据主键ID查询单个记录时调用本方法 + @Override + public String getTable(String table, Serializable primary) { + String id = (String) primary; + return getTable(table, 0, Long.parseLong(id.substring(0, 9), 36)); + } + private String getTable(String table, int day, long createtime) { int pos = table.indexOf('.'); - String year = (day > 0 ? "" + day / 10000 : String.format(yearformat, createtime)); + String year = (day > 0 ? "" + day / 10000 : String.format(yearformat, createtime)); //没有day取createtime return "platf_login_" + year + "." + table.substring(pos + 1) + "_" + (day > 0 ? day : String.format(dayformat, createtime)); } } diff --git a/test/org/redkale/test/source/LoginRecord2.java b/test/org/redkale/test/source/LoginRecord2.java deleted file mode 100644 index b7acdd4db..000000000 --- a/test/org/redkale/test/source/LoginRecord2.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkale.test.source; - -import java.io.Serializable; -import javax.persistence.*; -import org.redkale.source.*; -import org.redkale.util.Utility; - -/** - * - * @author zhangjx - */ -@DistributeTable(strategy = LoginRecord2.TableStrategy.class) -public class LoginRecord2 extends BaseEntity { - - @Id - @Column(comment = "主键ID; 值=UUID+create36time") - private String loginid = ""; //主键ID; 值=UUID+create36time - - @Column(updatable = false, comment = "C端用户ID") - private long userid; //C端用户ID - - @Column(updatable = false, comment = "登录网络类型; wifi/4g/3g") - private String netmode = ""; //登录网络类型; wifi/4g/3g - - @Column(updatable = false, comment = "APP版本信息") - private String appversion = ""; //APP版本信息 - - @Column(updatable = false, comment = "APP操作系统信息") - private String appos = ""; //APP操作系统信息 - - @Column(updatable = false, comment = "登录时客户端信息") - private String loginagent = ""; //登录时客户端信息 - - @Column(updatable = false, comment = "登录时的IP") - private String loginaddr = ""; //登录时的IP - - @Column(updatable = false, comment = "创建时间") - private long createtime; //创建时间 - - /** 以下省略getter setter方法 */ - // - public void setLoginid(String loginid) { - this.loginid = loginid; - } - - public String getLoginid() { - return this.loginid; - } - - public void setUserid(long userid) { - this.userid = userid; - } - - public long getUserid() { - return this.userid; - } - - public void setNetmode(String netmode) { - this.netmode = netmode; - } - - public String getNetmode() { - return this.netmode; - } - - public String getAppversion() { - return appversion; - } - - public void setAppversion(String appversion) { - this.appversion = appversion; - } - - public String getAppos() { - return appos; - } - - public void setAppos(String appos) { - this.appos = appos; - } - - public void setLoginagent(String loginagent) { - this.loginagent = loginagent; - } - - public String getLoginagent() { - return this.loginagent; - } - - public void setLoginaddr(String loginaddr) { - this.loginaddr = loginaddr; - } - - public String getLoginaddr() { - return this.loginaddr; - } - - public void setCreatetime(long createtime) { - this.createtime = createtime; - } - - public long getCreatetime() { - return this.createtime; - } - - //创建对象 - public static void main(String[] args) throws Throwable { - LoginRecord2 record = new LoginRecord2(); - long now = System.currentTimeMillis(); - record.setCreatetime(now); //设置创建时间 - String create36time = Long.toString(now, 36); //时间的36进制 - if (create36time.length() < 9) create36time = "0" + create36time; //当前时间值的36进制只可能是8位或9位,不足9位填充0 - record.setLoginid(Utility.uuid() + create36time); //主键的生成策略 - //.... 填充其他字段 - //source.insert(record); - } - - public static class TableStrategy implements DistributeTableStrategy { - - private static final String dayformat = "%1$tY%1$tm%1$td"; - - private static final String yearformat = "%1$tY"; - - //过滤查询时调用本方法 - @Override - public String getTable(String table, FilterNode node) { - Serializable day = node.findValue("#day"); - if (day != null) getTable(table, (Integer) day, 0L); //存在#day参数则直接使用day值 - Serializable time = node.findValue("#createtime"); //存在createtime则使用最小时间,且createtime的范围必须在一天内,因为本表以天为单位建表 - return getTable(table, 0, (time == null ? 0L : (time instanceof Range ? ((Range.LongRange) time).getMin() : (Long) time))); - } - - //创建或单个查询时调用本方法 - @Override - public String getTable(String table, LoginRecord2 bean) { - return getTable(table, 0, bean.getCreatetime()); - } - - //根据主键ID查询单个记录时调用本方法 - @Override - public String getTable(String table, Serializable primary) { - String loginid = (String) primary; - String create36time = loginid.substring(loginid.length() - 9); //固定最后9位为创建时间的36进制值 - return getTable(table, 0, Long.parseLong(create36time, 36)); - } - - private String getTable(String table, int day, long createtime) { - int pos = table.indexOf('.'); - String year = (day > 0 ? "" + day / 10000 : String.format(yearformat, createtime)); - return "platf_login_" + year + "." + table.substring(pos + 1) + "_" + (day > 0 ? day : String.format(dayformat, createtime)); - } - } -}