-
GreenDao
2020-08-11 09:05:59GreenDao 根目录 buildscript { repositories { google() jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'org.greenrobot:...GreenDao
根目录
buildscript { repositories { google() jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
项目
apply plugin: 'org.greenrobot.greendao' // apply plugin android { ... } greendao { schemaVersion 1 daoPackage 'com.ping.greendao.gen' targetGenDir 'src/main/java' } dependencies { implementation 'org.greenrobot:greendao:3.2.2' // add library }
创建bean @Entity public class Bean { @Id(autoincrement = true)//设置自增长 Long id; String name; }
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MainActivity.this, "xia.db"); SQLiteDatabase db = devOpenHelper.getWritableDatabase(); XiaBeanDao xiaBeanDao = new DaoMaster(db).newSession().getXiaBeanDao(); xiaBeanDao.insert(new XiaBean(1, "男", "AA")); //添加 List<XiaBean> xiaBeans = xiaBeanDao.loadAll(); //获取全部 for (XiaBean xiaBean : xiaBeans) { Log.i("liuxuan", "onCreate: "+xiaBean.getId()); } xiaBeanDao.deleteAll(); //删除全部
-
greenDao
2019-03-14 20:14:48classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' //app gradle apply plugin: 'org.greenrobot.greendao' greendao { schemaVersion 1 //数据库版本号 daoPackage '包名.database' //设置时生成代...依赖 //根gradle classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' //app gradle apply plugin: 'org.greenrobot.greendao' greendao { schemaVersion 1 //数据库版本号 daoPackage '包名.database' //设置时生成代码的目录 targetGenDir 'src/main/java' //设置DaoMaster、DaoSession、Dao目录 } dependencies { compile 'org.greenrobot:greendao:3.2.2' }
@Entity:用于标识当前实体需要GreenDao生成代码。
schema:项目中存在多个Schema时,表明当前实体属于哪个Schema。
active:标记实体是否处于活动状态,活动状态才支持更新删除刷新等操作。
nameInDb:存储在数据库中的表名,不写默认与类名一致。
createInDb:标记创建数据库表,若有多个实体关联此表可设为false避免重复创建,默认为true。
属性注解
@Id :主键Long型,可以通过@Id(autoincrement = true)设置自增长。
@Property:设置一个非默认关系映射所对应的列名,默认是的使用字段名例如@Property (nameInDb="name")。
@NotNul:设置数据库表当前列不能为空。
@OrderBy:指定排序。
@Transient:添加此标记之后不会生成数据库表的列。
@Generated:为build之后GreenDao自动生成的注解,为防止重复,每一块代码生成后会加个hash作为标记。
private DaoSession daoSession; private PersonDao personDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); daoSession = DaoManager.getDefault(this).daoSession(); personDao = daoSession.getPersonDao(); //添加 for (int i = 0; i < 10; i++) { personDao.insert(new Person(null, "wang" + i, i)); } } public void onClick(View view) { //查询 List<Person> list = personDao.queryBuilder().build() .list(); fore(list); //修改 Person unique = personDao.queryBuilder() .where(PersonDao.Properties.Id.eq(2)) .build().unique(); unique.setName("tao"); personDao.update(unique); //条件查询 List<Person> list1 = personDao.queryBuilder() .where(PersonDao.Properties.Age.gt(10),PersonDao.Properties.Id.lt(18)) .build() .list(); fore(list1); //删除 personDao.deleteByKey(unique.getId()); } public void fore(List<Person> list) { for (Person person : list) { Log.e("tag",person.toString()); } }
DaoManager
package com.example.zsd.myapplications0314.utils; import android.content.Context; import com.example.zsd.myapplications0314.greendao.DaoMaster; import com.example.zsd.myapplications0314.greendao.DaoSession; public class DaoManager { private final DaoSession daoSession; private static DaoManager daoManager; private DaoManager (Context context){ daoSession = DaoMaster.newDevSession(context, "my.db"); } public static DaoManager getIntance(Context context){ if (daoManager == null) { synchronized (DaoManager.class){ daoManager = new DaoManager(context); } }else { return daoManager; } return daoManager; } public DaoSession getDaoSession() { return daoSession; } }
-
greendao
2019-07-18 13:44:111.添加依赖 ...compile 'org.greenrobot:greendao:3.2.2' // add library compile 'org.greenrobot:greendao-generator:3.2.2' 这句话是在上边写的 小渣渣啊→_→ apply plugin: 'org.gree...1.添加依赖
在Model 下 bulid.gradle文件下的dependencies下添加所需依赖
compile 'org.greenrobot:greendao:3.2.2' // add library compile 'org.greenrobot:greendao-generator:3.2.2'
这句话是在上边写的 小渣渣啊→_→
apply plugin: 'org.greenrobot.greendao'
2.在project bulid.gradle下进行配置
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'//greenDao } }
3.在Model 下 bulid.gradle文件下的android在对greendao的generator生成文件进行配置
greendao { schemaVersion 1 //版本 daoPackage '生成文件包名' // 一般为app包名+生成文件的文件夹名 targetGenDir 'src/main/java' //生成文件路径 }
例如:
4.创建实体类
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = “NAME”)
private String name;注意:编写完实体类以后在实体类界面下按下Ctrl+F9(Make project),程序会自动编译生成dao文件,生成的文件一共有三个。
5.使用Greendao
(1)创建一个application类,在application中完成DaoSession的初始化,避免以后重复初始化,便于使用。public class MyApplication extends Application { private static MyApplication App; private DaoMaster.DevOpenHelper data; private SQLiteDatabase writableDatabase; private DaoMaster daoMaster; private DaoSession daoSession; @Override public void onCreate() { super.onCreate(); App = this; Fresco.initialize(this); NetWrokProwor.getNetWrokProwor(this); setDataBase(); } public static MyApplication getInstance(){ return App; } private void setDataBase() { //通过DaoMaster的内部类DevOpenHelper data = new DaoMaster.DevOpenHelper(this, "data", null); writableDatabase = data.getWritableDatabase(); daoMaster = new DaoMaster(writableDatabase); daoSession = daoMaster.newSession(); } public SQLiteDatabase getWritableDatabase() { return writableDatabase; } public DaoSession getDaoSession() { return daoSession; } }
6.bean类(参数是自己要存入地方的参数)
@Entity public class GreenDao { @Id(autoincrement = true) private Long id; private int CommentId; private String num; @Generated(hash = 1121101909) public GreenDao(Long id, int CommentId, String num) { this.id = id; this.CommentId = CommentId; this.num = num; } @Generated(hash = 766040118) public GreenDao() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public int getCommentId() { return this.CommentId; } public void setCommentId(int CommentId) { this.CommentId = CommentId; } public String getNum() { return this.num; } public void setNum(String num) { this.num = num; } }
7.主页面
public class MainActivity extends AppCompatActivity implements View_MianActivity{ private ExpandableListView MyExpandableListView; private CheckBox quanxuancheck; private TextView allprice; private Button jiesuan; private Presenter presenter; private ExAdapter exAdapter; private GreenDaoDao greenDaoDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); greenDaoDao = MyApplication.getInstance().getDaoSession().getGreenDaoDao(); boolean netWrokProwor = NetWrokProwor.getNetWrokProwor(this); if(netWrokProwor){ initData(); }else{ Toast.makeText(this, "请检查网络", Toast.LENGTH_SHORT).show(); } if(!EventBus.getDefault().isRegistered(true)){ EventBus.getDefault().register(this); } } private void initView() { MyExpandableListView = findViewById(R.id.MyExpandableListView); quanxuancheck = findViewById(R.id.quanxuancheck); allprice = findViewById(R.id.allprice); jiesuan = findViewById(R.id.jiesuan); quanxuancheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取当前条目状态 boolean allcheckbox = exAdapter.isAllcheckbox(); //给条目复制 exAdapter.isAllCheckBox(!allcheckbox); //刷新 exAdapter.notifyDataSetChanged(); } }); } private void initData() { presenter = new Presenter(this); Map<String, String> map = new HashMap<>(); map.put("uid","71"); presenter.request(map); } @Override public void onSuccess(List<GoodsBean.DataBean> data) { for(int i = 0 ; i < data.size() ; i++){ GoodsBean.DataBean dataBean = data.get(i); List<GoodsBean.DataBean.ListBean> list = dataBean.getList(); for (int j = 0 ; j < list.size() ; j++){ GoodsBean.DataBean.ListBean listBean = list.get(j); GreenDao greenDao = new GreenDao(null, listBean.getPscid(), listBean.getNum() + ""); greenDaoDao.insert(greenDao); } } exAdapter = new ExAdapter(data,this); MyExpandableListView.setAdapter(exAdapter); List<GreenDao> greenDaos = greenDaoDao.loadAll(); for (int i = 0 ; i < greenDaos.size() ; i++){ GreenDao greenDao = greenDaos.get(i); int commentId = greenDao.getCommentId(); String num = greenDao.getNum(); Log.e("lsq","商品id:"+commentId+"----商品数量:"+num); } } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void onMessagesEvent(CountPrice countPrice){ allprice.setText(countPrice.getPrice()+""); jiesuan.setText("去结算("+countPrice.getCount()+")"); } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void onMessagesEvent(MessageEvent messageEvent){ quanxuancheck.setChecked(messageEvent.isCheck()); } @Override protected void onDestroy() { super.onDestroy(); if(EventBus.getDefault().isRegistered(true)){ EventBus.getDefault().unregister(this); } if(presenter != null){ presenter = null; } } }
-
Flutter的setState更新原理和流程
-
mtk-openwrt-sdk-20170518-1443366e.tar.xz
-
PPT大神之路高清教程
-
投标方法论
-
微信程序-源码
-
基于Qt的LibVLC开发教程
-
C++总结
-
救救孩子
-
小白IP代理工具
-
【硬核】一线Python程序员实战经验分享(1)
-
libusb-rs:libusb的安全Rust封装器-源码
-
由浅入深分析阻塞队列
-
Apache.NMS-1.5.1-bin.zip
-
C语言零基础入门(详细讲解)
-
access应用的3个开发实例
-
MySQL 多实例安装 及配置主从复制实验环境
-
用微服务spring cloud架构打造物联网云平台
-
放映时间:使用简单的CLI观看电视节目和电影-源码
-
纯css制作3D旋转照片墙
-
背景生成器-源码