greendao3.2 数据库增加字段

greendao3.2 数据库增加字段,第1张

greenDAO 3.2 生成的字段添加了非空约束。

字段类型为基本类型默认会添加非空约束,字段类型设置为对象类型默认不会添加非空约束,而且最终生成的sql会使用对象类型。

https://github.com/greenrobot/greenDAO/issues/17

打开属性表在d出的菜单中添加。

具体步骤:

1、选中图层,鼠标右键点击,在d出菜单中选择打开属性表。

2、点击左上角表选项,在d出的菜单中选择添加字段。

3、输入字段的名称,类型,点击确定。

4、可以看到新字段已经添加进去,可以填写需要的属性数据。

5、如果字段添加不正确,可以进行删除重新进行添加。

一.下载GreenDao

要使用肯定要先下载他的软件包了,官网上有它的连接,对于marven和gradle环境直接到serarch.maven.org上下载jar包就好了。

下载的jar导入到工程里面就可以了,通常都是/libs目录下。

上面那个下载地址下载解压后有三个文件如下图

首先我们要创建java generator工程greendao-generator-1.3.0.jar 和 freemarker-2.3.20.jar是我们创建java generator工程时生成Dao文件需要用到的(什么是我说的Dao文件,往下看就会知道)

greendao-1.3.7.jar是Android开发中需要用到的

二.创建generator工程(用来生成GreenDao开发过程中需要的java文件)

(1)创建Java工程(非Android工程) 

(2)导入greenDao-generator.jar和freemarker.jar两个包。freemarker是一个用java写的模板引擎,它能够基于模板来生成文本输出。应该就是用来自动生成DAO文件的。eclipse下面就是在properties –>Java build path –>libraries下面导入jar包。 

(3)创建一个包javagreendao 

(4)创建一个类,类名为ExampleDaoGenerator,类的定义如下:

package javagreendao

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

{

   //总之main函数就执行了下面几个函数                                                                                                                                                                                                                                             

   public static void main(String[] args) throws Exception

   {

   // 参数3是数据库版本号,“com.cn.speedchat.greendao”是包名,也就是说生成的Dao文件会在这个包下,可以将Schema理解为数据库上下文吧

        Schema schema = new Schema(3, "com.cn.speedchat.greendao")

        //addNote() addSession() addReplay()这三个函数相当于建立了三个表,表名你都可以不用管了会自动生成

       addNote(schema)     

       addSession(schema)

       addReplay(schema)

       addCustomerOrder(schema)

   //这个是生成Dao文件的路径的位置,这个代表当前工程的上一级目录的javagreendao的src-gen文件夹里面,其实就是跟src同一级目录,所以你自己要在src同一级目录下新建一个src-gen文件夹待会要生成的文件

       new DaoGenerator().generateAll(schema, "../javagreendao/src-gen")   

   }

     //这个是一个Note表,然后后面的node.add***是表的字段名以及属性                                                                                                                                                                                          

   private static void addNote(Schema schema)                

   {

       //"MqttChatEntity"相当于是表的类名,用MqttChatEntity生成对象就可以访问这个表属性了,也就是这个表对应了这个类,待会使用你就会明白了

       Entity note = schema.addEntity("MqttChatEntity")   

       note.addIdProperty().autoincrement()

       note.addIntProperty("mode").notNull()

       note.addStringProperty("sessionid").notNull()

       note.addStringProperty("from").notNull()

       note.addStringProperty("to").notNull()

       note.addStringProperty("v_code")

       note.addStringProperty("timestamp").notNull()

       note.addStringProperty("platform")

       note.addStringProperty("message")

       note.addBooleanProperty("isread").notNull()

       note.addLongProperty("gossipid")

       note.addStringProperty("gossip")

       note.addIntProperty("chattype").notNull()

       note.addStringProperty("imagepath")

       note.addStringProperty("base64image")

   }

   //这个是一个Session表,然后后面的node.add***是表的字段名以及属性(这是我写的会话的一个表)

   private static void addSession(Schema schema)    

   {

       Entity note = schema.addEntity("SessionEntity")

       note.addIdProperty().autoincrement()

       note.addStringProperty("sessionid").notNull().unique()

       note.addStringProperty("from").notNull()

       note.addStringProperty("to").notNull()

       note.addLongProperty("gossipid").notNull()

       note.addStringProperty("gossip")

       note.addIntProperty("sessiontype").notNull()

       note.addBooleanProperty("asdasd").notNull()

   }

 //这个是一个Replay表,然后后面的node.add***是表的字段名以及属性(这是我写的回复的一个表)

   private static void addReplay(Schema schema)

   {

       //ReplayEntity对应的类名

       Entity note = schema.addEntity("ReplayEntity")   

       note.addIdProperty().autoincrement()

       note.addIntProperty("mode").notNull()

       note.addStringProperty("from").notNull()

       note.addStringProperty("to").notNull()

       note.addStringProperty("v_code")

       note.addStringProperty("timestamp").notNull()

       note.addStringProperty("platform")

       note.addStringProperty("message")

       note.addIntProperty("msgtype").notNull()

       note.addBooleanProperty("isread").notNull()

   }

   //这个不用管了,照抄吧

   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)

   }                                                                                                                                                                                                                                                                                                              

}

1)增加表如果你自己想加一些其他的表的话,你可以自己按照addSession,addNote ,addReplay三个函数的方式加,类名、字段名可以自己随便取比如说,比我我要加一个用户表,字段包括name,age,sex三个,我可以这样做

 private static void addUser(Schema schema)  

   {

       Entity note = schema.addEntity("UserEntity")   

       note.addIdProperty().autoincrement()

       note.addStringProperty("name").notNull()

       note.addIntProperty("age").notNull()

       //true代表男,false代表女

       note.addBooleanProperty("sex").notNull()   

   }

然后在main函数里面做如下调用

   public static void main(String[] args) throws Exception

   {

       Schema schema = new Schema(3, "com.cn.speedchat.greendao")   

       addNote(schema)       

       addSession(schema)

       addReplay(schema)

       addUser(schema)

       addCustomerOrder(schema)

       new DaoGenerator().generateAll(schema, "../javagreendao/src-gen")

   }

2)删除表当然一些不需要的表你可以不用,删掉就行,比如说你不须要addReplay,你就在main函数里面别调用addReplay(schema)就行

总之呢,这就是一个基于greenDao-generator.jar和freemarker.jar两个包的java工程

然后运行该工程,控制台打印出如下结果:

greenDAO Generator Copyright 2011-2013 Markus Junginger,

greenrobot.de. Licensed under GPL V3. This program comes with

ABSOLUTELY NO WARRANTY Processing schema version 3… Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/MqttChatEntityDao.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/MqttChatEntity.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/SessionEntityDao.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/SessionEntity.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/ReplayEntityDao.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/ReplayEntity.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/CustomerDao.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/Customer.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/OrderDao.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/Order.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/DaoMaster.java

Written

/home/csm/workspace/javagreendao/src-gen/com/cn/speedchat/greendao/DaoSession.java

Processed 5 entities in 189ms

这代表成功的生成了Dao文件,然后我们按F5刷新该工程,在查看src-gen目录文件,自动生成了很多java文件,这就是我们要的,我这里截图给大家看

但是有很多错误是不是,没关系,这个工程识别不了这些文件,这些文件是基于greendao-1.3.7.jar包的,是Android工程里面要用到的。先不管这个java工程了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11572291.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存