超强跨平台轻量级ORM(无反射;缓存控制;分布式事务;万能绑定)
支持什么数据库???
与具体数据库无关(或许支持所有数据库)
特点:
1.零反射零注解
2.漂亮的缓存控制
3.纯代码无任何配置
4.分布式事务集成
5.万能的数据绑定
占位符说明:
$. //数据库名占位数
$fun //SQL函数占位符
? //参数占位符
?... //数组型参数占位符
QQ群:
22200020
--------------------------------------
示例1.1.1::入门级
//DbContext db = new DbContext("user","proxool.xxx_db");
DbContext db = new DbContext("user","jdbc:mysql://x.x.x:3306/user","root","1234",null);
//简易.查询示例
db.table("user_info")
.where("user_id<?", 10)
.select("user_id,name,sex")
.getDataList(); //.getList(new UserInfoModel());
//简易.插入示例
db.table("$.test")
.set("log_time", "$DATE(NOW())")
.insert();
//简易.批量插入示例
db.table("test")
.insertList(list,(d,m)->{
m.set("log_time", "$DATE(NOW())");
m.set("name",d.name);
});
//简易.更新示例
db.table("test")
.set("txt", "NOW()xx")
.set("num", 44)
.where("id IN (?...)", new int[] { 15,14,16}) //数据参数
.update();
//简易.存储过程调用示例,及使用使用示例
db.call("user_get")
.set("xxx", 1) //保持与存储过程参数的序顺一致
.getItem(new UserInfoModel());
//简易.查询过程调用示例,及使用使用示例
db.call("select * from user where user_id=@userID")
.set("@userID", 1)
.caching(cache)//使用缓存
.usingCache(60 * 100) //缓存时间
.getItem(new UserInfoModel());
//简易.存储过程调用示例,及使用事务示例
db.tran(tran->{
db.call("$.user_set").set("xxx", 1)
.tran(tran) //使用事务
.execute();
});
示例1.1.2::不确定因素的连式处理支持
//连式处理::对不确定的条件拼装
db.table("test")
.expre(tb -> {
tb.where("1=1");
if (1 == 2) {
tb.and("mobile=?", "xxxx");
} else {
tb.and("icon=?", "xxxx");
}
}).select("*");
//连式处理::对不确定字段的插入
db.table("test")
.expre(tb -> {
tb.set("name", "xxx");
if (1 == 2) {
tb.set("mobile", "xxxx");
} else {
tb.set("icon", "xxxx");
}
}).insert();
示例1.2::事务控制
//demo1:: //事务组
db.tran((t) => {
//以下操作在同一个事务里执行
t.db().sql("insert into test(txt) values(?)", "cc").tran(t).execute();
t.db().sql("insert into test(txt) values(?)", "dd").tran(t).execute();
t.db().sql("insert into test(txt) values(?)", "ee").tran(t).execute();
t.db().sql("update test set txt='1' where id=1").tran(t).execute();
});
//demo2:: //事务队列
DbTranQueue queue = new DbTranQueue();
db.tran().join(queue).execute((t) => {
db.sql("insert into test(txt) values(?)", "cc").tran(t).execute();
db.sql("insert into test(txt) values(?)", "dd").tran(t).execute();
db.sql("insert into test(txt) values(?)", "ee").tran(t).execute();
});
db2.tran().join(queue).execute((t) => {
db2.sql("insert into test(txt) values(?)", "gg").tran(t).execute();
});
queue.complete();
示例1.3::缓存控制
//最简单的缓存控制
db.call("user_get").set("xxx", 1)
.caching(cache)
.usingCache(60 * 1000)
.getItem(new UserInfoModel());
//根据查询结果控制缓存
db.call("user_get").set("xxx",1)
.caching(cache)
.usingCache(60 * 100)
.getItem(new UserInfoModel(), (cu, t) => {
if (t.user_id == 0)
cu.usingCache(false);
});
//通过tag维护缓存
//1.缓存并添加简易标签
db.call("user_get").set("xxx", 1)
.caching(cache)
.cacheTag("user_"+ 1)
.usingCache(60 * 1000)
.getItem(new UserInfoModel());
CacheTags tags = new CacheTags(cache);
//2.1.可根据标签清除缓存
tags.clear("user_" + 1);
//2.2.可根据标签更新缓存
tags.update<UserInfoModel>("user_" + 1, (m)=>{
m.name = "xxx";
return m;
});
示例2::数据模型类(或叫实体类等)
public class UserInfoModel implements IBinder {
public long user_id;
public int role;
public String mobile;
public String udid;
public int city_id;
public String name;
public String icon;
public void bind(GetHandlerEx s) {
user_id = s.get("user_id").value(0l);
role = s.get("role").value(0);
mobile = s.get("mobile").value("");
udid = s.get("udid").value("");
city_id = s.get("city_id").value(0);
name = s.get("name").value("");
icon = s.get("icon").value("");
}
public IBinder clone() {
return new UserInfoModel();
}
}
示例3.1::[存储过程]映射类
public class user_get_by_id extends DbStoredProcedure
{
public user_get_by_id()
{
super(Config.user);
call("user_get_by_id");
//set("{colname}", ()->{popname});
//
set("_user_id", ()->user_id);
}
public long user_id;
}
//使用示例
user_get_by_id sp = new user_get_by_id();
sp.user_id = 1;
sp.caching(cache)
.getItem(new UserInfoModel());
示例3.2::[查询过程]映身类
public class user_get_by_id extends DbQueryProcedure
{
public user_get_by_id()
{
super(Config.user);
sql("SELECT * FROM `user` where user_id = @user_id;");
//set("{colname}", ()->{popname});
//
set("@user_id", ()->user_id);
}
public long user_id;
}
//使用示例
user_get_by_id sp = new user_get_by_id();
sp.user_id = 1;
sp.caching(cache)
.getItem(new UserInfoModel());
示例3.3::[数据表]映射类
public class UserM extends DbTable {
public UserM() {
super(DbConfig.test);
table("users u");
set("UserID", () -> UserID);
set("Nickname", () -> Nickname);
set("Sex", () -> Sex);
set("Icon", () -> Icon);
set("City", () -> City);
}
public Long UserID;
public String Nickname;
public Integer Sex;
public String Icon;
public String City;
}
//使用示例1
UserM m = new UserM();
m.UserID=1;
m.Icon="http:///xxxx";
m.insert();
//使用示例2
UserM m = new UserM();
m.icon="http:///xxxx";
m.where("UserID=?",1).update();
//使用示例3
UserM m = new UserM();
m.where("sex=?",1)
.select("*")
.getList(new UserInfoModel());
更多高级示例请参考Weed3Demo
--------------------------------------
by noear