Xutils Dbutils about entity class annotations Summary
Rockyzhang posted 1 year ago, total 0 reviews
First, the official demo.
Dbutils db = dbutils.create (This); user user = new User (); //It is important to note that the user object must have an id attribute, or a property user.setemail through @id annotations ("[email protected ] "); user.setname (" wyouflf "); db.save (user); // When you save an entity by using SAVEBINDINGID, the ID of the entity is assigned ... // found parent entity = db.findbyid (Parent.class, parent.getid ()); List<parent> list = db.findall (Parent.class);//Search by Type 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" &NBSP;, "<", 54) .and (Wherebuilder.b ("Age", ">", 20). or ("Age", " < ", 30)) .orderby ("id") .limit (pageSize) .offset (Pagesize * pageindex)); When // op is "in", the last parameter must be an array or Iterable implementation class (such as list, etc.) Parent test = Db.findfirst (Selector.from (Parent.class). WHERE ("id", "in", new int[]{1, 2, 3})); // When OP is "between", the last parameter must be an array or Iterable implementation class (such as list, etc.) 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") only takes out the name column List<DbModel> dbModels = Db.finddbmodelall (Selector.from (Parent.class). GroupBy ("name"). Select ("Name", "Count (name)"); ... list<dbmodel> dbmodels = db.finddbmodelall (SQL); // Custom SQL query db.execnonquery (SQL) // perform custom sql &NBSP, .....
Summary of annotations
1. Primary key
@Id//If the primary key does not have a name named Id or _id, you need to add this note to the main key @NoAutoIncrement//Int,long type ID default self-increment, do not want to add this annotation with self-increment when the private int Id;
2. Ignore fields
Transient causes this column to be ignored, not deposited into the database @Transient public String Willignore; /**---------------------------------*/public static String Staticfieldwillignore; Static fields are not stored in the database
3. Table name
@Table (name = "Parent", execaftertablecreated = "CREATE UNIQUE INDEX index_name on parent (name,email)")//name is the table name,//execa ftertablecreated the SQL to execute after the custom table is created. Create a (name,email) index for the parent table and create a unique index on the table. A unique index means that two rows cannot have the same index value.
4. Column names
The @Column (column = "name")//for the column name plus annotations can be used for naming disunity and to prevent confusion of public String name;
5. Foreign key
Lazy Loading
@Finder (ValueColumn = "id", Targetcolumn = "ParentID") public finderlazyloader<child> children; It is recommended to use this method for multiple associated objects, which is more efficient for lazy loading. @Foreign (column = "ParentID", Foreign = "id") public foreignlazyloader<parent> Parent;
Non-lazy loading
@Finder (ValueColumn = "id", Targetcolumn = "parentid") public child children; @Foreign (column = "ParentID", Foreign = "ISVIP") public list<parent> Parent;
1-to-many
@Finder (ValueColumn = "id", Targetcolumn = "ParentID") private list<child> children; @Foreign (column = "ParentID", Foreign = "id") public parent parent;
Code
parent parent = new parent (); parent.name = "Test" + 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);//Save object associated database generated ID
6. Other
@NotNull//cannot be empty @Check (value = "age>0")//age must be greater than 0 when the table is created called "@Unique//Unique public int of age;
Android Xutils dbutils annotations