Github Project address:yun_dao

Introduction to the

An annotated ORM database solution based on source_gen

use

Dart = * _entity.table.dart = *_entity.dart = * _entity.table.dart

Dart Your entity class file: student_entity.dart The compiled database operation class file: student_entity.dao.dartCopy the code

2. Annotate your Entity class with @entity and use the nameInDb property to define the name of its table in the database. PropertyList is of type List

Ex. :

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

3. The entity class must have a primary key and declare this Property as a primary key in the @Property annotation with isPrimary=true

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

To generate a database operation file, enter the following command flutter packages pub run build_runner build

5. The database operation file generated after compilation contains the creation, increase, deletion, change and check of the current table, etc., which needs to be initialized first in the project

/// Import the database management class
import 'package:yun_dao/db_manager.dart';

// initialize the database by passing in the database version, database path, and database name
DBManager dBManager = DBManager();
dBManager.initByPath(1DbPath ",""dbName");
/// You can also initialize the database using the default path getDatabasesPath()
dBManager.init(1."dbName");
Copy the code

6. Call the init() method of the generated database operation file in the project to create the table, init() method will make the corresponding judgment will not create the table repeatedly

StudentEntityDao.init();
Copy the code

7. Then you can add, delete, change and check the database conveniently in the project. All the database operation methods are static

StudentEntityTable.queryAll();
StudentEntityTable.insert(StudentEntity());
Copy the code

8. You can also query data by constructing queriers

List list = await StudentEntityDao.queryBuild()
        .where(StudentEntityDao.NAME.equal("Bill"))
        .where(StudentEntityDao.AGE.equal(2))
        .list();
Copy the code