標籤:
說明
/** * YDL_Hibernate總結 <br/> * (一)支援功能: 1.自己主動建表,支援屬性來自繼承類:可依據註解自己主動完畢建表,而且對於繼承類中的註解欄位也支援自己主動建表. 2.自己主動支援增刪改 * ,增改支援對象化操作:增刪改是資料庫操作的最基本單元,不用反覆寫這些增刪改的代碼,而且加入和更新支援相似於hibernate中的對象化操作. * 3.查詢方式靈活:支援android架構提供的方式,也支援原生sql方式. * 4.查詢結果對象化:對於查詢結果可自己主動封裝為實體物件,相似於hibernate架構. * 5.查詢結果靈活:查詢結果支援對象化,也支援結果為List<Map<String,String>>形式,這種方法在實際項目中非常有用,且效率更好些. * 6.日誌較具體:由於android開發不支援熱部署調試,運行報錯時可依據日誌來定位錯誤,這樣能夠降低運行Android的次數. <br/> * (二)不足之處: <br/> * 1.id臨時僅僅支援int類型,不支援uuid,在sqlite中不建議用uuid. * 2.如今每一個方法都自己開啟和關閉事務,臨時還不支援在一個事務中做多個操作然後統一提交事務. <br/> * (三)作者寄語:<br/> * 昔日有JavaScript借Java發展,今日也希望YDL_Hibernate借Hibernate之名發展. * 希望這個項目以後會成為開源社區的重要一員,更希望這個項目能給全部Android開發人員帶便利. * 歡迎訪問我的部落格:http://blog.csdn.net/linglongxin24, * 這裡有這個架構的使用範例和原始碼,希望朋友們多多交流完好這個架構,共同推動中國開源事業的發展,YDL_Hibernate期待與您共創美好未來!!! */public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 熟悉用介面的朋友注意哦,這裡也能夠定義為介面哦,見StudentDaoImpl.java中的凝視.TeacherDaoImpl teacherDao = new TeacherDaoImpl(MainActivity.this);StudentDaoImpl studentDao = new StudentDaoImpl(MainActivity.this);// 加入Teacher teacher = new Teacher();teacher.setName("米老師");teacher.setAge(50);teacher.setTitle("教授");Long teacherId = teacherDao.insert(teacher);Student student1 = new Student();student1.setName("lk");student1.setAge(26);student1.setClasses("五");student1.setTeacherId(teacherId.intValue());Long studentId1 = studentDao.insert(student1);Student student2 = new Student();student2.setName("cls");student2.setAge(26);student2.setClasses("五");student2.setTeacherId(teacherId.intValue());Long studentId2 = studentDao.insert(student2);Student student3 = new Student();student3.setName("lb");student3.setAge(27);student3.setClasses("五期");student3.setTeacherId(teacherId.intValue());Long studentId3 = studentDao.insert(student3);// 查詢// 方式1:依據Id查詢單個對象// 結果:student1Student [id=1, name=lk,age=26,teacherId=1, classes=五]Student student4 = studentDao.get(studentId1.intValue());System.out.println("student4" + student4);// 方式2:查詢出表中的全部記錄// 運行結果例如以下:// list1:Student [id=1, name=lk,age=26,teacherId=1, classes=五]// list1:Student [id=2, name=cls,age=26,teacherId=1, classes=五]// list1:Student [id=3, name=lb,age=27,teacherId=1, classes=五期]List<Student> list1 = studentDao.find();for (Student student : list1) {System.out.println("list1:" + student);}// 方式3:限制條件查詢和查詢結果// 運行結果: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);}// 方式4:使用sql查詢出結果,此種方式是2,3,4中最靈活的.// 運行結果:// list3:Student [id=2, name=cls,age=26,teacherId=1, classes=五]// list3:Student [id=3, name=lb,age=27,teacherId=1, classes=五期]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);}// 方式4進階:假設想查詢出米老師的學生,能夠這樣實現:// 運行結果:// list4:Student [id=1, name=lk,age=26,teacherId=1, classes=五]// list4:Student [id=2, name=cls,age=26,teacherId=1, classes=五]// list4:Student [id=3, name=lb,age=27,teacherId=1, classes=五期]List<Student> list4 = studentDao.rawQuery("select s.* from t_student s join t_teacher t on s.teacher_id = t.id where t.name= ? ",new String[] { "米老師" });for (Student student : list4) {System.out.println("list4:" + student);}// 方式5:我僅僅想知道姓名和年齡,查詢得到List<Map<String,String>>形式.僅僅查2個字會比查詢全部欄位並封裝為對象效率高吧,尤其欄位值非常多時我們的手機更喜歡這樣的方式哦.// 結果:// 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) {// 查詢的List中的map以查詢sql中的屬性值的小寫形式為key,注意是小寫形式哦.System.out.println("listMap1: name:" + map.get("name") + ";age:"+ map.get("age"));}// 方式5進階:我想知道前2名學生的姓名和班主任姓名,這樣的方式是不是超靈活啊,用其它的方式查詢都沒這樣的方式好用吧,哈哈.// 結果:// listMap2: student_name:lk;teacher_name:米老師// listMap2: student_name:cls;teacher_name:米老師List<Map<String, String>> 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<String, String> map : listMap2) {System.out.println("listMap2: student_name:" + map.get("sname")+ ";teacher_name:" + map.get("tname"));}// 更新// 結果: Student [id=1, name=李坤,age=26,teacherId=1, classes=五期]student1 = studentDao.get(studentId1.intValue());student1.setName("李坤");student1.setClasses("五期");studentDao.update(student3);System.out.println(student1);// 刪除:支援單個id刪除,也支援多個id同一時候刪除哦.studentDao.delete(studentId1.intValue());studentDao.delete(new Integer[] { studentId2.intValue(),studentId3.intValue() });// 支援運行sql語句哦.teacherDao.execSql("insert into t_teacher(name,age) values('米教授',50)",null);}}
Demo和原始碼下載
著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。
Android資料庫hibernate架構