build.gradle裏添加
compileOptions{annotationEnabled true}
dao
dao/dao
public interface NodeDao {
boolean insert(NodeEntity entity);
boolean update(NodeEntity entity);
boolean delete(int id);
List<NodeEntity> query();
NodeEntity queryNodeById(int id);
}
dao/daoImpl
public class NodeDaoImpl implements NodeDao {
@Override
public boolean insert(NodeEntity entity) {
ormContext.insert(entity);
return ormContext.flush();
}
private OrmContext ormContext;
public NodeDaoImpl(Context context) {
DatabaseHelper helper = new DatabaseHelper(context);
ormContext = helper.getOrmContext("NodeDB","node.db", NodeDatabase.class);
}
@Override
public boolean update(NodeEntity entity) {
OrmPredicates predicates =ormContext.where(NodeEntity.class).equalTo("id",entity.getId());
List<NodeEntity> entities = ormContext.query(predicates);
NodeEntity updateEntity = entities.get(0);
updateEntity.setTitle(entity.getTitle());
updateEntity.setContent(entity.getContent());
updateEntity.setDate(entity.getDate());
ormContext.update(updateEntity);
return ormContext.flush();
}
@Override
public boolean delete(int id) {
OrmPredicates predicates =ormContext.where(NodeEntity.class).equalTo("id",id);
List<NodeEntity> entities = ormContext.query(predicates);
ormContext.delete(entities.get(0));
return ormContext.flush();
}
@Override
public List<NodeEntity> query() {
OrmPredicates predicates =ormContext.where(NodeEntity.class).greaterThan("id",0);
List<NodeEntity> entities = ormContext.query(predicates);
return entities;
}
@Override
public NodeEntity queryNodeById(int id) {
OrmPredicates predicates =ormContext.where(NodeEntity.class).equalTo("id",id);
List<NodeEntity> entities = ormContext.query(predicates);
return entities.get(0);
}
db/數據庫
@Database(entities = {NodeEntity.class},version = 1)
public abstract class NodeDatabase extends OrmDatabase {}
entity/
@Entity(tableName = "nodes")
public class NodeEntity extends OrmObject {
@PrimaryKey(autoGenerate = true)
private Integer id;
private String title;
private String content;
private long date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
}
private NodeDao dao;private Text text;@Overridepublic void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); dao = new NodeDaoImpl(this); text = (Text) findComponentById(ResourceTable.Id_text_helloworld); text.setClickedListener(component -> { NodeEntity nodeEntity = new NodeEntity(); nodeEntity.setTitle("這是一個標題idmxm"); nodeEntity.setContent("這是一個內容idmxm"); nodeEntity.setDate(System.currentTimeMillis()); boolean isSuccess = dao.insert(nodeEntity); if(isSuccess){ new ToastDialog(this).setText("新建記錄成功!").show(); }else { new ToastDialog(this).setText("新建記錄失敗!").show(); } }); findComponentById(ResourceTable.Id_button).setClickedListener(component -> { List<NodeEntity> query = dao.query(); System.out.println("----------------------------------"); System.out.println(query.toArray().length); text.append(""+query.size()); System.out.println(query.get(0).getTitle()); });}