Android ORM框架介绍之greenDao封装(二)

时间:2022-02-06 15:58:36

Android ORM框架介绍之greenDao封装(二)

数据库操作增删改查操作重复太高,所以需要进行抽取,通过DataBaseManager统一管理。

package www.weshared.greendao;

import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;
import com.zhang.db.dao.DaoMaster;
import com.zhang.db.dao.DaoSession;
import com.zhang.db.entity.BaseModel;
import com.zhang.db.entity.Model;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.query.WhereCondition;
import java.util.List;

public class DataBaseManager<T> {

protected static final String DB_NAME = "my.db";
protected final DaoSession session;
protected final AbstractDao dao;

private static final String DB_NAME = "my.db";
private DaoSession session;
private AbstractDao dao;
private DaoMaster.DevOpenHelper helper;
private DaoMaster master;

private static volatile DataBaseManager manager;

private DataBaseManager(Context context) {
if (helper == null) {
helper = new DaoMaster.DevOpenHelper(context, DB_NAME);
}
}

public static DataBaseManager getInstance(Context context) {
if (manager == null) {
synchronized (DataBaseManager.class) {
if (manager == null) {
manager = new DataBaseManager(context);
}
}
}
return manager;
}

//未加密
public DataBaseManager create(T t) {
if (helper != null) {
master = new DaoMaster(helper.getWritableDb());
}
session = master.newSession();
dao = createDao(session, t.getClass());
return manager;
}

//加密 [需要依赖 compile 'net.zetetic:android-database-sqlcipher:3.5.3']
public DataBaseManager create(String pwd,T t) {
if (helper != null) {
master = new DaoMaster(helper.getEncryptedWritableDb(pwd));
}
session = master.newSession();
dao = createDao(session, t.getClass());
return manager;
}
//如果Model类太多,使用工厂模式
public AbstractDao createDao(@NonNull DaoSession session, Class<?> clazz) {
if (BaseModel.class.equals(clazz)) {
return session.getBaseDao();
} else if (Model.class.equals(clazz)) {
return session.getModelDao();
}
return null;
}

public long insert(T t) {
return session.insert(t);
}

public long insertOrReplace(T t) {
return session.insertOrReplace(t);
}

@SuppressWarnings("unchecked")
public void insertAll(List<T> list) {
dao.insertInTx(list);
}

@SuppressWarnings("unchecked")
public void insertOrReplaceAll(List<T> list) {
dao.insertOrReplaceInTx(list);
}

public void update(T t) {
session.update(t);
}

@SuppressWarnings("unchecked")
public void updateAll(List<T> list) {
dao.updateInTx(list);
}

public void delete(T t) {
session.delete(t);
}

@SuppressWarnings("unchecked")
public void deleteAll(List<T> list) {
dao.deleteInTx(list);
}

public void deleteAll() {
dao.deleteAll();
}

@SuppressWarnings("unchecked")
public List<T> queryAll() {
return dao.loadAll();
}

@SuppressWarnings("unchecked")
public T query(long id) {
return (T) dao.loadByRowId(id);
}

@SuppressWarnings("unchecked")
public T query(Object key) {
return (T) dao.load(key);
}

/**
* 简单的条件查询
*/

@SuppressWarnings("unchecked")
public List<T> query(String where, String... args) {
return dao.queryRaw(where, args);
}

/**
* 简单的条件查询
*/

@SuppressWarnings("unchecked")
public List<T> queryBuild1(WhereCondition condition, WhereCondition... conditions) {
return dao.queryBuilder().where(condition, conditions).build().list();
}

/**
* 查询个数
*/

@SuppressWarnings("unchecked")
public long queryBuild2(WhereCondition condition, WhereCondition... conditions) {
return dao.queryBuilder().where(condition, conditions).buildCount().count();
}

/**
* 获取Cursor
*/

@SuppressWarnings("unchecked")
public Cursor queryBuild3(WhereCondition condition, WhereCondition... conditions) {
return dao.queryBuilder().where(condition, conditions).buildCursor().query();
}

}

工厂模式抽取[注:如果类太多,使用工厂方法模式]

public class DaoFactory {

public static AbstractDao createDao(@NonNull DaoSession session, Class<?> clazz) {
if (BaseModel.class.equals(clazz)) {
return session.getBaseDao();
} else if (Model.class.equals(clazz)) {
return session.getModelDao();
}
return null;
}
}

在MainActivity中的使用

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button insert;
private Button delete;
private Button deleteall;
private Button update;
private Button query;
private Button queryall;
private Button querybuild;
private TextView tv_query;
private TextView tv_queryall;
private TextView tv_querybuild;
private DataBaseManager<Model> manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initView();
initListener();
}

private void init() {
try {
manager = new DataBaseManager<Model>(MainActivity.this, Model.class.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}

private void initView() {
insert = (Button) findViewById(R.id.btn_insert);
delete = (Button) findViewById(R.id.btn_delete);
deleteall = (Button) findViewById(R.id.btn_deleteall);
update = (Button) findViewById(R.id.btn_update);
query = (Button) findViewById(R.id.btn_query);
queryall = (Button) findViewById(R.id.btn_queryall);
querybuild = (Button) findViewById(R.id.btn_querybuild);

tv_query = (TextView) findViewById(R.id.tv_query);
tv_queryall = (TextView) findViewById(R.id.tv_queryall);
tv_querybuild = (TextView) findViewById(R.id.tv_querybuild);
}

private void initListener() {
insert.setOnClickListener(this);
delete.setOnClickListener(this);
deleteall.setOnClickListener(this);
update.setOnClickListener(this);
query.setOnClickListener(this);
queryall.setOnClickListener(this);
querybuild.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_insert:
Model model = new Model();
model.setName("zhangsan");
model.setAge(32);
model.setCreateTime(System.currentTimeMillis());
manager.insert(model);
break;
case R.id.btn_update:
Model model1 = new Model();
model1.setId((long) 1);
model1.setName("lisi");
model1.setAge(23);
model1.setCreateTime(System.currentTimeMillis());
manager.update(model1);
break;
case R.id.btn_delete:
Model model2 = new Model();
model2.setId((long) 2);
manager.delete(model2);
break;
case R.id.btn_deleteall:
manager.deleteAll();
break;
case R.id.btn_query:
Model m = manager.query(1);
if (m!=null){
tv_query.setText("单条查询:"+m.toString());
}else {
tv_query.setText("单条查询:没有查询到结果");
}
break;
case R.id.btn_queryall:
List<Model> li = manager.queryAll();
tv_queryall.setText("查询全部:"+li.toString());
break;
case R.id.btn_querybuild:
List<Model> list = manager.queryBuild1(ModelDao.Properties.Name.like("zhangsan"));//注意条件查询
tv_querybuild.setText("条件查询:"+list.toString());
break;
}
}
}

Android ORM框架介绍之greenDao封装(二)

水平有限,封装的不是很好