一、環境配置
LitePal 在GitHub地址為:https://github.com/LitePalFramework/LitePal
我們使用起來也很方便,直接在gradle中配置即可。
如果你是用的是Java版本,則在 build.gradle(Module:app)中添加:
dependencies { implementation 'org.litepal.android:java:3.0.0' }
Kotlin版本:
dependencies { implementation 'org.litepal.android:kotlin:3.0.0' }
然后點擊Sync Now即可自動下載配置。
LitePal在使用的時候,需要配置一個litepal.xml文件放在安卓的assets目錄中。
如果沒有這個文件夾,點擊你的項目app,右鍵new->Folder->Assets Folder
有了這個文件夾后,選中右鍵新建一個File,注意不要選擇xml
命名為litepal.xml即可。
文件內容官方給的例子如下:
<?xml version="1.0" encoding="utf-8"?> <litepal> <!-- Define the database name of your application. By default each database name should be end with .db. If you didn't name your database end with .db, LitePal would plus the suffix automatically for you. For example: <dbname value="demo" /> --> <dbname value="demo" /> <!-- Define the version of your database. Each time you want to upgrade your database, the version tag would helps. Modify the models you defined in the mapping tag, and just make the version value plus one, the upgrade of database will be processed automatically without concern. For example: <version value="1" /> --> <version value="1" /> <!-- Define your models in the list with mapping tag, LitePal will create tables for each mapping class. The supported fields defined in models will be mapped into columns. For example: <list> <mapping class="com.test.model.Reader" /> <mapping class="com.test.model.Magazine" /> </list> --> <list> </list> <!-- Define where the .db file should be. "internal" means the .db file will be stored in the database folder of internal storage which no one can access. "external" means the .db file will be stored in the path to the directory on the primary external storage device where the application can place persistent files it owns which everyone can access. "internal" will act as default. For example: <storage value="external" /> --> </litepal>
這樣基礎配置就完成了。
二、建表(Bean類)
在LitePal中,一個數據庫的每一張表對應着一個類,這個類只需要繼承自LitePalSupport
類中就會有save() 和 delete()兩個方法,分別代表着保存(更新)和刪除
我們需要創建一個Bean類,使用Alt+Insert快捷鍵可以快速調出生成菜單,生成get和set
例如:
import org.litepal.crud.LitePalSupport; import java.text.SimpleDateFormat; import java.util.Date; /** * 作者:created by 巴塞羅那的余暉 on 2019/10/23 15:16 * 郵箱:zhubaoluo@outlook.com * 不會寫BUG的程序猿不是好程序猿,嚶嚶嚶 */ public class DiaryBean extends LitePalSupport { String weather; String author; String content; String dateString; public DiaryBean(){ Date date=new Date(); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); dateString=simpleDateFormat.format(date); } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getDateString() { return dateString; } public void setDateString(String dateString) { dateString = dateString; } }
在要放在數據庫中的Bean類建好后,我們就需要去litepal.xml中去添加這個類。
<?xml version="1.0" encoding="utf-8"?> <litepal> <dbname value="demo" /> <version value="1" /> <list> <mapping class="com.paul.testlitepal.DiaryBean"/> </list> </litepal>
注意看一下這里的dbname即項目數據庫名稱,list中放的就是你需要使用數據庫的類。
都是固定寫法,請注意這里面的version,如果你要對list記錄中進行刪除或者添加操作,一定要將value的數字加一,否則不會生效且閃退。
三、使用
若要使用LitePal,則必須初始化。
即在onCreat中調用
LitePal.initialize(Context context);
最常用的方法是返回指定表中所有數據的集合:
List<DiaryBean> diaryBeans=LitePal.findAll(DiaryBean.class);
若未找到則會返回一個空的集合,注意一下。
更多的操作方法請參考官方文檔
LitePal.initialize(MainActivity.this); LitePal.findFirst(DiaryBean.class);//查詢DiaryBean表中的第一個元素 LitePal.findLast(DiaryBean.class);//查詢DiaryBean表中的最后一個元素 LitePal.select("content","weather").find(DiaryBean.class);//查詢相應字符的數據,其他是不會反回的 LitePal.order("dateString desc").find(DiaryBean.class);//根據dateString排序,desc降序,asc升序
對對象的操作請看代碼:
LitePal.initialize(MainActivity.this);//初始化 /*-------創建一個DiaryBean對象並保存---------------*/ DiaryBean diary=new DiaryBean(); diary.setAuthor("小明"); diary.setWeather("晴天"); diary.setContent("啦啦啦我會用LitePal啦"); diary.save(); /*-------創建一個DiaryBean對象並保存---------------*/ List<DiaryBean> diaryBeans=LitePal.findAll(DiaryBean.class);//取出所有數據 int cont=0; for(DiaryBean item:diaryBeans){ if(cont%2==0){ item.setAuthor("巴塞羅那的余暉");//修改數據 item.save();//調用save方法會自動更新 }else { item.delete();//刪除 }
cont++; }