orm数据库
2021/7/19 19:11:52
本文主要是介绍orm数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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; } }
这篇关于orm数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10百万架构师第十三课:源码分析:Spring 源码分析:Spring核心IOC容器及依赖注入原理|JavaGuide
- 2025-01-10便捷好用的电商API工具合集
- 2025-01-09必试!帮 J 人团队解决物流错发漏发的软件神器!
- 2025-01-09不容小觑!助力 J 人物流客服安抚情绪的软件!
- 2025-01-09为什么医疗团队协作离不开智能文档工具?
- 2025-01-09惊叹:J 人团队用啥软件让物流服务快又准?
- 2025-01-09如何利用数据分析工具优化项目资源分配?4种工具推荐
- 2025-01-09多学科协作难?这款文档工具可以帮你省心省力
- 2025-01-09团队中的技术项目经理TPM:工作内容与资源优化策略
- 2025-01-09JIT生产管理法:优化流程,提升竞争力的秘诀