介绍一个ORM框架,LitePal。
GitHub:https://github.com/LitePalFramework/LitePal
使用方法很简单,导入xx包就不说了。
- 创建一个类,继承DataSupport。
public class StudentBean extends DataSupport {
private int studentID;
private String studentName;
private String studentAge;
private String studentSex;
private String studentPhone;
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
this.studentSex = studentSex;
}
public String getStudentPhone() {
return studentPhone;
}
public void setStudentPhone(String studentPhone) {
this.studentPhone = studentPhone;
}
}
- assets目录下创建一个名为litepal.xml的配置文件
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="litepal" >
</dbname>
<version value="3" >
</version>
<list>
<mapping class="com.jeremy.universal_android.sqlite.bean.StudentBean"></mapping>
</list>
</litepal>
- application继承LitePalApplication
public class UniversalAndroidApplication extends LitePalApplication {
@Override
public void onCreate() {
super.onCreate();
// 初始化sqlite库
LitePalApplication.initialize(this);
}
}
如何使用?
private void insertWithTs() {
StudentBean studentBean = null;
for (int i = 0; i < 1000; i++) {
studentBean = new StudentBean();
studentBean.setStudentName("小明" + i);
studentBean.setStudentAge("" + i);
studentBean.save();
}
}
private void insertWithoutTs() {
List<StudentBean> studentBeanList = new ArrayList<>();
StudentBean studentBean = null;
for (int i = 0; i < 1000; i++) {
studentBean = new StudentBean();
studentBean.setStudentName("小明" + i);
studentBean.setStudentAge("" + i);
studentBeanList.add(studentBean);
}
DataSupport.saveAll(studentBeanList);
}
问题来了,上面两个方法同样是插入1000条数据,区别是什么呢?insertWithTs()调用的是对象的save(),执行1000次;而insertWithoutTs()是把1000个对象放在一个集合中,最后调用一次saveAll()。
看一下两种方法效率如何。
insertWithTs(),单位是毫秒
E/SQLITE: begin --> 1470285148375
E/SQLITE: end --> 1470285156347
E/SQLITE: gap --> 7972
insertWithoutTs(),单位是毫秒
E/SQLITE: begin --> 1470285043075
E/SQLITE: end --> 1470285045001
E/SQLITE: gap --> 1926
差别如此之大,原因在哪里?看源码
save()方法是这样的
public synchronized void saveThrows() {
SQLiteDatabase db = Connector.getDatabase();
db.beginTransaction();
try {
// some code
db.setTransactionSuccessful();
} catch (Exception e) {
throw new DataSupportException(e.getMessage());
} finally {
db.endTransaction();
}
}
而saveAll()呢?
public static synchronized <T extends DataSupport> void saveAll(Collection<T> collection) {
SQLiteDatabase db = Connector.getDatabase();
db.beginTransaction();
try {
//code to save collection
db.setTransactionSuccessful();
} catch (Exception e) {
throw new DataSupportException(e.getMessage());
} finally {
db.endTransaction();
}
}
每一次save()都涉及到一个事务的begin和end,当数据量变大时,代码执行效率就会明显低于saveAll()的方式。另外需要注意的是,数据插入属于耗时操作,应开启线程。