Premise: Recently wrote Android project, Android comes with database API a bit complex, so lazy use Greendao. Greendao Advantages of self-check, here do not repeat, but 0 basis of the I use up or 3 days of Kung Fu, take it to the network, special in this dedication and network. 1: Generate the required key files through Daogenerator
Step 1, the new Java project, named Daogenerator
Step 2: Import two jar packages, respectively Freemarker-2.3.20.jar and Greendao-generator-1.3.1.jar, download path:/http search.maven.org, you can search for these two packages here and import them into the Java project yourself.
When importing, remember not to have Chinese path
Step 3: Create a new Daogenerator.java file with the following file format
ImportDe.greenrobot.daogenerator.DaoGenerator;Importde.greenrobot.daogenerator.Entity;ImportDe.greenrobot.daogenerator.Property;ImportDe.greenrobot.daogenerator.Schema;ImportDe.greenrobot.daogenerator.ToMany; Public classDaogenerator { Public Static voidMain (string[] args)throwsException {Schema schema=NewSchema (1, "Com.example.menu"); Addmenudetail (schema); Addmenutable (schema); NewDaogenerator (). Generateall (Schema, "./"); } Private Static voidaddmenutable (schema Schema) {Entity menutable= Schema.addentity ("menutable"); Menutable.addstringproperty ("MenuName");//the name of the menu (Monday Breakfast General edition)Menutable.addintproperty ("Menutype");//0 Breakfast 1 Lunch 2 dinnerMenutable.addlongproperty ("MenuId"). PrimaryKey (). AutoIncrement ();//Menu IDMenutable.addstringproperty ("Menucreatetime");//Menu creation TimeMenutable.addlongproperty ("StoreId");//the number of the store } Private Static voidAddmenudetail (schema Schema) {Entity menudetail= Schema.addentity ("Menudetail"); Menudetail.addstringproperty ("Menudateid"); Menudetail.addlongproperty ("MenuId");//Menu IDMenudetail.addintproperty ("Widgetid");//Control IDMenudetail.addstringproperty ("type");//control type: TextView, pic, line, Verticalline, theme, tipsMenudetail.addstringproperty ("name");//control content "Lump soup"Menudetail.adddoubleproperty ("X");//xMenudetail.adddoubleproperty ("Y");//y } }
The project will be finished running in the current directory generated a folder COM, into the example and then into the menu (why would produce such a folder, which is by our program in the schema schema = new schema (1, "Com.example.menu") , New Daogenerator (). Generateall (Schema, "./"); These two lines decide, if you run an error, check if you are not a path to write wrong, you can find the following several files are generated:
Brief description: Menudetail and menutable is the table we specify in the Daogenerator, each table in the generated file corresponds to two files, for example: Table menutable corresponding two files one is menutable, One is Menutabledao.
Menutable.java file is primarily a method of getting and setting elements in a column in Menudetail, see, note: This is the automatically generated code, if you want to modify the element name in the column can be modified in Daogenerator :
1 Packagecom. ShanFuBao.SmartCall.menu;2 3 //This CODE was GENERATED by Greendao, does not EDIT. Enable "Keep" sections if you want to edit. 4 /**5 * Entity mapped to table menu_table.6 */7 Public classmenutable {8 9 PrivateString MenuName;Ten PrivateInteger Menutype; One PrivateLong menuId; A PrivateString menucreatetime; - PrivateLong storeId; - the Publicmenutable () { - } - - Publicmenutable (Long menuId) { + This. MenuId =menuId; - } + A Publicmenutable (String menuName, Integer menutype, Long menuId, String menucreatetime, long storeId) { at This. MenuName =MenuName; - This. Menutype =Menutype; - This. MenuId =menuId; - This. Menucreatetime =Menucreatetime; - This. StoreId =storeId; - } in - PublicString Getmenuname () { to returnMenuName; + } - the Public voidsetmenuname (String menuName) { * This. MenuName =MenuName; $ }Panax Notoginseng - PublicInteger Getmenutype () { the returnMenutype; + } A the Public voidSetmenutype (Integer menutype) { + This. Menutype =Menutype; - } $ $ PublicLong Getmenuid () { - returnmenuId; - } the - Public voidSetmenuid (Long menuId) {Wuyi This. MenuId =menuId; the } - Wu PublicString Getmenucreatetime () { - returnMenucreatetime; About } $ - Public voidsetmenucreatetime (String menucreatetime) { - This. Menucreatetime =Menucreatetime; - } A + PublicLong Getstoreid () { the returnstoreId; - } $ the Public voidSetstoreid (Long storeId) { the This. StoreId =storeId; the } the -}View Code
Android Orm's Greendao