RockyZhang 发布于 1年前,共有 0 条评论
先来官方demo
DbUtils db = DbUtils.create(this); User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性 user.setEmail("[email protected]"); user.setName("wyouflf"); db.save(user); // 使用saveBindingId保存实体时会为实体的id赋值 ... // 查找 Parent entity = db.findById(Parent.class, parent.getId()); List<Parent> list = db.findAll(Parent.class);//通过类型查找 Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=","test")); // IS NULL Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=", null)); // IS NOT NULL Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","!=", null)); // WHERE id<54 AND (age>20 OR age<30) ORDER BY id LIMIT pageSize OFFSET pageOffset List<Parent> list = db.findAll(Selector.from(Parent.class) .where("id" ,"<", 54) .and(WhereBuilder.b("age", ">", 20).or("age", " < ", 30)) .orderBy("id") .limit(pageSize) .offset(pageSize * pageIndex)); // op为"in"时,最后一个参数必须是数组或Iterable的实现类(例如List等) Parent test = db.findFirst(Selector.from(Parent.class).where("id", "in", new int[]{1, 2, 3})); // op为"between"时,最后一个参数必须是数组或Iterable的实现类(例如List等) Parent test = db.findFirst(Selector.from(Parent.class).where("id", "between", new String[]{"1", "5"})); DbModel dbModel = db.findDbModelAll(Selector.from(Parent.class).select("name"));//select("name")只取出name列 List<DbModel> dbModels = db.findDbModelAll(Selector.from(Parent.class).groupBy("name").select("name", "count(name)")); ... List<DbModel> dbModels = db.findDbModelAll(sql); // 自定义sql查询 db.execNonQuery(sql) // 执行自定义sql ...
注解总结
1 .主键
@Id // 如果主键没有命名名为id或_id的时,需要为主键添加此注解 @NoAutoIncrement // int,long类型的id默认自增,不想使用自增时添加此注解 private int id;
2. 忽略字段
// Transient使这个列被忽略,不存入数据库 @Transient public String willIgnore; /** ---------------------------------*/ public static String staticFieldWillIgnore; // 静态字段也不会存入数据库
3.表名
@Table(name = "parent", execAfterTableCreated = "CREATE UNIQUE INDEX index_name ON parent(name,email)") //name即表名, //execAfterTableCreated 自定义表创建之后要执行的sql。为parent表创建(name,email)索引 -->在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。
4.列名
@Column(column = "name") //为列名加上注解 可以针对命名不统一和防止混淆 public String name;
5.外键
延迟加载
@Finder(valueColumn = "id", targetColumn = "parentId") public FinderLazyLoader<Child> children; // 关联对象多时建议使用这种方式,延迟加载效率较高。 @Foreign(column = "parentId", foreign = "id") public ForeignLazyLoader<Parent> parent;
非延迟加载
@Finder(valueColumn = "id",targetColumn = "parentId") public Child children; @Foreign(column = "parentId", foreign = "isVIP") public List<Parent> parent;
1对多
@Finder(valueColumn = "id", targetColumn = "parentId") private List<Child> children; @Foreign(column = "parentId", foreign = "id") public Parent parent;
代码
Parent parent = new Parent(); parent.name = "测试" + System.currentTimeMillis(); parent.setAdmin(true); parent.setEmail("[email protected]"); DbUtils db = DbUtils.create(this.getActivity()); db.configAllowTransaction(true); db.configDebug(true); Child child = new Child(); child.name = "child' name"; child.parent = parent; db.saveBindingId(child);//保存对象关联数据库生成的id
6.其他
@NotNull //不能为空 @Check(value = "age>0") //age必须大于0 创建表时调用" @Unique //唯一 public int age;