第三方数据库框架 - LitePal简介

1. 简介


LitePal是一款开源的Android数据库框架,采用对象关系映射(ORM)模式,将常用的数据库功能进行封装,可以不用写一行SQL语句就可以完成创建表、增删改查的操作。并且很轻量级,jar包不到100k,几乎零配置。

2. 关系映射模型?


我们的编程语言使用的是面向对象语言,数据库用的是关系型数据库,将面向对象语言和关系型数据库建立的一种映射关系成为对象关系映射。

3. 为什么使用对象关系映射?


因为我们都比较擅长面向对象编程,只有很少一部分人精通关系型数据库,绝大多数的人都不太喜欢在代码中写Sql语句,使用面向对象来操作数据库,从而可以从Sql语句中解脱出来。

4. 关系映射模型特点?


每一张表,都有一张对应的JavaBean类,比如我要创建一张news表,就需要去创建一个News { } 类。

5. 使用步骤如下:


1>:添加依赖:

 

// litepal数据库
compile 'org.litepal.android:core:1.6.1'

2>:新建assets目录,然后创建 litepal.xml资源文件,用于创建数据库名称、数据库版本、表名、数据库存放的位置;

图片.png

 

<?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" />
    -->

    <!-- 直接设置这个,就表示数据库存储的位置,直接打开手机存储就可以找到 -->
    <storage value="guolin/database"/>
</litepal>

以上数据库名称是litepaldemo、版本是1、只有一张表是News、数据库存放的位置是guolin/database 打开手机存储就可以找到。

注意两点:

第一:每次只要数据库发生变动, 版本号version都必须加1;
第二:表的写法是全类名+表名(News),如果是多张表,就在list标签中写多个mapping标签就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种写法:

第一:如果你自己项目中没有写BaseApplication这种基类的话,就直接在清单文件中配置 LitePalApplication,代码如下:

 

<manifest>
    <application
        android:name="org.litepal.LitePalApplication"
        ...
    >
        ...
    </application>
</manifest>

第二:如果你自己的项目中定义了自己的 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数据库
        LitePal.initialize(this);
    }
    ...
}

这个时候,我们就已经创建好了数据库、表,并且也已经初始化好了,就可以开始进行增、删、改、查等操作了,我们在开发的过程中其实用的最多的也就是增、删、改、查等方法。

6. 增、删、改、插写法如下:


6.1>:增加一条数据

图片.png

注意:save()是添加一条数据,批量添加是 DataSupport.saveAll(persons);

6.2>:删除

图片.png

6.3>:修改

图片.png

6.4>:查询

图片.png

注意:

1>:以上的增、删、改、查语句中只要涉及到 的 "?",意思是 占位符,前边有几个 "?", 那么后边就会有对应的几个值,然后用 "," 逗号隔开;
2>:以上就是常用的增、删、改、查的语句了,当然肯定不是特别的全,以后如果还有其他需求的话,大家可以直接去网上去搜都是可以的,下边给大家罗列下关于 LitePal的系列文章,郭大神写的,我上边的四张截图就是运行郭大神的demo,然后放上去的。



作者:世道无情
链接:https://www.jianshu.com/p/8035eb5da7a2
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值