APP比较简单或者APP数据库 *** 作比较少,简单的几张表,少量的数据存储。 这个时候其实没有必要去使用第三方的ORM库
需要去学习第三方ORM库,增加学习成本,也会一定程度上增加APP的复杂度 如果使用或者选择不当的话,可能影响APP整体的性能,好多ORM库其实是用运行时注解和反射来实现的,对性能会有一定的影响sqlGenerator
致力于在不使用ORM库的情况下,通过生成一些boilerplate code
方式简化数据库 *** 作,从而减轻工作量
现在sqlGenerator
已经支持根据Model的定义自动生成相应的table定义,避免编写枯燥的代码
Model定义如下
@table(name = "User")@Index(name = "name_age_index",columns = {"name","age"},unique = @H_502_45@true)@H_502_45@public @H_502_45@class User { @PrimaryKey @H_502_45@public @H_502_45@long ID; @Column(unique = @H_502_45@true,notNulL = @H_502_45@true) @H_502_45@public String name; @Serializer( serializedTypeCanonicalname = String.class,serializerCanonicalname = fileSerializer.class) @H_502_45@public file avtar; @Column(check = "age>16") @H_502_45@public @H_502_45@int age; @Column(name = "address_ID") @H_502_45@public Address address; @ForeignKey(referencetablename = "Group",referenceColumnname = "ID",action = "ON UPDATE CASCADE") @H_502_45@public @H_502_45@long groupID;}
最终会生成表的外观
类
@H_502_45@public @H_502_45@final @H_502_45@class UserContract { @H_502_45@public @H_502_45@static @H_502_45@final String table_name = "User"; @H_502_45@public @H_502_45@static @H_502_45@final String ID_ColUMN = "ID"; @H_502_45@public @H_502_45@static @H_502_45@final String name_ColUMN = "name"; @H_502_45@public @H_502_45@static @H_502_45@final String AVTAR_ColUMN = "avtar"; @H_502_45@public @H_502_45@static @H_502_45@final String AGE_ColUMN = "age"; @H_502_45@public @H_502_45@static @H_502_45@final String ADDRESS_ID_ColUMN = "address_ID"; @H_502_45@public @H_502_45@static @H_502_45@final String GROUPID_ColUMN = "groupID"; @H_502_45@public @H_502_45@static @H_502_45@final String CREATE_table_sql = "CREATE table IF NOT EXISTS User(ID INTEGER PRIMARY KEY autoINCREMENT,name TEXT NOT NulL UNIQUE,avtar TEXT,age INTEGER CHECK(age>16),address_ID TEXT,groupID INTEGER,FOREIGN KEY(groupID) REFERENCES Group(ID) ON UPDATE CASCADE)"; @H_502_45@public @H_502_45@static @H_502_45@final String CREATE_name_AGE_INDEX_sql = "CREATE INDEX name_age_index ON User(name,age)"; @H_502_45@public UserContract() { } @H_502_45@public @H_502_45@static @H_502_45@voID createtable(sqliteDatabase db) { db.execsql("CREATE table IF NOT EXISTS User(ID INTEGER PRIMARY KEY autoINCREMENT,FOREIGN KEY(groupID) REFERENCES Group(ID) ON UPDATE CASCADE)"); } @H_502_45@public @H_502_45@static @H_502_45@voID createIndex(sqliteDatabase db) { db.execsql("CREATE INDEX name_age_index ON User(name,age)"); } @H_502_45@public @H_502_45@static @H_502_45@voID droptable(sqliteDatabase db) { db.execsql("DROP table IF EXISTS User"); }}Download
buildscript { repositorIEs { mavenCentral() } dependencIEs { classpath 'com.neenbedankt.gradle.plugins:androID-apt:1.8' }}apply plugin: 'com.neenbedankt.androID-apt'dependencIEs { compile 'com.github.tubb.sqlgenerator:sqlgenerator-annotations:0.0.3-SNAPSHOT' apt 'com.github.tubb.sqlgenerator:sqlgenerator-compiler:0.0.3-SNAPSHOT' }Usage
定义好Model类之后重新构建应用,会生成以ModelnameContract命名的表外观
类,可以在modulename/build/intermediates/classes
目录下查看
./gradlew clean buildNote
sqlGenerator
还处于开发阶段,功能不是很稳定,正式项目请谨慎使用。非常希望有兴趣的朋友来一起完善这个library,这里写链接内容
以上是内存溢出为你收集整理的SQLite表‘外观’类生成器全部内容,希望文章能够帮你解决SQLite表‘外观’类生成器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)