android greenDao(二)
来源:互联网 发布:腾讯云主机 绑定域名 编辑:程序博客网 时间:2024/05/18 02:02
上一篇介绍了如何建立类生成工程,现在介绍如何使用。
以下是ExampleDaoGenerator工程代码,做了一些修改
- /*
- * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package de.greenrobot.daogenerator.gentest;
- import de.greenrobot.daogenerator.DaoGenerator;
- import de.greenrobot.daogenerator.Entity;
- import de.greenrobot.daogenerator.Property;
- import de.greenrobot.daogenerator.Schema;
- import de.greenrobot.daogenerator.ToMany;
- /**
- * Generates entities and DAOs for the example project DaoExample.
- *
- * Run it as a Java application (not Android).
- *
- * @author Markus
- */
- public class ExampleDaoGenerator {
- public static void main(String[] args) throws Exception {
- //该方法第一个参数用来更新数据库版本号,第二个参数为要生成的DAO类所在包路径。
- Schema schema = new Schema(1, "de.greenrobot.daoexample");
- //然后进行建表
- addNote(schema);
- //addCustomerOrder(schema);
- //设置要生成DAO文件的目标工程的项目路径,其中src-gen这个目录名需要在运行前手动创建,否则会报错
- new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
- }
- private static void addNote(Schema schema) {
- /**
- * 在使用greenDAO时,一个实体类只能对应一个表,目前没法做到一个表对应多个实体类,或者多个表共用一种对象类型。
- * 后续的升级也不会针对这一点进行扩展。
- * */
- Entity note = schema.addEntity("Note");//创建表
- //note.addIdProperty(); //增加ID列
- note.addIdProperty().primaryKey().autoincrement();//设置一个自增长ID列为主键:
- note.addStringProperty("text").notNull();//创建非空的列
- note.addStringProperty("comment").unique();//创建唯一的
- //note.addDateProperty("date");
- }
- private static void addCustomerOrder(Schema schema) {
- Entity customer = schema.addEntity("Customer");
- customer.addIdProperty();
- customer.addStringProperty("name").notNull();
- Entity order = schema.addEntity("Order");
- order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
- order.addIdProperty();
- Property orderDate = order.addDateProperty("date").getProperty();
- Property customerId = order.addLongProperty("customerId").notNull().getProperty();
- order.addToOne(customer, customerId);
- ToMany customerToOrders = customer.addToMany(order, customerId);
- customerToOrders.setName("orders");
- customerToOrders.orderAsc(orderDate);
- }
- }
上面代码主要有两个
Schema schema = new Schema(1, "de.greenrobot.daoexample");
第一个参数为版本号,第二个参数为产生的类放在引用工程的那个地方,需要结合下面代码
new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
那么产生的文件将放到
ExampleDaoGenerator工程目录的上上两级目录下的 HelloWorld/src-gen/de/greenrobot/daoexample 目录下。
1、我们在ExampleDaoGenerator工程下,按下键盘的 ctrl+F11 则会自动生成四个java类放到HelloWorld/src-gen/de/greenrobot/daoexample目录下面
2、把这4个类依赖的相关类放到一起,依赖的类直接把 "greenDAO-master\DaoCore\src\de"目录下的内容放到一起4个类一起。
3、将src-gen目录设置为编译文件夹
4、根据生成类增加相关代码,这里只贴相关用到的代码
- private DaoMaster daoMaster;
- private DaoSession daoSession;
- private NoteDao noteDao;
- private Cursor cursor;
- private SQLiteDatabase db;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- DevOpenHelper helper = new DaoMaster.DevOpenHelper(MainActivity.this, "Note", null);
- db = helper.getWritableDatabase();
- daoMaster = new DaoMaster(db);
- daoSession = daoMaster.newSession();
- noteDao = daoSession.getNoteDao();
- }
- final Button btn6 = (Button) findViewById(R.id.button6);
- btn6.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "长度 :" + noteDao.count(),
- Toast.LENGTH_SHORT).show();
- Note note = new Note(null, "noteText1", "comment1");
- try {
- noteDao.insert(note);
- } catch (Exception e) {
- Toast.makeText(MainActivity.this, "数据重复 :" + noteDao.count(),
- Toast.LENGTH_SHORT).show();
- }
5、按ctrl+shift+o自动补齐相关类
6、下载APK到手机,即可。
补充:
在调试过程中,修改了生成工程的类之后发现,重新编译helloworld工程有出现代码异常的情况 代码位于"db = helper.getWritableDatabase();"
该问题暂时未知道什么原因。但有方法解决:在手机端-->在设置界面-->选择对应的应用(对于我来说,是helloworld这个应用)-->清除数据
- android greenDao(二)
- Android greenDao 数据库的使用(二)
- Android ORM 框架之GreenDAO(二) 进阶
- android greenDAO
- android greenDAO
- Android greenDao 使用教程(二) 在Eclipse 下编译使用greenDao
- Android ORM-GreenDao学习二之进阶篇
- Android ORM数据库框架之-greenDao(二)
- GreenDAO 3.x官方文档 — Android ORM框架(二)
- Android ORM框架介绍之greenDao封装(二)
- GreenDAO 3.x官方文档 — Android ORM框架(二)
- Android数据库框架greenDao学习笔记(二)---注解
- Android框架学习之GreenDao(二)数据库升级
- Android GreenDao使用(二)增删改查
- GreenDao常用操作(二)
- GreenDao 使用教程 二
- GreenDao进阶篇(二)
- greenDao orm 进阶 <二>
- easyui列表查询报错:Cannot read property 'length' of undefined
- BZOJ 4010: [HNOI2015]菜肴制作|拓扑排序贪心
- 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的区别
- CSS学习摘记
- 第二章Spring资源和配置开发环境-第一节spring资源网址 spring官网 eclipse插件 Spring jar包下载
- android greenDao(二)
- Ambiguous source files
- 时间戳转时间
- javaEE之------ApectJ的切面技术===标签
- oracle 查询杀死死锁方案
- SQL Server执行计划的理解
- C++ Primer Plus(第六版)读书笔记(二)
- 【Java核心技术学习】Java数据类型
- adapter中操作listview之外的View 注意事项