Usage
/** * Ydl_hibernate Overview <br/> * (i) Support features: 1. Auto-build table, support attributes from inheriting classes: The table can be built automatically based on annotations, and automatic tables are supported for annotation fields in inheriting classes. 2. Automatic support Add and remove change *, increase support for object-based operation: Adding and deleting is the most basic unit of database operation, do not repeat these additions and deletions of code, and added and updated to support similar to hibernate in the object-based operations. * 3. Flexible query: Support the way the Android framework provides, and also support native SQL mode. * 4. Query result object: The query result can be automatically wrapped as a solid object, similar to the Hibernate framework. * 5. Flexible query Results: Query results support the object, also support the result as list<map<string,string>> form, this method is practical in the actual project, and more efficient. * 6. Log more detailed: Because Android development does not support hot deployment debugging, running an error can be based on the log to locate errors, which can reduce the number of times to run Android. <br/> * (ii) deficiencies: <br/> * 1.id temporarily supports only int type, does not support UUID, it is not recommended to use UUID in SQLite. * 2. Now each method opens and closes the transaction itself, and temporarily does not support doing multiple operations in one transaction and then committing the transaction uniformly. <br/> * (c) Author's message:<br/> * In the past, JavaScript borrowed Java development, today also hope Ydl_hibernate borrow Hibernate name development. * Hopefully this project will become an important part of the open source community, and hopefully this project will bring convenience to all Android developers. * Welcome to visit my blog: http://blog.csdn.net/linglongxin24, * Here are the use of this framework and source code, I hope that friends to improve the framework, and jointly promote the development of China's open source, Ydl_ Hibernate looks forward to working with you to create a better future!!! */public class Mainactivity extends Activity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main);//familiar with the interface of friends notice Oh, here can also be defined as an interfaceOh, see the note in Studentdaoimpl.java. Teacherdaoimpl Teacherdao = new Teacherdaoimpl (mainactivity.this); Studentdaoimpl Studentdao = new Studentdaoimpl (mainactivity.this);//Add teacher teacher = new Teacher (); Teacher.setname ( "Miss Mi"); Teacher.setage ("Professor"); Teacher.settitle ("Prof."); Long Teacherid = Teacherdao.insert (teacher); Student student1 = new Student (), Student1.setname ("LK"), Student1.setage (+), student1.setclasses ("V"); Student1.setteacherid (Teacherid.intvalue ()); Long studentId1 = Studentdao.insert (student1); Student Student2 = new Student (), Student2.setname ("CLS"), Student2.setage (+), student2.setclasses ("V"); Student2.setteacherid (Teacherid.intvalue ()); Long studentId2 = Studentdao.insert (Student2); Student Student3 = new Student (), Student3.setname ("lb"); Student3.setage (student3.setclasses); ("Phase Five"); Student3.setteacherid (Teacherid.intvalue ()); Long studentId3 = Studentdao.insert (STUDENT3);//query//Mode 1: Query individual objects by ID//Result: Student1student [id=1, Name=lk,age=26, Teacherid=1, classes= five]student student4 = STUDENTDAO.GET (Studentid1.intvalue ()); System.out.println ("student4" + student4);//Mode 2: Query out all records in the table//execution results are as follows://List1:student [Id=1, Name=lk,age=26,teacherid =1, classes= five]//list1:student [id=2, Name=cls,age=26,teacherid=1, classes= five]//list1:student [id=3, name=lb,age=27, Teacherid=1, classes= five period]list<student> List1 = Studentdao.find (); for (Student student:list1) {System.out.println ("List1:" + student);} Mode 3: Limit the condition query and query results//Execution results: list2:student [id=2, name=cls,age=0,teacherid=0, classes=null]list<student> list2 = Studentdao.find (new string[] {"id", "name"}, "id =?" ", new string[] {studentid2.tostring ()}, NULL, null,null, NULL); for (Student student:list2) {System.out.println (" List2 : "+ student);} Mode 4: Use SQL to query the results, this way is the most flexible 2,3,4.//Execution Result://List3:student [id=2, Name=cls,age=26,teacherid=1, classes= five]//list3: Student [Id=3, name=lb,age=27,teacherid=1, classes= five period]list<student> list3 = Studentdao.rawquery ("SELECT * from T_ Student where ID in (?,?) ", new string[] {STUDENTID2.TOSTRING (), studentid3.tostring ()}), for (Student student:list3) {System.out.println ("List3:" + Student);} Way 4 Advanced: If you want to query out the teacher's students, you can do this://Execution results://List4:student [Id=1, Name=lk,age=26,teacherid=1, classes= five]//list4:student [ id=2, Name=cls,age=26,teacherid=1, classes= five]//list4:student [id=3, name=lb,age=27,teacherid=1, classes= phase five]list< student> list4 = Studentdao.rawquery ("Select s.* from t_student s joins T_teacher t on s.teacher_id = t.id where t.name= ? ", new string[] {" M teacher "}); for (Student student:list4) {System.out.println (" List4: "+ Student);} Mode 5: I just want to know the name and age, the query gets list<map<string,string>> form. It's more efficient to look at only 2 words than to query all the fields and encapsulate them as objects, especially if the field values are many, our phones prefer this way.//Results ://listmap1:name:lk;age:26//listmap1:name:cls;age:26//listmap1:name:lb;age:27list<map<string, String> > listMap1 = Studentdao.query2maplist ("Select Name,age from T_student", null); for (map<string, string> Map:lis TMAP1) {///query the map in the list to query the SQL for attribute values in lowercase form key, note that it is in lowercase form oh. System.out.println ("Listmap1:name:" +Map.get ("name") + "; Age:" + map.get ("Age");} Way 5 Advanced: I want to know the first 2 students name and the name of the class teacher, this way is not super flexible Ah, in other ways to query the use of this way, haha.//Result://Listmap2:student_name:lk;teacher_name: M// Listmap2:student_name:cls;teacher_name: Rice teacher list<map<string, string>> listMap2 = Studentdao.query2maplist ("Select S.name sname,t.name tname from t_student s joins T_teacher t on s.teacher_id = t.id limit ? ", new string[] {" 2 "}); for (map<string, string> map:listmap2) {System.out.println (" Listmap2:student_name: "+ Map . Get ("sname") + "; Teacher_name:" + map.get ("Tname"));} Update//results: Student [id=1, Name= kun, age=26,teacherid=1, classes= five period]student1 = Studentdao.get (Studentid1.intvalue ()); Student1.setname ("Kun"); Student1.setclasses ("Phase Five"); Studentdao.update (STUDENT3); System.out.println (student1);//delete: Support Single ID Delete, also support multiple IDs simultaneously delete Oh. Studentdao.delete (Studentid1.intvalue ()); Studentdao.delete (new integer[] {studentid2.intvalue (), Studentid3.intvalue ()});//Support Execute SQL statement OH. Teacherdao.execsql (" Insert into T_teacher (name,age) VALUES (' Professor Rice ',", null);}} Demo and source download
Android Database Hibernate framework