最近才接触xUtils,看了一会才发现已更新3.0的新版本,例子简直少的可怜,还是各种互抄各种转载=。=
<!----------吐槽的分割线----------------------------————————————————————----------------->
本文主要就单表查询做一番演示(为了解耦做了一点封装):
这里一共有四步操作:
1.首先,得有个实体类吧(xUtils用注解建表)
2.新建一个DatabaseOpenHelper类,封装初始化操作,提供DbManager.DaoConfig对象(数据库辅助建立类)
3.新建一个与业务数据库操作相关的类,这里就是PersonDB类(具体业务模块实现类)
这个类会把一些常用的数据库操作封装起来
4.在Activity中使用
<!-----------实操分割线-----------------------————————————————————---------------------->
1.创建实体类
@Table(name="Person")
public class Person {
@Column(name = "id",isId = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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 "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
//为了测试方便加个toString
}
2.DatabaseOpenHelper类
public class DatabaseOpenHelper {
private DbManager.DaoConfig daoConfig;
public DatabaseOpenHelper(String name,int version) {
daoConfig = new DbManager.DaoConfig()
.setDbName(name)
.setDbVersion(version)
.setDbOpenListener(new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
db.getDatabase().enableWriteAheadLogging();
//开启WAL, 对写入加速提升巨大(作者原话)
}
})
.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
//数据库升级操作
}
});
}
public DbManager.DaoConfig getDaoConfig(){
return daoConfig;
}
}
3.PersonDB类
public class PersonDB {
public static final String DB_NAME = "person.db";
//数据库名
public static final int VERSION = 1;
//数据库版本号
private static PersonDB personDB;
private static DbManager db;
//接收构造方法初始化的DbManager对象
private PersonDB(){
DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(DB_NAME,VERSION);
db = x.getDb(databaseOpenHelper.getDaoConfig());
}
//构造方法私有化,拿到DbManager对象
public synchronized static PersonDB getIntance(){
if (personDB == null){
personDB = new PersonDB();
}
return personDB;
}
//获取PersonDB实例
/****************************************************************************************/
//写两个测试方法,也就是常用的数据库操作
public void savePerson(Person person){
try {
db.save(person);
Log.d("xyz","save succeed!");
} catch (DbException e) {
Log.d("xyz",e.toString());
}
}
//将Person实例存进数据库
public List<Person> loadPerson(){
List<Person> list = null;
try {
list = db.selector(Person.class).findAll();
} catch (DbException e) {
e.printStackTrace();
}
return list;
}
//读取所有Person信息
}
4.在Activity中使用
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDB personDB = PersonDB.getIntance();
Person person1 = new Person("jay",20);
Person person2 = new Person("wang",40);
personDB.savePerson(person1);
personDB.savePerson(person2);
//写
List<Person> list = personDB.loadPerson();
if (list!=null) {
for (Person person : list) {
Log.d("xyz", person.toString());
}
}
//读
}
}
<!--------16/8/2/19:48更新--------------------------------------------------------------------------------------->
由于对数据库单例不熟,有以下改进写法:
DatabaseOpenHelper类:
public class DatabaseOpenHelper {
private DbManager.DaoConfig daoConfig;
private static DbManager db;
private final String DB_NAME = "mydb";
private final int VERSION = 1;
private DatabaseOpenHelper() {
daoConfig = new DbManager.DaoConfig()
.setDbName(DB_NAME)
.setDbVersion(VERSION)
.setDbOpenListener(new DbManager.DbOpenListener() {
@Override
public void onDbOpened(DbManager db) {
db.getDatabase().enableWriteAheadLogging();
//开启WAL, 对写入加速提升巨大(作者原话)
}
})
.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
//数据库升级操作
}
});
db = x.getDb(daoConfig);
}
public static DbManager getInstance(){
if (db == null){
DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper();
}
return db;
}
}
PersonDB类:
public class PersonDB {
private DbManager db;
//接收构造方法初始化的DbManager对象
public PersonDB(){
db = DatabaseOpenHelper.getInstance();
}
/****************************************************************************************/
//写两个测试方法
public void savePerson(Person person){
try {
db.save(person);
Log.d("xyz","save succeed!");
} catch (DbException e) {
Log.d("xyz",e.toString());
}
}
//将Person实例存进数据库
public List<Person> loadPerson(){
List<Person> list = null;
try {
list = db.selector(Person.class).findAll();
} catch (DbException e) {
e.printStackTrace();
}
return list;
}
//读取所有Person信息
}
在Activity中使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDB personDB = new PersonDB();
Person person1 = new Person("jay",20);
Person person2 = new Person("wang",40);
personDB.savePerson(person1);
personDB.savePerson(person2);
List<Person> list = personDB.loadPerson();
if (list!=null) {
for (Person person : list) {
Log.d("xyz", person.toString());
}
}
}