1. 簡(jiǎn)介
LitePal是一款開(kāi)源的Android數(shù)據(jù)庫(kù)框架,采用對(duì)象關(guān)系映射(ORM)模式,將常用的數(shù)據(jù)庫(kù)功能進(jìn)行封裝,可以不用寫(xiě)一行SQL語(yǔ)句就可以完成創(chuàng)建表、增刪改查的操作。并且很輕量級(jí),jar包不到100k,幾乎零配置。
2. 關(guān)系映射模型?
我們的編程語(yǔ)言使用的是面向?qū)ο笳Z(yǔ)言,數(shù)據(jù)庫(kù)用的是關(guān)系型數(shù)據(jù)庫(kù),將面向?qū)ο笳Z(yǔ)言和關(guān)系型數(shù)據(jù)庫(kù)建立的一種映射關(guān)系成為對(duì)象關(guān)系映射。
3. 為什么使用對(duì)象關(guān)系映射?
因?yàn)槲覀兌急容^擅長(zhǎng)面向?qū)ο缶幊?,只有很少一部分人精通關(guān)系型數(shù)據(jù)庫(kù),絕大多數(shù)的人都不太喜歡在代碼中寫(xiě)Sql語(yǔ)句,使用面向?qū)ο髞?lái)操作數(shù)據(jù)庫(kù),從而可以從Sql語(yǔ)句中解脫出來(lái)。
4. 關(guān)系映射模型特點(diǎn)?
每一張表,都有一張對(duì)應(yīng)的JavaBean類,比如我要?jiǎng)?chuàng)建一張news表,就需要去創(chuàng)建一個(gè)News { } 類。
5. 使用步驟如下:
1>:添加依賴:
// litepal數(shù)據(jù)庫(kù)
compile 'org.litepal.android:core:1.6.1'
2>:新建assets目錄,然后創(chuàng)建 litepal.xml資源文件,用于創(chuàng)建數(shù)據(jù)庫(kù)名稱、數(shù)據(jù)庫(kù)版本、表名、數(shù)據(jù)庫(kù)存放的位置;

<?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="litepaldemo" />
<!--
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>
<mapping class="com.novate.litepal.News"></mapping>
</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" />
-->
<!-- 直接設(shè)置這個(gè),就表示數(shù)據(jù)庫(kù)存儲(chǔ)的位置,直接打開(kāi)手機(jī)存儲(chǔ)就可以找到 -->
<storage value="guolin/database"/>
</litepal>
以上數(shù)據(jù)庫(kù)名稱是litepaldemo、版本是1、只有一張表是News、數(shù)據(jù)庫(kù)存放的位置是guolin/database 打開(kāi)手機(jī)存儲(chǔ)就可以找到。
注意兩點(diǎn):
第一:每次只要數(shù)據(jù)庫(kù)發(fā)生變動(dòng), 版本號(hào)version都必須加1;
第二:表的寫(xiě)法是全類名+表名(News),如果是多張表,就在list標(biāo)簽中寫(xiě)多個(gè)mapping標(biāo)簽就ok,比如:
<list>
<mapping class="com.novate.litepal.News"></mapping>
<mapping class="com.test.model.Reader" ></mapping>
<mapping class="com.test.model.Magazine" ></mapping>
</list>
3>:然后在你Application中配置,這里有2種寫(xiě)法:
第一:如果你自己項(xiàng)目中沒(méi)有寫(xiě)B(tài)aseApplication這種基類的話,就直接在清單文件中配置 LitePalApplication,代碼如下:
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
第二:如果你自己的項(xiàng)目中定義了自己的 BaseApplication,那么就直接在BaseApplication中的onCreate()方法中初始化下 LitePal就ok。
<manifest>
<application
android:name="com.example.BaseApplication"
...
>
...
</application>
</manifest>
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化LitePal數(shù)據(jù)庫(kù)
LitePal.initialize(this);
}
...
}
這個(gè)時(shí)候,我們就已經(jīng)創(chuàng)建好了數(shù)據(jù)庫(kù)、表,并且也已經(jīng)初始化好了,就可以開(kāi)始進(jìn)行增、刪、改、查等操作了,我們?cè)陂_(kāi)發(fā)的過(guò)程中其實(shí)用的最多的也就是增、刪、改、查等方法。
6. 增、刪、改、插寫(xiě)法如下:
6.1>:增加一條數(shù)據(jù)

