要读取和修改数据,我有一个基础存储库
首先是我的实体://Category.java
package Entities;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Generated;
@org.greenrobot.greendao.annotation.Entity(
nameInDb = "Categories"
)
public class Category {
@Id
public Long CategoryId;
@NotNull
public String Name;
public String Description;
@Generated(hash = 1903625692)
public Category(Long CategoryId, @NotNull String Name, String Description) {
this.CategoryId = CategoryId;
this.Name = Name;
this.Description = Description;
}
@Generated(hash = 1150634039)
public Category() {
}
public Long getCategoryId() {
return this.CategoryId;
}
public void setCategoryId(Long CategoryId) {
this.CategoryId = CategoryId;
}
public String getName() {
return this.Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getDescription() {
return this.Description;
}
public void setDescription(String Description) {
this.Description = Description;
}
}
和 User.java
package Entities;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Generated;
@org.greenrobot.greendao.annotation.Entity(
nameInDb = "Users"
)
public class User {
@Id
public Long UserId;
public String Username;
public String Mobile;
@NotNull
public String UserApiKey;
@NotNull
public String SecretKey;
public String FirstName;
public String LastName;
public boolean IsFirstLogin;
@Generated(hash = 1246565860)
public User(Long UserId, String Username, String Mobile,
@NotNull String UserApiKey, @NotNull String SecretKey, String FirstName,
String LastName, boolean IsFirstLogin) {
this.UserId = UserId;
this.Username = Username;
this.Mobile = Mobile;
this.UserApiKey = UserApiKey;
this.SecretKey = SecretKey;
this.FirstName = FirstName;
this.LastName = LastName;
this.IsFirstLogin = IsFirstLogin;
}
@Generated(hash = 586692638)
public User() {
}
public Long getUserId() {
return this.UserId;
}
public void setUserId(Long UserId) {
this.UserId = UserId;
}
public String getUsername() {
return this.Username;
}
public void setUsername(String Username) {
this.Username = Username;
}
public String getMobile() {
return this.Mobile;
}
public void setMobile(String Mobile) {
this.Mobile = Mobile;
}
public String getUserApiKey() {
return this.UserApiKey;
}
public void setUserApiKey(String UserApiKey) {
this.UserApiKey = UserApiKey;
}
public String getSecretKey() {
return this.SecretKey;
}
public void setSecretKey(String SecretKey) {
this.SecretKey = SecretKey;
}
public String getFirstName() {
return this.FirstName;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getLastName() {
return this.LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public boolean getIsFirstLogin() {
return this.IsFirstLogin;
}
public void setIsFirstLogin(boolean IsFirstLogin) {
this.IsFirstLogin = IsFirstLogin;
}
}
和BaseRepository.java
package services;
import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.WhereCondition;
import java.util.ArrayList;
import java.util.List;
import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Interfaces.IBaseRepository;
/**
* Created by macintosh on 1/20/18.
*/
public class BaseRepository<T> implements IBaseRepository<T> {
Class<T> type;
public AbstractDao<T, ?> dao;
public BaseRepository(Class<T> _type) {
type = _type;
DaoSession daoSession = MainDbContext.getDaoSession();
dao = (AbstractDao<T, ?>) daoSession.getDao(_type);
}
public Boolean Create(T entity)
{
dao.insert(entity);
return true;
}
public void Update(T entity)
{
dao.update(entity);
}
public boolean Delete(Long id)
{
DeleteQuery<T> deleteQuery= dao.queryBuilder()
.where(dao.getPkProperty().eq(id)).buildDelete();
deleteQuery.executeDeleteWithoutDetachingEntities();
MainDbContext.getDaoSession().clear();
return true;
}
public boolean DeleteAll()
{
dao.deleteAll();
return true;
}
public boolean Delete(WhereCondition cond)
{
DeleteQuery<T> deleteQuery= dao.queryBuilder()
.where(cond).buildDelete();
deleteQuery.executeDeleteWithoutDetachingEntities();
MainDbContext.getDaoSession().clear();
return true;
}
public T FirstOrDefault(Long id)
{
List<T> entities = dao.queryBuilder()
.where(dao.getPkProperty().eq(id)).limit(1).list();
if (entities.size() > 0)
return entities.get(0);
else
return null;
}
public boolean Exists(WhereCondition cond)
{
Long counts = dao.queryBuilder()
.where(cond).count();
if (counts > 0)
return true;
else
return false;
}
public Long Count(WhereCondition cond)
{
Long counts = dao.queryBuilder()
.where(cond).count();
return counts;
}
public T FirstOrDefault(WhereCondition... cond)
{
List<T> lst = new ArrayList<>();
if (cond.length == 1)
lst = dao.queryBuilder().where(cond[0]).limit(1).list();
else
if (cond.length == 2)
lst = dao.queryBuilder().where(cond[0],cond[1]).limit(1).list();
else
if (cond.length == 3)
lst = dao.queryBuilder().where(cond[0],cond[1],cond[2]).limit(1).list();
if (lst.size() > 0)
return lst.get(0);
return null;
}
public T LastOrDefault()
{
List<T> entities = dao.queryBuilder()
.orderDesc(dao.getPkProperty()).limit(1).list();
if (entities.size() > 0)
return entities.get(0);
else
return null;
}
public List<T> FetchMulti()
{
return (List<T>)dao.loadAll();
}
public List<T> FetchMulti(WhereCondition... cond)
{
if (cond.length == 1)
return dao.queryBuilder().where(cond[0]).list();
else
if (cond.length == 2)
return dao.queryBuilder().where(cond[0],cond[1]).list();
else
if (cond.length == 3)
return dao.queryBuilder().where(cond[0],cond[1],cond[2]).list();
else
return dao.loadAll();
}
}
及其界面
package Interfaces;
import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.WhereCondition;
import java.util.List;
/**
* Created by macintosh on 1/20/18.
*/
public interface IBaseRepository<T> {
Boolean Create(T entity);
void Update(T entity);
boolean Delete(Long id);
boolean DeleteAll();
boolean Delete(WhereCondition cond);
T FirstOrDefault(Long id);
boolean Exists(WhereCondition cond);
Long Count(WhereCondition cond);
T FirstOrDefault(WhereCondition... cond);
T LastOrDefault();
List<T> FetchMulti();
List<T> FetchMulti(WhereCondition... cond);
}
然后我可以在我的服务中访问这些方法CategoryService.java
package services;
import android.content.Context;
import Common.VolleyCallback;
import Entities.Category;
import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Interfaces.ICategoryService;
import Interfaces.IProductService;
import iOC.ServiceFactory;
import restful.restfulApiInterfaces.IProductApi;
import restful.restfulResponse.ProductCategoryApiResponse;
import viewModels.ProductCategoryViewModel;
public class CategoryService extends BaseRepository<Category> implements ICategoryService {
Context context;
public CategoryService() {
super(Category.class);
context = MainDbContext.getAppContext();
}
}
及其界面
package Interfaces;
import Common.VolleyCallback;
import Entities.Category;
public interface ICategoryService extends IBaseRepository<Category> {
}
或 userService.java
package services;
import android.content.Context;
import java.util.List;
import Entities.DaoSession;
import ir.swifttips.fidiboo.MainDbContext;
import Entities.User;
import Entities.UserDao;
import Interfaces.IUserService;
/**
* Created by macintosh on 12/30/17.
*/
public class UserService extends BaseRepository<User> implements IUserService {
Context context;
public UserService() {
super(User.class);
context = MainDbContext.getAppContext();
}
public User GetUserById_Query(Long id) {
List<User> users = dao.queryBuilder().where(UserDao.Properties.UserId.eq(id)).list();
if (users.size() > 0)
return users.get(0);
return null;
}
public User GetUserById(Long id) {
DaoSession daoSession = MainDbContext.getDaoSession();
User user = daoSession.getUserDao().load(id);
return user;
}
public boolean Exists(Long id) {
DaoSession daoSession = MainDbContext.getDaoSession();
User user = daoSession.getUserDao().load(id);
if (user == null)
return false;
else
return true;
}
}
及其界面
package Interfaces;
import Entities.User;
import Entities.DaoSession;
/**
* Created by macintosh on 1/3/18.
*/
public interface IUserService extends IBaseRepository<User> {
User LoggedinUser();
User GetUserById_Query(Long id);
User GetUserById(Long id) ;
boolean Exists(Long id) ;
}