【Android】LitePal的基础

一、环境配置

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++; }
原文地址:https://www.cnblogs.com/robotpaul/p/11726751.html