当前位置: 首页 > 工具软件 > LBase-Android > 使用案例 >

Android-LitePal数据库框架

宣望
2023-12-01

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

1、引入

dependencies {
    implementation 'org.litepal.guolindev:core:3.2.3'
}

2、app/src/main /assets目录下新建文件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>

3、配置LitePalApplication,在AndroidManifest.xml中添加

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

如果自己有自定义的Application,则在自定义的Application中添加:

public class MyOwnApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
    }
    ...
}

4、开始使用

4.1 创建实体类,例如:

public class Album extends LitePalSupport {
	
    @Column(unique = true, defaultValue = "unknown")
    private String name;
    
    @Column(index = true)
    private float price;

    // generated getters and setters.
    ...
}

4.2 在litepal.xml中配置

<list>
    <mapping class="org.litepal.litepalsample.model.Album" />
</list>

4.3 基本使用

// 获取对象
SQLiteDatabase db = LitePal.getDatabase();
//增
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
//删
LitePal.delete(Album.class, id);
//改
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);
// 查
Song song = LitePal.find(Album.class, id);
List<Album> allAlbums = LitePal.findAll(Album.class);
List<Album> albums = LitePal.where("name like ? and duration < ?", "album%", "200").order("duration").find(Album.class);

更多使用方法请参考:https://github.com/guolindev/LitePal

 类似资料: