compileOptions{annotationEnabled true}
db/DataBase
@Database(entities = {Table.class},version = 1)
public abstract class DataBase extends OrmDatabase {
}
db/Table
@Entity(tableName = "user")
public class Table extends OrmObject {
@PrimaryKey(autoGenerate = true)
private Integer id;
private String name;
private String telnum;
private String info;
及get set 方法
dao/Dao
public interface Dao {
boolean insert(Table table);
boolean update(Table table);
boolean delete(int id);
List<Table> query();
Table queryById(int id);
}
dao/DaoImpl
public class DaoImpl implements Dao {
private OrmContext ormContext;
@Override
public boolean insert(Table table) {
ormContext.insert(table);
return ormContext.flush();
}
public DaoImpl(Context context) {
DatabaseHelper helper = new DatabaseHelper(context);
ormContext = helper.getOrmContext("电话本","tel.db", DataBase.class);
}
@Override
public boolean update(Table table) {
return false;
}
@Override
public boolean delete(int id) {
return false;
}
@Override
public List<Table> query() {
OrmPredicates predicates =ormContext.where(Table.class).greaterThan("id",0);
List<Table> entities = ormContext.query(predicates);
return entities;
}
@Override
public Table queryById(int id) {
return null;
}
}
ServiceAbility
private MyRemote remote = new MyRemote();
private Dao dao;
class MyRemote extends RemoteObject implements IRemoteBroker{
public MyRemote() {
super("MyService_MyRemote");
}
@Override
public IRemoteObject asObject() {
return this;
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
switch (code) {
case 0: {
System.out.println("--------------0---------------");
String dataStr = data.readString();//读出字符
ZSONObject dataobj = ZSONObject.stringToZSON(dataStr);//字符转对象
//reply.writeString(dataobj.getString("name"));
Table table = new Table();
table.setName(dataobj.getString("name"));
table.setTelnum(dataobj.getString("telnum"));
table.setInfo(dataobj.getString("info"));
boolean isSuccess = dao.insert(table);
if (isSuccess) {
reply.writeString("success");
}
break;
}
case 1:{
System.out.println("--------------1---------------");
List<Table> query = dao.query();
reply.writeString(ZSONObject.toZSONString(query));
}
}
return true;
}
}
@Override
public IRemoteObject onConnect(Intent intent) {
super.onConnect(intent);
return remote.asObject();
}
@Override
public void onStart(Intent intent) {
HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
super.onStart(intent);
dao = new DaoImpl(this);
}
@Override
public void onBackground() {
super.onBackground();
HiLog.info(LABEL_LOG, "ServiceAbility::onBackground");
}
@Override
public void onStop() {
super.onStop();
HiLog.info(LABEL_LOG, "ServiceAbility::onStop");
}
@Override
public void onCommand(Intent intent, boolean restart, int startId) {
}
@Override
public void onDisconnect(Intent intent) {
}
index.js
async search(){
this.title = "读取数据"
var ad = {}
var a = {}
a.bundleName = "com.idmxm.test"
a.abilityName = ".ServiceAbility"
a.messageCode = 1
a.abilityType = 0
a.data = ad
var result = await FeatureAbility.callAbility(a)
this.todolist = JSON.parse(result);
}