Greendao:
Generates a data access without the code we write to access the database:
CORE schematic diagram
Generate code
is to generate a build factory of the corresponding Java class with the generator.
Public static void main(string[] args)
{
//ID name age sex
//Schema S=new schema (version number, code package name);
Schema s =NewSchema (1,"Com.itheima.redbaby.dao");
//Description Object
Entity person = s.addentity ("Person");
Person.addidproperty (). AutoIncrement (). PrimaryKey ();//ID
Person.addstringproperty ("Name");
Person.addintproperty ("Age");
Person.addstringproperty ("Sex");
//Generate code
//New Daogenerator (). Generateall (S, input path);
Try{//: Working space
NewDaogenerator (). Generateall (s),".. /greendao_redbaby/src/");
}Catch(IOException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}Catch(Exception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
Code generation in the specified path is as follows
Test code
@Test
Public void testLoad() {
Daosession sesion = getsession ();
Persondao person = Sesion.getpersondao ();
Person P=person.load (1L);
LOG.I ("WZX", P.getname ());
}
@Test
Public void testloadall() {
Daosession sesion = getsession ();
Persondao person = Sesion.getpersondao ();
list<person> list = Person.loadall ();
for(Person P:list) {
LOG.I ("WZX", P.getname ());
}
}
@Test
Public void testinsert() {
Daosession sesion = getsession ();
//Get specific one DAO Perpsondao
Persondao DAO = Sesion.getpersondao ();
Person p =NewPerson ();
P.setage (1);
P.setname ("The song of Zhang San.");
P.setsex ("Man");
Dao.insert (P);
}
Public daosession getsession() {
//Description Data shopping.db
//Sqliteopenhelper
//|--devopenhelper
Sqlitedatabase db =NewDaomaster.devopenhelper (Mcontext,"Shopping.db",NULL). Getwritabledatabase ();
Daomaster DM =NewDaomaster (DB);
//Get collection of DAO daosession Persondao
Daosession sesion = Dm.newsession ();
returnsesion;
}
@Test
Public void testdeletebykey() {
Daosession sesion = getsession ();
Persondao Persondao = Sesion.getpersondao ();
Persondao.deletebykey (1L);
}
@Test
Public void testupdate() {
Daosession sesion = getsession ();
Persondao Persondao = Sesion.getpersondao ();
Person P=persondao.load (1L);
P.setage ( A);
P.setsex ("Woman");
Persondao.update (P);
}
Summary :
The overall simple use of our database, so we are too much to write SQL code;
Just need us to put each row of the database. It is possible to have a javabean field.
Simple and rude;
SqLite Frame Greendao