一.LitePal的介绍
Litepal是Android郭霖大神的一个开源Android数据库的开源框架,它采用了对象关系映射(ORM)的模式,这是让我们非常好的理解的数据库,一个实体类对应我们数据库中的一个表。该库中还封装了许多的方法,就算对SQL语句的了解很少,也能很好的建立起数据库,以及各种增删改查的操作。Litepal官方链接.
二.Litepal的配置
1.添加依赖
首先去官方地址中找到当前最新的依赖,并在我们项目的dependencies下添加:目前我最新版本是3.2.3
dependencies {implementation 'org.litepal.guolindev:core:3.2.3'
}
2.配置Litepal
一定要先去我们的项目AndroidManifest.xml下配置我们的数据库,这样我们的Litepal才能正常工作,加上android:name=“org.litepal.LitePalApplication”:
<manifest><applicationandroid:name="org.litepal.LitePalApplication"...>...</application>
</manifest>
如果你已经有了自己的Application的configure在这,例如:
<manifest><applicationandroid:name="com.example.MyApplication"...>...</application>
</manifest>
那你可以在你的Myapplication中加上LitePal.initialize(this);,来初始化配置。
public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();LitePal.initialize(this);}...
}
3.创建litepal.xml文件
右击app/src/main 目录->new Directory,创建一个assets目录,然后再在该目录下new File一个litepal.xml文件,接着编辑里面的内容:
<?xml version="1.0" encoding="utf-8"?>
<litepal><dbname value="NewsDemo" /><version value="1" /><list></list></litepal>
dbname 标签用来指定数据库的名称 version 标签是指定数据库版本,更新表的时候需要增大该值 list 标签是用来指定我们的映射模型,也就是将我们实体类的路径用 mapping标签放进来
到现在为止我们的litepal配置工作就全部完成了,接下来就来正式使用吧
三.Litepal的使用
1.创建数据库
首先定义我们的实体类,这里我就简单化,先定义一个News新闻类和一个内容类Content:
在我们对实体类进行数据库操作之前,一定要先让我们数据库知道哪些是我们的实体类,可以对其进行操作,故而需要让我们的实体类先继承LitepalSupport这个类!!!
public class News extends LitepalSupport{private int id;@Column(unique = true, defaultValue = "unknown")private String title;private String Content;//getter , setter 方法//...
}
public class Comment extends LitepalSupport{private int id;@Column(nullable = false)private String content;//getter , setter 方法//...
}
这里就是典型的两个javaBean类,定义了新闻的id,标题,内容,评论类的id 和内容,以及他们的getter 和 setter的方法。还有一些限定类的一些注解。这里你是不是感觉到了关系映射的最直观的体验了吧!
定义好实体类,一定不要忘记添加到映射模型的列表当中,修改litepal.xml文件
<?xml version="1.0" encoding="utf-8"?>
<litepal><dbname value="NewsDemo" /><version value="1" /><list><mapping ></mapping><mapping ></mapping></list></litepal>
这样我们只需要对数据库随便进行一次操作就能初始化数据库啦,我们一般可以调用Litepal.getDatabase() 方法。
怎么看是否成功了呢?
我们可以通过Android Studio的View ->ToolsWindows -> Device File Explorer
然后进入 data/data/< 包名 > / databases 下回生成两个文件,其中一个是以db结尾的,这就是我们的数据库文件,如果想要看其中的内容,就需要通过db shell 查看数据库的创建情况了,这我就不多展开了,想看的朋友自己去看一下。
2.升级数据库
比如现在我们的News 类需要和 Comment建立一个关系映射,是一对多的关系,也就是一个News类可以有多个Comment类对应,这样我们就可以修改类:
public class News{private int id;@Column(unique = true, defaultValue = "unknown")private String title;private String Content;private List<Comment> commentList = new ArrayList<>();//getter , setter 方法//...
}
public class Comment{private int id;@Column(nullable = false)private String content;private News news;//getter , setter 方法//...
}
这样,我们在News类中加上了private List commentList = new ArrayList<>();,这里特别注意commentList要先自己实例化,方便我们后面对comment的存储,而对应的Comment是只对应我们一个News的,故而其里面加上private News news;
修改了实体类,我们要怎么通知数据库去更新呢?很简单,我们只需要把litepal.xml里面的版本号加1就行了!value=1 -> value=2
<?xml version="1.0" encoding="utf-8"?>
<litepal><dbname value="NewsDemo" /><version value="2" /><list><mapping ></mapping><mapping ></mapping></list></litepal>
3.Litepal的CRUD
①添加数据
Comment comment1 = new Comment();
comment1.setContent("太不道德了吧!");
comment1.save();Comment comment2 = new Comment();
comment2.setContent("这新闻真让人汗颜!");
comment2.save();News new = new News();
new.setTitle("日本核污水");
new.setContent("日本决定把核污水排入海洋");
new.getCommentList().add(comment1);
new.getCommentList().add(comment2);
new.save();
注意事项
首先第一点,调用save()方法,将我们的实体类对象保存到Litepal数据库中 接着,News 类中和Comment类中的id是不需要我们指定的,框架会帮我们自动生成 指定两个类的映射关系后,这里指一对多的关系,多的那个类(Comment)里面的news对象是不需要我们来设置的,它在数据库中会自动转变成一个news_id字段,该字段指明了它是对应在那个News中的评论,调用new.getCommentList().add(comment1)的时候,Litepal框架会帮我们自动设置好这两个类的映射关系。 在我们要查询特定News对象中有哪些评论的时候,可以这样查,根据你这个对象的id,去匹配所有comment的news_id:
List<Comment> commentList = LitePal.where("news_,String.valueOf(news.getId())).find(Comment.Class);
②查询数据
//查询所有的new
List<News> newList = LitePal.findAll(News.class);//根据指定条件查询
//
List<News> newList1 = LitePal.where("title=? and content=?","日本核污水","日本决定把核污水排入海洋").find(News.class);
③更新数据
//第一种方式:
News news = new News();
news.setContent("排污水进海洋太可恶了!!");
news.upDateAll("title=?","日本核污水").find(News.class);//第二种方式:
News news = LitePal.find(News.class,1); //1指id号
news.setContent("排污水进海洋太可恶了!!");
news.save();//第三种方式
News news = new News();
news.setContent("排污水进海洋太可恶了!!");
news.update(id);
④删除数据
//第一种
LitePal.deleteAll(News.class,"title=?","日本核污水");//第二种
LitePal.delete(News.class,id);
4.使用多个数据库:
第一种方法:代码方式创建
LitePalDB litePalDB = new LitePalDB("otherDataBase", 1);
litePalDB.addClassName(Singer.class.getName());
litePalDB.addClassName(Album.class.getName());
litePalDB.addClassName(Song.class.getName());
LitePal.use(litePalDB);
第二种:用类似litepal.xml创建,这里是otherDataBase.xml:
LitePalDB litePalDB = LitePalDB.fromDefault("otherDataBase");
LitePal.use(litePalDB);
随时切换回默认数据库: LitePal.useDefault();
删除数据库: LitePal.deleteDatabase(“otherDataBase”);
四.总结
LitePal关系型数据库用起来还是很简单明了的,如果你对文章有什么疑惑或者指正,欢迎评论区留言!如果你觉得文章对你有帮助的话,点个赞或者收藏都是对笔者最大的支持!感谢!