-
Notifications
You must be signed in to change notification settings - Fork 0
Home
FengPeng edited this page Feb 27, 2017
·
1 revision
Welcome to the ORMProvider wiki!
以注解及ORM的形式使Android content provider的操作更加简洁
首先,在工程级别的build.gradle中添加如下代码:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
然后,在目标模块下的build.gradle中添加如下代码:
dependencies {
...
compile 'com.github.oxsource:ORMProvider:release-v1.0.0'
}
@DataBase(
name = "tools.db",
authority = "ox.source.provider.tools",
tables = {HostTable.class},
since = 1)
public class ToolsProvider extends SQLiteProvider {
@Override
protected Class extends SQLiteProvider> getBuildClass() {
return getClass();
}
@Override
protected void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
创建好对象后需要在AndroidMainfest.xml中配置该provider,具体操作不赘述。
@Table(
name = "hostTable",
db = ToolsProvider.class,
since = 1)
public class HostTable {
@Column(name = "_id", type = Column.FieldType.INTEGER, primary = true, autoIncrement = true)
private int id;
@Column(name = "name", type = Column.FieldType.TEXT, notNull = true)
private String name;
@Column(name = "host", type = Column.FieldType.TEXT, notNull = true)
private String host;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
@Override
public String toString() {
return "HostTable{" +
"id=" + id +
", name='" + name + '\'' +
", host='" + host + '\'' +
'}';
}
}
//query
private void query() {
SQLiteBuilder builder = new SQLiteBuilder(HostTable.class);
SQLiteResolver resolver = new SQLiteResolver(getApplicationContext());
List lists = resolver.query(HostTable.class, builder);
for (HostTable e : lists) {
...
}
}
//insert
private void insert() {
SQLiteResolver resolver = new SQLiteResolver(getApplicationContext());
List lists = new ArrayList<>();
HostTable table = new HostTable();
table.setHost("https://www.baidu.com");
table.setName("baidu");
lists.add(table);
int lines = resolver.insert(lists);
if (lines > 0) {
...
} else {
...
}
}
//delete
private void delete() {
SQLiteBuilder builder = new SQLiteBuilder(HostTable.class);
builder = builder.whereEquals("_id", "1");
SQLiteResolver resolver = new SQLiteResolver(getApplicationContext());
int lines = resolver.delete(HostTable.class, builder);
if (lines > 0) {
...
} else {
...
}
}
//update
private void update() {
HostTable table = new HostTable();
table.setName("update baidu");
table.setHost("update https://www.baidu.com");
SQLiteBuilder builder = new SQLiteBuilder(HostTable.class);
builder.whereEquals("name", "baidu");
SQLiteResolver resolver = new SQLiteResolver(getApplicationContext());
int lines = resolver.update(table, builder);
if (lines > 0) {
...
} else {
...
}
}