This commit is contained in:
Redkale
2016-11-25 17:20:45 +08:00
parent 9d85a4dcaf
commit 09a5b41d96
3 changed files with 273 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
/*
* 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 org.redkale.convert.json.*;
/**
*
* @author zhangjx
*/
public abstract class BaseEntity implements Serializable {
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
}

View File

@@ -0,0 +1,138 @@
/*
* 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.*;
/**
*
* @author zhangjx
*/
@DistributeTable(strategy = LoginRecord.TableStrategy.class)
public class LoginRecord extends BaseEntity {
@Id
@GeneratedValue
@Column(comment = "UUID")
private String seqid = ""; //UUID
@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 setSeqid(String seqid) {
this.seqid = seqid;
}
public String getSeqid() {
return this.seqid;
}
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 class TableStrategy implements DistributeTableStrategy<LoginRecord> {
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, LoginRecord bean) {
return getTable(table, 0, bean.getCreatetime());
}
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));
}
}
}

View File

@@ -0,0 +1,113 @@
/*
* 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.convert.*;
import org.redkale.source.*;
/**
*
* @author zhangjx
*/
@DistributeTable(strategy = UserDetail.TableStrategy.class)
public class UserDetail extends BaseEntity {
@Id
private long userid; //用户ID
@Column(length = 64, comment = "用户昵称")
private String username = ""; //用户昵称
@Column(length = 32, comment = "手机号码")
private String mobile = ""; //手机号码
@Column(length = 64, comment = "密码")
@ConvertColumn(ignore = true, type = ConvertType.ALL)
private String password = ""; //密码
@Column(length = 128, comment = "备注")
private String remark = ""; //备注
@Column(updatable = false, comment = "创建时间")
private long createtime; //创建时间
/** 以下省略getter setter方法 */
public long getUserid() {
return userid;
}
public void setUserid(long userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public long getCreatetime() {
return createtime;
}
public void setCreatetime(long createtime) {
this.createtime = createtime;
}
public static class TableStrategy implements DistributeTableStrategy<UserDetail> {
@Override
public String getTable(String table, UserDetail bean) {
return getTable(table, bean.getUserid());
}
@Override
public String getTable(String table, FilterNode node) {
Serializable id = node.findValue("userid");
if (id != null) return getTable(table, id);
return getHashTable(table, (Integer) node.findValue("#hash"));
}
@Override
public String getTable(String table, Serializable userid) {
return getHashTable(table, (int) (((Long) userid) % 100));
}
private String getHashTable(String table, int hash) {
int pos = table.indexOf('.');
return "platf_user." + table.substring(pos + 1) + "_" + (hash > 9 ? hash : ("0" + hash));
}
}
}