flutter的 ORM数据库框架

网友投稿 1310 2022-10-19 07:21:01

flutter的 ORM数据库框架

中文版

Introduction

It is an ORM database solution by annotation method which is based on source_gen

Usage

1.Create entity file which names *_entity.dart,then will generate database operation file which names *.entity.dao.dart after compilation.

entity file: student_entity.dartgenerated databse file : student_entity.dao.dart

2.Use @Entity to annotate entity class; use nameInDb property to define the database table name of entity class; use propertyList to define the columns name in table which must be correspond to the property name of the entity class.And the type of propertyList is List

@Entity(nameInDb:'student',propertyList:[Property(name:'name',type:PropertyType.STRING)])class StudentEntity{ String name;}

3.Entity class must has primary key, and define the property as primary key by the code isPrimary=true in @Property annotation

@Entity(nameInDb:'student', propertyList:[ Property(name:'name',type:PropertyType.STRING), Property(name:'id',type:PropertyType.INT,isPrimary:true), ])class StudentEntity{ String name; int id;}

4.Using the command to create database operation file in the command-line:

flutter packages pub run build_runner build

5.The gennereated databse operation file includes the CURD methods after compilation.And you should init database when use in project

///import database manager classimport 'package:yun_dao/db_manager.dart';///pass the params include vesion,path,name to init databse. DBManager is a singletonDBManager dBManager = DBManager();dBManager.initByPath(1,“dbPath”,"dbName");///you can also use default path to init database.The method to get default path is `getDatabasesPath()`dBManager.init(1,"dbName");

6.Call the method init() of genearated database operation file to create a table in project.However,the method would judge whether create a new table.Then you can don't be afraid of creating repeated tables.

StudentEntityDao.init();

7.Next,it's convenient to execute CURD operation in databse.And the all operation methods is static.

StudentEntityTable.queryAll();StudentEntityTable.insert(StudentEntity());

8.Also you can select data by query builder

List list = await StudentEntityDao.queryBuild() .where(StudentEntityDao.NAME.equal("李四")) .where(StudentEntityDao.AGE.equal(2)) .list();

Install

dev_dependencies: yun_dao: 0.0.4

Example

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:记一次springboot中用undertow的坑
下一篇:后端接口性能差,该从哪些方面进行优化?
相关文章