Description
/** * Ydl_hibernate Summary <br/> * (i) Support features: 1. Self-built tables, support attributes from inherited classes: You can build the table by the annotations themselves, and the annotation fields in the inheriting class support your own initiative to build the table. 2. Self-active support Add and remove change *, to support the object-based operation: Adding and deleting is the most basic unit of database operations, do not have to write these additions and deletions of the code, and add and update 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 results can be self-packaged as entity objects, similar to the Hibernate framework. * 5. Flexible query results: The query results support the object, and the result is list<map<string,string>> form, this method is very useful in the actual project, and more efficient. * 6. The log is more specific: since Android development does not support hot deployment debugging, errors can be located according to the log when running an error, 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, and does not recommend 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 my blog: http://blog.csdn.net/linglongxin24, * Here is the framework of the use of examples and source code, I hope that friends to communicate well this framework, and jointly promote the development of China's open source business, 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 friendsFriends Note Oh, here can also be defined as interface Oh, see Studentdaoimpl.java in the gaze. Teacherdaoimpl Teacherdao = new Teacherdaoimpl (mainactivity.this); Studentdaoimpl Studentdao = new Studentdaoimpl (mainactivity.this);//join 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 single object 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//run results such as the following://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 condition Query and query result//Run result: 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);} Method 4: Use SQL to query the results, this way is the most flexible 2,3,4.//Run 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 (" lis T3: "+ student);} Way 4 Advanced: Suppose to query out the teacher's students, can be achieved://Running 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 is more efficient to look at only 2 words than to query all the fields and encapsulate them as objects, especially when the field values are very long our phone prefers 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: LISTMAP1) {///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");} Mode 5 Advanced: I would like to know the first 2 students name and the name of the class teacher, such a way is not super flexible Ah, in other ways to query the way to use it, haha.//Result://listmap2:student_name:lk;teacher_name: Miss MI 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 at the same time delete Oh. Studentdao.delete (Studentid1.intvalue ()); Studentdao.delete (new integer[] {studentid2.intvalue (), Studentid3.intvalue ()});//support for running SQL statements Oh. Teacherdao.execsql (" Insert into T_teacher (name,age) VALUES (' Professor ', ') ', null);}}
Demo and source code download
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android Database Hibernate framework