Android database hibernate framework

Source: Internet
Author: User

Android database hibernate framework
Usage

/*** YDL_Hibernate overview * (1) supported functions: 1. automatic table creation. attributes can be derived from inheritance classes: tables can be created automatically based on annotations, and tables can be created automatically for annotation fields in inheritance classes. 2. automatically supports addition, deletion, modification, and Object-based operations. addition, deletion, and modification are the most basic unit of database operations. You do not need to repeatedly write the code for addition, deletion, and modification, added and updated support for Object-like operations in hibernate. * 3. flexible query methods: supports the android framework and native SQL methods. * 4. query results: the query results can be automatically packaged as object objects, similar to the hibernate framework. * 5. flexible query results: query results support objectization and List
 
  
> Form: This method is useful in actual projects and more efficient. * 6. log details: android development does not support hot deployment debugging. When an error is reported, you can locate the Error Based on the log, which can reduce the number of Android operations. * (2) Disadvantages: * 1. currently, IDS only support int type, but do not support uuid. uuid is not recommended in sqlite. * 2. now, each method starts and closes the transaction by itself. At present, it does not support multiple operations in a transaction and then committing the transaction in a unified manner. * (3) the author sent a message: * in the past, JavaScript was developed by using Java. Today, YDL_Hibernate is expected to be developed by Hibernate. * We hope this project will become an important member of the open-source community in the future. We also hope that this project will facilitate all Android Developers. * Welcome to my blog: http://blog.csdn.net/linglongxin24, * here are examples of the use of this framework and source code, I hope my friends can improve this framework, and jointly promote open source in China YDL_Hibernate is looking forward to a better future with you !!! */Public class MainActivity extends Activity {@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // notes for friends who are familiar with Interfaces. This can also be defined as interfaces. For details, see StudentDaoImpl. java annotations. teacherDaoImpl teacherDao = new TeacherDaoImpl (MainActivity. this); StudentDaoImpl studentDao = new StudentDaoImpl (MainActivity. this); // Add Teacher teacher = new Teacher (); teacher. setName ("Miss meters"); teacher. setAge (50); teacher. setTitle ("professor"); Long teacherId = teacherDao. insert (teacher); Student student1 = new Student (); student1.setName ("lk"); student1.setAge (26); student1.setClasses ("5"); student1.setTeacherId. intValue (); Long studentId1 = studentDao. insert (student1); Student student2 = new Student (); student2.setName ("cls"); student2.setAge (26); student2.setClasses ("5"); role (teacherId. intValue (); Long studentId2 = studentDao. insert (student2); Student student3 = new Student (); student3.setName ("lb"); student3.setAge (27); student3.setClasses ("Phase 5"); terminate (teacherId. intValue (); Long studentId3 = studentDao. insert (student3); // query // Method 1: query a single object by Id // result: student1Student [id = 1, name = lk, age = 26, teacherId = 1, classes = 5] Student student4 = studentDao. get (studentId1.intValue (); System. out. println ("student4" + student4); // Method 2: Query all records in the Table. // The execution result is as follows: // list1: Student [id = 1, name = lk, age = 26, teacherId = 1, classes = 5] // list1: Student [id = 2, name = cls, age = 26, teacherId = 1, classes = 5] // list1: Student [id = 3, name = lb, age = 27, teacherId = 1, classes = five] List
  
   
List1 = studentDao. find (); for (Student student: list1) {System. out. println ("list1:" + student);} // method 3: Restricted Condition query and query results // execution result: list2: Student [id = 2, name = cls, age = 0, teacherId = 0, classes = null] List
   
    
List2 = studentDao. find (new String [] {"id", "name"}, "id =? ", New String [] {studentId2.toString ()}, null); for (Student student: list2) {System. out. println ("list2:" + student);} // Method 4: use SQL to query the result. This method is the most flexible among 2, 3, and 4. // execution result: // list3: Student [id = 2, name = cls, age = 26, teacherId = 1, classes = 5] // list3: student [id = 3, name = lb, age = 27, teacherId = 1, classes = Phase 5] List
    
     
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);} // Method 4 (advanced tutorial): If you want to query the students of the MI instructor, you can achieve the following: // execution result: // list4: student [id = 1, name = lk, age = 26, teacherId = 1, classes = 5] // list4: Student [id = 2, name = cls, age = 26, teacherId = 1, classes = 5] // list4: Student [id = 3, name = lb, age = 27, teacherId = 1, classes = five] List
     
      
List4 = studentDao. rawQuery ("select s. * from t_student s join t_teacher t on s. teacher_id = t. id where t. name =? ", New String [] {" mi "}); for (Student student: list4) {System. out. println ("list4:" + student);} // Method 5: I only want to know the name and age.
      
        > Format. querying only two words is more efficient than querying all fields and encapsulating them as objects. Our mobile phone prefers this method when there are many field values. // result: // listMap1: name: lk; age: 26 // listMap1: name: cls; age: 26 // listMap1: name: lb; age: 27 List
       
         > ListMap1 = studentDao. query2MapList ("select name, Age from t_student", null); for (Map
        
          Map: listMap1) {// the map in the List to be queried uses the lower-case form of the attribute value in the query SQL statement as the key. Note that it is in the lower-case form. system. out. println ("listMap1: name:" + map. get ("name") + "; age:" + map. get ("age");} // Method 5 (advanced tutorial): I want to know the names of the first two students and the names of the class teachers. Is this method extremely flexible, it is not easy to use this method in other ways, haha. // result: // listMap2: student_name: lk; teacher_name: Mi instructor // listMap2: student_name: cls; teacher_name: Mi instructor List
         
           > ListMap2 = studentDao. query2MapList ("select s. name sname, t. name tname from t_student s join t_teacher t on s. teacher_id = t. id limit? ", New String [] {" 2 "}); for (Map
          
            Map: listMap2) {System. out. println ("listMap2: student_name:" + map. get ("sname") + "; teacher_name:" + map. get ("tname");} // update // result: Student [id = 1, name = Li Kun, age = 26, teacherId = 1, classes = Phase 5] student1 = studentDao. get (studentId1.intValue (); student1.setName ("Li Kun"); student1.setClasses ("Phase 5"); studentDao. update (student3); System. out. println (student1); // Delete: You can delete a single id or multiple IDs at the same time. studentDao. delete (studentId1.intValue (); studentDao. delete (new Integer [] {studentId2.intValue (), studentId3.intValue ()}); // supports execution of SQL sentence Oh. teacherdao.exe cSql ("insert into t_teacher (name, age) values (Professor 'meters ', 50) ", null );}}
          
         
        
       
      
     
    
   
  
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.