Android中使用ormlite實現持久化--HelloOrmLite
By 黃雲坤
2011-10-30 更新日期:2014-03-18
Android中內建了sqlite,但是常用的開發語言java是物件導向的,而資料庫是關係型的,二者之間的轉化每次都很麻煩(主要是我對sql語言不熟悉)。而Java Web開發中有很多orm架構,但是想直接放到Android上用有些麻煩。嘗試了一下找Android的orm架構,說實話還有好幾個。
實現考慮的是:androrm
官網:http://androrm.the-pixelpla.net/
說實話,這個我實在沒有弄懂,一共兩個包。
一個是依賴包:Apache Commons - Lang (2.6)
另外一個就是主包:androrm.jar 不管怎麼下載的都不能使用…
然後有考慮了一下db4o
官網:http://www.db4o.com/
官網上的介紹說是已經支援Android了,但是我一是覺得包有點大,而是覺得速度有點慢
最後看到的就是ormlite
官網:http://ormlite.com/
一共兩個包:一個是ormlite-core-4.24.jar,另一個是ormlite-android-4.24.jar
從以下網址可以下載到:http://ormlite.com/releases/
下面按照慣例來個Hello world
建立Android項目:HelloOrmLite
添加檔案夾:libs,將所需的兩個包複製到其中。添加引用
建立一個model:Hello.java
12345678910111213141516171819202122 |
package cn.sdx.model; import com.j256.ormlite.field.DatabaseField; public class Hello { @DatabaseField(generatedId = true) int id; @DatabaseField String word; public Hello() { } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(id=).append(id); sb.append( ,word=).append(word); return sb.toString(); } } |
@DatabaseField是聲明id為資料庫欄位,generatedId =true聲明id為自增長
然後重寫了toString()
再添加一個DataHelper.java
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
package cn.sdx.utils; import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import cn.sdx.model.Hello; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; public class DataHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = HelloOrmlite.db; private static final int DATABASE_VERSION = 1; private Dao helloDao = null; public DataHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, Hello.class); } catch (SQLException e) { Log.e(DataHelper.class.getName(), 建立資料庫失敗, e); e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) { try { TableUtils.dropTable(connectionSource, Hello.class, true); onCreate(db, connectionSource); } catch (SQLException e) { Log.e(DataHelper.class.getName(), 更新資料庫失敗, e); e.printStackTrace(); } } @Override public void close() { super.close(); helloDao = null; } public Dao getHelloDataDao() throws SQLException { if (helloDao == null) { helloDao = getDao(Hello.class); } return helloDao; } } |
在布局檔案中添加一個TextView
HelloOrmliteActivity.java中添加對資料庫的操作
代碼如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
package cn.sdx; import java.sql.SQLException; import java.util.List; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; import com.j256.ormlite.dao.Dao; import android.os.Bundle; import android.widget.TextView; import cn.sdx.model.Hello; import cn.sdx.utils.DataHelper; public class HelloOrmliteActivity extends OrmLiteBaseActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) this.findViewById(R.id.output); try { Dao helloDao = getHelper().getHelloDataDao(); // 添加資料 for (int i = 0; i < 2; i++) { Hello hello = new Hello(Hello + i); helloDao.create(hello); } tv.setText(tv.getText() + + 添加資料完成); // 查詢添加的資料 List hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + + h.toString()); } // 刪除資料第一條資料 helloDao.delete(hellos.get(0)); tv.setText(tv.getText() + + 刪除資料完成); // 重新查詢資料 hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + + h.toString()); } // 修改資料 Hello h1 = hellos.get(0); h1.setWord(這是修改過的資料); tv.setText(tv.getText() + + 修改資料完成); helloDao.update(h1); // 重新查詢資料 hellos = helloDao.queryForAll(); for (Hello h : hellos) { tv.setText(tv.getText() + + h.toString()); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
以上實現了資料庫操作相關的增刪改,下面是效果:
OrmLite的功能非常強大,Model類的聲明中非常重要,外鍵約束,非空檢查等等問題都有相對的處理方法。
Android中使用Ormlite實現持久化(二)--持久化類的詳細配置
By 黃雲坤
2011-11-01 更新日期:2014-03-16
上一篇文章簡單的使用了Ormlite一下,但是我覺得Ormlite出色之處就是對於需要欄位,表等等的配置設計的比較好。
下面來說說類的配置:
如果我們開發的Android應用需要保持使用者資訊,那麼現在建立一個類:UserAccount
該類有六個變數:
1 |
private int id; private String username; private String password; private Date regTime; private String tellphone; private String email; |
用eclipse產生get和set方法:
1 |
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public String getTellphone() { return tellphone; } public void setTellphone(String tellphone) { this.tellphone = tellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } |
下面通過註解的方式配置該類的持久化參數:
1.表名:
1 |
@DatabaseTable(tableName=dataTableName) |
不指定的話表名就是類名。
2.欄位
這個可以配置的屬性有點多。
2.1 主鍵
1 |
@DatabaseField(id=true) |
2.2 列名
1 |
@DatabaseField(columnName=columnName) |
不指定的話就是和變數名一樣的
2.3 資料類型
1 |
@DatabaseField(dataType=DataType.INTEGER) |
這個一般情況下都不用指定,可以根據java 類獲得
2.4 預設值
1 |
@DatabaseField(defaultValue=0) |
2.5 長度
1 |
@DatabaseField(width=13) |
一般用於String型
2.6 能否為空白
1 |
@DatabaseField(canBeNull=false) |
預設為True
2.7 是否自增長
1 |
@DatabaseField(generatedId=true) |
這是一些比較簡單的,關於外鍵啥的下篇文章再說吧。