Xutils框架之DBUtils(DbManager)数据库增删改查

Xutils的数据库的简单使用

1、首先app build.gralde中添加依赖

 api 'org.xutils:xutils:3.5.0'

2、然后在自定义的Application中初始化

  //xutils 初始化
  x.Ext.init(this);
  x.Ext.setDebug(BuildConfig.DEBUG);

3、创建数据库帮助类

/**
 * 数据库帮助类
 */
public class DatabaseOpenHelper {

    private final String TAG = DatabaseOpenHelper.class.getSimpleName();
    private DbManager.DaoConfig daoConfig;
    private static DbManager dbManager;
    private final String DB_NAME = "contact.db";
    private final int DB_VERSION = 1;

    private DatabaseOpenHelper() {

        daoConfig = new DbManager.DaoConfig()
                .setDbName(DB_NAME)
                .setDbVersion(DB_VERSION)
                .setDbOpenListener(db -> db.getDatabase().enableWriteAheadLogging())
                .setDbUpgradeListener((db, oldVersion, newVersion) -> {

                }).setTableCreateListener((db, table) -> {

                });
        dbManager = x.getDb(daoConfig);
    }

    public static DbManager getInstance() {
        if (dbManager == null) {
            DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper();
        }
        return dbManager;
    }
    
}

4、需要保存到数据库的实体类


@Table(name = "contact_list") //表名
public class ContactBean implements Serializable {

    @Column(name = "id", isId = true, autoGen = false)// isId是否主键,autoGen是否自增长
    private String id;
    @Column(name = "photo")
    private String photo;
    @Column(name = "nickname")
    private String nickname;
    @Column(name = "message_num")
    private int message_num;
    @Column(name = "message")
    private String message;
    @Column(name = "last_time")
    private String last_time;
    @Column(name = "is_read")
    private String is_read;
    @Column(name = "user_id")
    private String user_id;

    //这个一定要有
    public ContactBean() {
    }
    
    //set和get此处省略

}

5、创建增删改查的Dao类

public class ContactDao {

    private final String TAG = ContactDao.class.getSimpleName();
    private DbManager dbManager;
    private boolean isSuccess;
    private List contactList = null;

    public ContactDao() {
        dbManager = DatabaseOpenHelper.getInstance();
    }

    //保存或者更新联系人数据
    public void addOrUpdate(ContactBean contactBean) {
        try {
            ContactBean contact = dbManager.findById(ContactBean.class, contactBean.getId());

            if (contact == null) {
                dbManager.save(contactBean);
               
            } else {
                WhereBuilder builder = WhereBuilder.b();

                builder.and("id", "=", contactBean.getId());

                KeyValue key1 = new KeyValue("message", contactBean.getMessage());
                KeyValue key2 = new KeyValue("last_time", contactBean.getLast_time());
                KeyValue key3 = new KeyValue("message_num",         contactBean.getMessage_num());

                dbManager.update(ContactBean.class, builder, key1, key2, key3);
               
            }
           
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    //根据条件查找数据
    public List getContactAll2() {
        try {
            String user_id = LoginManager.getUserId();
            contactList = dbManager.selector(ContactBean.class).where("user_id",
                    "=", user_id).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return contactList;
    }

    //查找全部数据
    public List getContactAll() {
        try {
            contactList = dbManager.selector(ContactBean.class).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return contactList;
    }

    public boolean delete(ContactBean contactBean) {
        try {
            dbManager.delete(contactBean);
            isSuccess = true;
        } catch (DbException e) {
            isSuccess = false;
            e.printStackTrace();
        }
        return isSuccess;
    }

    public void close() {
        try {
            dbManager.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(Xutils框架之DBUtils(DbManager)数据库增删改查)