对象到数据库映射
Pod
pod 'SJDBMap' (请执行“pod update”操作)
描述
根据模型自动创建与该类相关的表(多个表),可以进行增删改查。当类添加了新的属性的时候,会自动更新相关的表字段。Automatically create tables based on the model. To achieve additions and deletions. When the class adds a new attribute, it will automatically update the relevant table field.
数据库依据模型进行存储,因此存储的目标需要是对象。例如直接存储数组是无法存储的,需要在外层包一个类,将数组作为这个类的属性。示例如下
The database is stored on the basis of the model, so the stored target needs to be an object. For example, a array can not be stored, a class is needed in the outer layer, and the array is used as an property for this class.
@interface ClassA : NSObject
@end
@interface Example : NSObject
@property(nonatomic, assign) NSInteger ID;
@property(nonatomic, strong) NSArray<ClassA *> *arr;
@end
Example *obj = [Example new];
obj.ID = 111;
obj.arr = @[A1, A2, A3];
[[SJDatabaseMap sharedServer] insertOrUpdateDataWithModel:obj callBlock:nil];
insertOrUpdate 插入数据或更新数据
在将数据插入到表中之前,会检测是否存在相关表。如果不存在,会首先创建相关表(可能创建多个表),然后再进行数据的更新或插入。如果类中新增了属性,会自动检测并更新相关表的字段。
在数据被插入到表之前,它会检测相关表是否存在。如果不存在,将首先创建相关表(可能创建多个表),然后更新或插入数据。如果类中添加了新的属性,将自动检测并更新相关表字段。
- (void)insertOrUpdate {
Person *sj = [Person new];
sj.personID = 0;
sj.name = @"sj";
sj.tags = @[[PersonTag tagWithID:0 des:@"A"],
[PersonTag tagWithID:1 des:@"B"],
[PersonTag tagWithID:2 des:@"C"],
[PersonTag tagWithID:3 des:@"D"],
[PersonTag tagWithID:4 des:@"E"],];
[[SJDBMap sharedServer] insertOrUpdateDataWithModel:sj callBlock:^(BOOL result) {
// ....
}];
}
- (void)update {
[[SJDatabaseMap sharedServer] update:person property:@[@"tags", @"age"] callBlock:^(BOOL result) {
// ....
}];
[[SJDatabaseMap sharedServer] update:person insertedOrUpdatedValues:@{@"tags":insertedValues} callBlock:^(BOOL r) {
// ....
}];
}
delete 删除
删除数据是指删除该类对应的表中的数据,与其关联的其他类的数据没有进行处理。
删除数据是指删除该类对应的表中的数据,与之关联的其他类的数据没有进行处理。
- (void)del {
[[SJDBMap sharedServer] deleteDataWithClass:[Person class] primaryValue:0 callBlock:^(BOOL result) {
// ...
}];
[[SJDatabaseMap sharedServer] deleteDataWithClass:[Person class] primaryValues:@[@(1), @(0)] callBlock:^(BOOL r) {
// ...
}];
[[SJDatabaseMap sharedServer] deleteDataWithModels:personModels callBlock:^(BOOL result) {
// ...
}];
}
query 查询
查询数据会将与该类相关的所有数据读取出来,并转换成相应的模型。
查询数据将会读取与该类相关的所有数据,并将其转换为相应的模型。
- (void)query {
[[SJDBMap sharedServer] queryAllDataWithClass:[Person class] completeCallBlock:^(NSArray<id> * _Nonnull data) {
// ...
}];
[[SJDatabaseMap sharedServer] queryDataWithClass:[Person class] primaryValue:12 completeCallBlock:^(id<SJDBMapUseProtocol> _Nullable model) {
// ...
}];
[[SJDatabaseMap sharedServer] queryDataWithClass:[Person class] queryDict:@{@"name":@"sj", @"age":@(20)} completeCallBlock:^(NSArray<id<SJDBMapUseProtocol>> * _Nullable data) {
// ...
}];
[[SJDatabaseMap sharedServer] queryDataWithClass:[Person class] range:NSMakeRange(2, 10) completeCallBlock:^(NSArray<id<SJDBMapUseProtocol>> * _Nullable data) {
// ...
}];
}
// 模糊查询
- (void)fuzzyQuery {
// 匹配以 's' 开头的name.
[[SJDatabaseMap sharedServer] fuzzyQueryDataWithClass:[Person class] queryDict:@{@"name":@"s"} match:SJDatabaseMapFuzzyMatchFront completeCallBlock:^(NSArray<id<SJDBMapUseProtocol>> * _Nullable data) {
// ...
}];
* 匹配左右两边
* ...A...
*/
SJDatabaseMapFuzzyMatchAll = 0,
/*!
* 匹配以什么开头
* ABC.....
*/
SJDatabaseMapFuzzyMatchFront,
/*!
* 匹配以什么结尾
* ...DEF
*/
SJDatabaseMapFuzzyMatchLater
}
Use
实现协议方法。
实现 SJDBMapUseProtocol 方法。
@interface SampleVideoModel : NSObject<SJDBMapUseProtocol>
@property (nonatomic, assign) NSInteger videoId;
@property (nonatomic, strong) NSArray<SampleVideoTag *> *tags;
@property (nonatomic, strong) NSArray<SampleUser *> *likedUsers;
@property (nonatomic, strong) SampleOrgan *organ;
@end
@implementation SampleVideoModel
+ (NSString *)primaryKey {
return @"videoId";
}
// model
+ (NSDictionary<NSString *,NSString *> *)correspondingKeys {
return @{
@"organ":@"code",
};
}
// arr
+ (NSDictionary<NSString *,Class> *)arrayCorrespondingKeys {
return @{
@"tags":[SampleVideoTag class],
@"likedUsers":[SampleUser class],
};
}
@end
Use attention 使用注意
模型需要一个主键或自增主键
模型需要主键或自增主键。