{"id":21462296,"url":"https://github.com/espoirx/elegantdata","last_synced_at":"2025-09-01T14:44:51.009Z","repository":{"id":145931160,"uuid":"189190406","full_name":"EspoirX/ElegantData","owner":"EspoirX","description":"像操作Room一样操作 SharedPreferences 和 File 文件.","archived":false,"fork":false,"pushed_at":"2019-06-11T02:12:13.000Z","size":228,"stargazers_count":18,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-15T05:55:09.412Z","etag":null,"topics":["data","database","db","elegantdata","file","room","sharedpreferences"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EspoirX.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-05-29T09:08:28.000Z","updated_at":"2024-02-02T19:19:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"07465d29-2cd6-45e4-9725-c9d2aa1faf1f","html_url":"https://github.com/EspoirX/ElegantData","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/EspoirX/ElegantData","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EspoirX%2FElegantData","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EspoirX%2FElegantData/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EspoirX%2FElegantData/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EspoirX%2FElegantData/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EspoirX","download_url":"https://codeload.github.com/EspoirX/ElegantData/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EspoirX%2FElegantData/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273143442,"owners_count":25053123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","database","db","elegantdata","file","room","sharedpreferences"],"created_at":"2024-11-23T07:13:36.727Z","updated_at":"2025-09-01T14:44:50.997Z","avatar_url":"https://github.com/EspoirX.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ElegantData\n\n像操作Room一样操作 SharedPreferences 和 File 文件.\n\n[ ![](https://img.shields.io/badge/platform-android-green.svg) ](http://developer.android.com/index.html)\n[ ![Download](https://api.bintray.com/packages/lizixian/ElegantData/code/images/download.svg) ](https://bintray.com/lizixian/ElegantData/code/_latestVersion)\n[ ![](https://img.shields.io/badge/license-MIT-green.svg) ](http://choosealicense.com/licenses/mit/)\n\n## 相关文章 \n[掘金](https://juejin.im/post/5cf8d8faf265da1ba9156dc8)\n\n## 依赖\n```groovy\ndependencies {\n    implementation fileTree(dir: 'libs', include: ['*.jar'])\n    implementation 'com.lzx.elegantData:code:1.0.0'\n    annotationProcessor 'com.lzx.elegantData:compiler:1.0.0'\n}\n\nrepositories {\n    google()\n    jcenter()\n    maven {\n        url \"https://dl.bintray.com/lizixian/ElegantData\"\n    }\n}\n```\n\n## 使用方法\n\n#### 一、创建一个接口：\n```java\n@ElegantEntity(fileName = \"UserInfo_Preferences\")\npublic interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {\n    String keyUserName = \"\";\n}\n//或者\n@ElegantEntity(fileName = \"CacheFile.txt\", fileType = ElegantEntity.TYPE_FILE)\npublic interface FileCacheInfo extends IFileCacheInfoDao {\n    int keyPassword = 0;\n}\n```\n加上@ElegantEntity注解，并且定义 fileName 文件名，fileType 文件类型，fileType 默认为 SharedPreferences 文件。\n\n#### 二、定义一个抽象类继承 ElegantDataBase \n\n```java\n@ElegantDataMark\npublic abstract class AppDataBase extends ElegantDataBase {\n\n    public abstract SharedPreferencesInfo getSharedPreferencesInfo();\n\n    public abstract FileCacheInfo getFileCacheInfo();\n}\n```\n让这个类加上 @ElegantDataMark 注解，并且定义在第一步中定义的接口的抽象方法。\n\n然后后 ReBuild 一下，这时候会 build 报错，这时候会生成一个 IXXXDao 接口，然后让你的接口继承它，如上第一步所示。\n\n#### 三、使用单例模式去构建\n```java\n@ElegantDataMark\npublic abstract class AppDataBase extends ElegantDataBase {\n\n    public abstract SharedPreferencesInfo getSharedPreferencesInfo();\n\n    public abstract FileCacheInfo getFileCacheInfo();\n\n\n    private static AppDataBase spInstance;\n    private static AppDataBase fileInstance;\n    private static final Object sLock = new Object();\n\n    //使用SP文件\n    public static AppDataBase withSp() {\n        synchronized (sLock) {\n            if (spInstance == null) {\n                spInstance = ElegantData\n                        .preferenceBuilder(ElegantApplication.getContext(), AppDataBase.class)\n                        .build();\n            }\n            return spInstance;\n        }\n    }\n\n    //使用File文件\n    public static AppDataBase withFile() {\n        synchronized (sLock) {\n            if (fileInstance == null) {\n                String path = Environment.getExternalStorageDirectory() + \"/ElegantFolder\";\n                fileInstance = ElegantData\n                        .fileBuilder(ElegantApplication.getContext(), path, AppDataBase.class)\n                        .build();\n            }\n            return fileInstance;\n        }\n    }\n}\n```\n如果使用 SP 文件，调用 ElegantData#preferenceBuilder 方法去构建实例。  \n如果是 File 文件，则使用 ElegantData#fileBuilder 去构建。  \n两个方法都需要传入上下文和 AppDataBase 的 class。唯一不一样的是使用 File 文件需要先创建文件夹，所以在第二个参数传入的是创建文件夹的路径。\n\n#### 四、使用\n\n经过上面三个步骤后，ReBuild 一下，然后就可以开始使用了。\n\n```java\n//使用 SP 文件存入数据\nAppDataBase.withSp().getSharedPreferencesInfo().putKeyUserName(\"小明\");\n\n//使用 File 文件存入数据\nAppDataBase.withFile().getFileCacheInfo().putKeyPassword(123456789);\n\n\nString userName = AppDataBase.withSp().getSharedPreferencesInfo().getKeyUserName();\nLog.i(\"MainActivity\", \"userName = \" + userName);\n\nint password = AppDataBase.withFile().getFileCacheInfo().getKeyPassword();\nLog.i(\"MainActivity\", \"password = \" + password);\n```\n\n## @IgnoreField\n\n被 @IgnoreField 注解标记的字段，将不会被解析:\n```java\n@ElegantEntity(fileName = \"UserInfo_Preferences\")\npublic interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {\n    String keyUserName = \"\";\n    \n    @IgnoreField\n    int keyUserSex = 0;\n}\n```\nRebuild 后，keyUserSex 会被忽略，相关字段的方法不会被生成。\n\n## @NameField\n\n被 @NameField 注解标记的字段，可以重命名：\n```java\n@ElegantEntity(fileName = \"UserInfo_Preferences\")\npublic interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {\n    String keyUserName = \"\";\n    \n    @NameField(value = \"sex\")\n    int keyUserSex = 0;\n}\n```\n字段 keyUserSex 解析后生成的 put 和 get 方法是 putSex 和 getSex , 而不是 putUserSex 和 getUserSex。\n\n## @EntityClass\n@EntityClass 注解用来标注实体类，如果你需要往文件中存入实体类，那么需要加上这个注解，否则会出错。\n```java\n@ElegantEntity(fileName = \"UserInfo_Preferences\")\npublic interface SharedPreferencesInfo extends ISharedPreferencesInfoDao {\n    String keyUserName = \"\";\n\n    @EntityClass(value = SimpleJsonParser.class)\n    User user = null;\n}\n```\n如上所示，@EntityClass 注解需要传入一个 json 解析器，存入实体类的原理是把实体类通过解析器变成 json 字符串存入文件，取出来的时候\n通过解析器解析 json 字符串变成实体类。\n\n```java\npublic class SimpleJsonParser extends JsonParser\u003cUser\u003e {\n\n    private Gson mGson;\n\n    public SimpleJsonParser(Class\u003cUser\u003e clazz) {\n        super(clazz);\n        mGson = new Gson();\n    }\n\n    @Override\n    public String convertObject(User object) {\n        return mGson.toJson(object);\n    }\n\n    @Override\n    public User onParse(@NonNull String json)   {\n        return mGson.fromJson(json, User.class);\n    }\n}\n```\n\njson 解析器需要实现两个方法，convertObject 方法作用是把实体类变成 json 字符串，onParse 方法作用是把 json 字符串变成 实体类。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fespoirx%2Felegantdata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fespoirx%2Felegantdata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fespoirx%2Felegantdata/lists"}