注意:save()是添加一條數(shù)據(jù),批量添加是 DataSupport.saveAll(persons);
6.2>:刪除

6.3>:修改

6.4>:查詢

注意:
1>:以上的增、刪、改、查語(yǔ)句中只要涉及到 的 "?",意思是 占位符,前邊有幾個(gè) "?", 那么后邊就會(huì)有對(duì)應(yīng)的幾個(gè)值,然后用 "," 逗號(hào)隔開(kāi);
2>:以上就是常用的增、刪、改、查的語(yǔ)句了,當(dāng)然肯定不是特別的全,以后如果還有其他需求的話,大家可以直接去網(wǎng)上去搜都是可以的,下邊給大家羅列下關(guān)于 LitePal的系列文章,郭大神寫(xiě)的,我上邊的四張截圖就是運(yùn)行郭大神的demo,然后放上去的。
該demo的 github如下:
https://github.com/LitePalFramework/LitePal
7. 郭神相關(guān)LitePal系列文章鏈接:
Android數(shù)據(jù)庫(kù)高手秘籍(零)——前言
http://blog.csdn.net/guolin_blog/article/details/38083103
Android數(shù)據(jù)庫(kù)高手秘籍(一)——SQLite命令
http://blog.csdn.net/guolin_blog/article/details/38461239
Android數(shù)據(jù)庫(kù)高手秘籍(二)——?jiǎng)?chuàng)建表和LitePal的基本用法
http://blog.csdn.net/guolin_blog/article/details/38556989
Android數(shù)據(jù)庫(kù)高手秘籍(三)——使用LitePal升級(jí)表
http://blog.csdn.net/guolin_blog/article/details/39151617
Android數(shù)據(jù)庫(kù)高手秘籍(四)——使用LitePal建立表關(guān)聯(lián)
http://blog.csdn.net/guolin_blog/article/details/39207945
Android數(shù)據(jù)庫(kù)高手秘籍(五)——LitePal的存儲(chǔ)操作
http://blog.csdn.net/guolin_blog/article/details/39345833
Android數(shù)據(jù)庫(kù)高手秘籍(六)——LitePal的修改和刪除操作
http://blog.csdn.net/guolin_blog/article/details/40083685
Android數(shù)據(jù)庫(kù)高手秘籍(七)——體驗(yàn)LitePal的查詢藝術(shù)
http://blog.csdn.net/guolin_blog/article/details/40153833
Android數(shù)據(jù)庫(kù)高手秘籍(八)——使用LitePal的聚合函數(shù)
http://blog.csdn.net/guolin_blog/article/details/40614197
下面是迭代的版本:
2016-03-03 郭霖
LitePal 1.3.1發(fā)布了,從此支持圖片存儲(chǔ)!
http://mp.weixin.qq.com/s/7tDifDvOHr7YI7zCS9fpmQ
2016-11-15 郭霖
你們要的多數(shù)據(jù)庫(kù)功能終于來(lái)了
http://mp.weixin.qq.com/s/C7nbJXOS9lYPgOda-8Pw0A
2017-03-07 郭霖
LitePal 1.5.0版本發(fā)布,你想要的都在這里
http://mp.weixin.qq.com/s/GXPkrkiHk4MWNnpCLzTf_Q
2017-08-15
LitePal 1.6.0版本來(lái)襲,數(shù)據(jù)加解密功能保障你的應(yīng)用數(shù)據(jù)安全
https://mp.weixin.qq.com/s/TSp36cnKLxUmAHjT86UCrQ