1、下载jar
具体到Android,需要在 http://ormlite.com/releases 页面下载两个jar 包(本文基于ORMLite的版本是:ormlite 4.49-SNAPSHOT):
(1)core列表下的jar包;
(2)android列表下的jar包。
如图所示:
将上面的两个jar包下载后放到Android工程项目中的libs包中,如图所示:
以上完成后,ORMLite开发环境搭建就完成了,接下来就可以使用了。
2、代码:
表student.java和AClass.java
1 package com.zzw.ormlitedatabasetest; 2 3 import com.j256.ormlite.field.DatabaseField; 4 import com.j256.ormlite.table.DatabaseTable; 5 6 @DatabaseTable(tableName = "Student") 7 public class Student { 8 9 @DatabaseField(id = true, columnName = "Student_id") 10 public int Student_id; 11 @DatabaseField(columnName = "name") 12 public String name; 13 @DatabaseField(columnName = "sex") 14 public String sex; 15 @DatabaseField(columnName = "age") 16 public int age; 17 @DatabaseField(foreign = true, foreignAutoRefresh = true) 18 public AClass aClass; 19 20 public Student() { 21 22 } 23 24 public Student(String name, String sex, int age) { 25 super(); 26 this.name = name; 27 this.sex = sex; 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Student [Student_id=" + Student_id + ", name=" + name + ", sex=" + sex + ", age=" + age + ",className=" 34 + aClass.name + "]"; 35 } 36 37 }
Student
1 package com.zzw.ormlitedatabasetest; 2 3 import com.j256.ormlite.dao.ForeignCollection; 4 import com.j256.ormlite.field.DataType; 5 import com.j256.ormlite.field.DatabaseField; 6 import com.j256.ormlite.field.ForeignCollectionField; 7 import com.j256.ormlite.table.DatabaseTable; 8 9 @DatabaseTable(tableName = "Classess") 10 public class AClass { 11 12 @DatabaseField(id = true, columnName = "classess_id") 13 public int classess_id; 14 @DatabaseField(dataType = DataType.STRING) 15 public String name; 16 @ForeignCollectionField(eager = false) 17 public ForeignCollection<Student> students = null; 18 19 public AClass() { 20 super(); 21 } 22 23 public AClass(int classess_id, String name, ForeignCollection<Student> students) { 24 super(); 25 this.classess_id = classess_id; 26 this.name = name; 27 this.students = students; 28 } 29 30 }
AClass
ORMLite数据库管理工具类ORMLiteDatabaseHelper.java:
1 package com.zzw.ormlitedatabasetest; 2 3 import java.sql.SQLException; 4 5 import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; 6 import com.j256.ormlite.dao.Dao; 7 import com.j256.ormlite.support.ConnectionSource; 8 import com.j256.ormlite.table.TableUtils; 9 10 import android.content.Context; 11 import android.database.sqlite.SQLiteDatabase; 12 import android.database.sqlite.SQLiteDatabase.CursorFactory; 13 import android.util.Log; 14 15 public class OrmLiteDatabaseHelper extends OrmLiteSqliteOpenHelper { 16 private static final String TAG = "OrmLiteDatabaseHelper"; 17 18 public static OrmLiteDatabaseHelper mDatabaseHelper = null; 19 20 public Dao<Student, Integer> mStudentDao = null; 21 public Dao<AClass, Integer> mAClassDao = null; 22 23 public static String database_NAME = "school.db"; 24 public static int database_VERSION = 1; 25 26 public OrmLiteDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) { 27 super(context, databaseName, factory, databaseVersion); 28 } 29 30 public static OrmLiteDatabaseHelper getInstance(Context context) { 31 if (mDatabaseHelper == null) { 32 mDatabaseHelper = new OrmLiteDatabaseHelper(context, database_NAME, null, database_VERSION); 33 } 34 Log.d(TAG, "数据库school.db创建成功"); 35 return mDatabaseHelper; 36 } 37 38 @Override 39 public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { 40 try { 41 TableUtils.createTableIfNotExists(arg1, AClass.class); 42 Log.e(TAG, "AClass表在School.db中被创建"); 43 TableUtils.createTableIfNotExists(arg1, Student.class); 44 Log.e(TAG, "Student表在School.db中被创建"); 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } 48 49 } 50 51 @Override 52 public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { 53 54 } 55 56 public Dao<Student, Integer> getStudentDao() { 57 if (mStudentDao == null) { 58 try { 59 mStudentDao = getDao(Student.class); 60 Log.e(TAG, "得到Student的Dao"); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 } 65 return mStudentDao; 66 } 67 68 public Dao<AClass, Integer> getAClassDao() { 69 if (mAClassDao == null) { 70 try { 71 mAClassDao = getDao(AClass.class); 72 Log.e(TAG, "得到AClass的Dao"); 73 } catch (SQLException e) { 74 e.printStackTrace(); 75 } 76 } 77 return mAClassDao; 78 } 79 80 @Override 81 public void close() { 82 super.close(); 83 if (mAClassDao != null) { 84 mAClassDao = null; 85 } 86 if (mStudentDao != null) { 87 mStudentDao = null; 88 } 89 } 90 }
MainActivity:
1 package com.zzw.ormlitedatabasetest; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.j256.ormlite.dao.Dao; 7 8 import android.app.Activity; 9 import android.graphics.Color; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.widget.ScrollView; 13 import android.widget.TextView; 14 import android.widget.Toast; 15 16 public class MainActivity extends Activity { 17 18 private static final String TAG = "MainActivity"; 19 20 private Dao<AClass, Integer> mAClassDao = null; 21 private Dao<Student, Integer> mStudentDao = null; 22 23 AClass aClass1, aClass2, aClass3, aClass4, aClass5; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 OrmLiteDatabaseHelper mOrmLiteDatabaseHelper = OrmLiteDatabaseHelper.getInstance(this); 31 32 mAClassDao = mOrmLiteDatabaseHelper.getAClassDao(); 33 mStudentDao = mOrmLiteDatabaseHelper.getStudentDao(); 34 35 } 36 37 @Override 38 protected void onStart() { 39 Log.d(TAG, ">>>>>>>>>>>>"); 40 super.onStart(); 41 int AClasses_count = createAClasses(); 42 if (AClasses_count == 5) { 43 try { 44 aClass1 = mAClassDao.queryForId(1); 45 aClass2 = mAClassDao.queryForId(2); 46 aClass3 = mAClassDao.queryForId(3); 47 aClass4 = mAClassDao.queryForId(4); 48 aClass5 = mAClassDao.queryForId(5); 49 50 createStudents(); 51 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } else { 56 Toast.makeText(this, "班级创建错误", 0).show(); 57 } 58 queryAllStudents(); 59 } 60 61 private void createStudents() { 62 Log.d(TAG, "学生创建开始------>"); 63 Student student = new Student(); 64 for (int i = 1; i <= 20; i++) { 65 student.Student_id = i; 66 student.age = (int) (Math.random() * 100); 67 if (i % 2 == 0) { 68 student.sex = "男"; 69 } else { 70 student.sex = "女"; 71 } 72 if (i > 0 && i <= 4) { 73 student.aClass = aClass1; 74 student.name = "陶" + i; 75 } else if (i > 4 && i <= 8) { 76 student.aClass = aClass2; 77 student.name = "李" + i; 78 } else if (i > 8 && i <= 12) { 79 student.aClass = aClass3; 80 student.name = "阿" + i; 81 } else if (i > 12 && i <= 16) { 82 student.aClass = aClass4; 83 student.name = "曾" + i; 84 } else { 85 student.aClass = aClass5; 86 student.name = "蒋" + i; 87 } 88 89 try { 90 mStudentDao.createIfNotExists(student); 91 } catch (SQLException e) { 92 e.printStackTrace(); 93 } 94 } 95 Log.d(TAG, "学生创建成功------>"); 96 } 97 98 private int createAClasses() { 99 Log.d(TAG, "班级创建开始------>"); 100 AClass aClass = new AClass(); 101 int AClasses_count = 0; 102 for (int i = 1; i <= 5; i++) { 103 aClass.classess_id = i; 104 aClass.name = "高三" + i + "班"; 105 try { 106 mAClassDao.createIfNotExists(aClass); 107 AClasses_count++; 108 } catch (SQLException e) { 109 e.printStackTrace(); 110 } 111 } 112 Log.d(TAG, "班级创建成功------>"); 113 return AClasses_count; 114 } 115 116 private void queryAllStudents() { 117 Log.d(TAG, "查询全部学生开始------>>>>"); 118 try { 119 List<Student> students = mStudentDao.queryForAll(); 120 if (students != null && students.size() > 0) { 121 for (Student student : students) { 122 Log.i(TAG, student.toString()); 123 } 124 } 125 } catch (SQLException e) { 126 e.printStackTrace(); 127 } 128 Log.d(TAG, "查询全部学生完毕------>>>>"); 129 } 130 131 @Override 132 protected void onDestroy() { 133 super.onDestroy(); 134 if (mAClassDao != null) { 135 mAClassDao = null; 136 } 137 if (mStudentDao != null) { 138 mStudentDao = null; 139 } 140 Log.d(TAG, "<<<<<<<<<<<"); 141 } 142 143 }
时间: 2024-10-14 05:00:45