android Xutils dbutils 註解

來源:互聯網
上載者:User

標籤:

xUtils DbUtils 關於實體類註解 匯總

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;

 

android Xutils dbutils 註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.