xUtils是基于Afinal开发的目前功能比较完善的一个Android开源框架。这个框架里有网络模块,图片模块,注解模块,数据库操作模块等, 今天说下如何用xUtils来操作数据库.
1.先在app的build.gradle中添加依赖 compile 'org.xutils:xutils:3.3.36'
2.新建User实体类
/** * @table 表名 */ @Table(name = "t_user") public class User { /** * @colume 这个注解是将实体类的字段名和数据库字段名进行映射 * id 主键, * autoGen 是否自动增长 */ @Column(name = "id",isId = true,autoGen = true) private int id; @Column(name = "name") private String name; @Column(name = "age") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } }
3.新建一个Application在onCreate中进行初始化
/** * Created by Administrator on 2018/3/31. * 此处需要初始化,不然会报错 */ public class MyApplication extends Application{ @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } }
4.创建数据库管理类
/** * Created by Administrator on 2018/3/22. * 创建数据库管理类 */ public class DBManager { private static final String TAG = DBManager.class.getSimpleName(); public static DBManager dbManager; public static DBManager getDbManager(){ if(dbManager == null){ dbManager = new DBManager(); } return dbManager; } private DBManager(){ } public DbManager getConfig(){ DbManager.DaoConfig daoConfig = new DbManager.DaoConfig() .setDbName("user.db") .setDbVersion(1) .setAllowTransaction(true)//开启实务 .setDbOpenListener(new DbManager.DbOpenListener() { @Override public void onDbOpened(DbManager dbManager) { } }) .setDbUpgradeListener(new DbManager.DbUpgradeListener() { @Override public void onUpgrade(DbManager dbManager, int i, int i1) { } }) .setTableCreateListener(new DbManager.TableCreateListener() { @Override public void onTableCreated(DbManager dbManager, TableEntity> tableEntity) { } }); return x.getDb(daoConfig); } }
4.UserDao类,增删改查
public class UserDao { private static DbManager dbManager; static { dbManager = DBManager.getDbManager().getConfig(); } /** * 增加一条用户数据 * @param user */ public static void add(User user){ try { dbManager.save(user); } catch (DbException e) { e.printStackTrace(); } } public static void delete(User user){ try { //直接删除 dbManager.delete(user); //通过条件删除,删除id 为1的用户 WhereBuilder whereBuilder = WhereBuilder.b(); whereBuilder.and("id","=",1); dbManager.delete(User.class,whereBuilder); //通过条件删除,删除id 为<10的用户 或者 name为tom的用户 WhereBuilder whereBuilder2 = WhereBuilder.b(); whereBuilder.and("id","<",10).or("name","=","tom"); dbManager.delete(User.class,whereBuilder2); //通过条件删除,删除id 为<10的用户 并且 name 中间用t的用户 WhereBuilder whereBuilder3 = WhereBuilder.b(); whereBuilder.and("id","<",10).and("name","like","%t%"); dbManager.delete(User.class,whereBuilder3); //自定义SQL语句删除 Cursor cursor = dbManager.execQuery("DELETE FORM t_user WHERE id = 1"); while(cursor.moveToNext()){ } } catch (DbException e) { e.printStackTrace(); } } public static void update(User user){ try { //通过user的set方法改变值之后,保存即可更新 dbManager.saveOrUpdate(user); //通过条件更新,将id为1的姓名进行修改 //KeyValue 用数据库的字段作为key 将值做为字段的值 WhereBuilder whereBuilder = WhereBuilder.b(); whereBuilder.and("id","=",1); KeyValue keyValue = new KeyValue("name","michael"); dbManager.update(User.class,whereBuilder,keyValue); } catch (DbException e) { e.printStackTrace(); } } public static void find(){ try { //查询所有的User List这是xUtils最常用的操作方法了,感觉还是比原生的SQLite使用方便很多,xUtils还有很多功能大家可以去摸索下,比如一对多,多对多等复杂的表关系如何操作。userList = dbManager.findAll(User.class); //查询id = 1的用户 List userList2 = dbManager.selector(User.class).where("id","=",1).findAll(); //原生SQL查询 Cursor cursor = dbManager.execQuery("SELECT * FORM t_user WHERE id = 1"); while(cursor.moveToNext()){ } } catch (DbException e) { e.printStackTrace(); } } }