Weed3 一个微型ORM框架(只有0.1Mb哦)
源码:https://github.com/noear/weed3
源码:https://gitee.com/noear/weed3
查询查然是个麻烦的话题。。。
还好这篇条件会比较简单
- 单表条件查询(有了简单的自然能拼成复杂的)
//weed3 的条件构建,是相当自由的
String mobile = "111";
db.table("test")
.where("mobile=?",mobile).and().begin("sex=?",1).or("sex=2").end()
.limit(20)
.select("*").getMapList()
db.table("test")
.where("mobile=?",mobile).and("(sex=? OR sex=2)",1)
.limit(20)
.select("*").getMapList()
db.table("test").where("mible=? AND (sex=1 OR sex=2)",mobile)
.limit(20)
.select("*")
//以上三种,效果是一样的。。。因为很自由,所以很容易使用(也有观点认为:所以很难控制)
- 有时候一些条件需要动态控制
//这个示例,管理后台很常见
int type=ctx.paramAsInt("type",0);
String key=ctx.param("key");
int date_start=ctx.paramAsInt("date_start",0);
int date_end=ctx.paramAsInt("date_end",0);
DbTableQuery qr = db.table("test").where("1=1");
if(type > 0){
qr.and("type=?", type);
}
if(key != null){
qr.and('"title LIKE ?",key+"%");
}
if(date_start>0 && date_end >0){
qr.and("( date >=? AND date<=? )", date_start, date_end);
}
qr.select("id,title,icon,slug").getMapList();
- 多表关联查询:innerJoin(..), leftJoin(..), rightJoin(..)
//innerJoin()
db.table("user u")
.innerJoin("user_book b").on("u.id = b.user_id")
.select("u.name,b.*")
//leftJoin()
db.table("user u")
.leftJoin("user_book b").on("u.id = b.user_id").and("u.type=1")
.select("u.name,b.*")
//rightJoin()
db.table("user u")
.rightJoin("user_book b").on("u.id = b.user_id")
.where("b.type=1").and("b.price>",12)
.select("u.name,b.*")
- 想别的关联查询怎么样?(如:full join)
//因为不是所有的数据库都支持 full join,所以...
db.table("user u")
.append("FULL JOIN user_book b").on("u.id = b.user_id")
.select("u.name,b.*")
//.append(..) 可以添加任何内容的接口