http://www.cnblogs.com/taoweiji/p/5246540.html
http://www.cnblogs.com/sihaixuan/p/4852974.html Android studio Maven仓库使用
1、是什么?
dbflow是一款android高性的ORM数据库.可以使用在进行项目中有关数据库的操作。
2、android studio中配置引用。
(1)在项目工程下面的build.gradle中配置如下
buildscript {
repositories {
jcenter{url "http://52.79.55.169:8081/nexus/content/repositories/central/"}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
allprojects {
repositories {
jcenter{url "http://52.79.55.169:8081/nexus/content/repositories/central/"}
maven{ url "https://jitpack.io"}
}
}
2)在app的build.gradle中配置
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
def dbflow_version ="3.0.0-beta5"
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.wangyu.dbflowapplication"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
apt "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
}
3)编译项目,就可以看到项目下下载了有关dbflow的library.
3、项目中使用.
1)创建一个application , 初始化的时候init()
public class MyAppcation extends Application{ private static MyAppcation mContext; @Override public void onCreate() { super.onCreate(); FlowManager.init(this); } // 单例模式中获取唯一的MyApplication实例 public static MyAppcation getInstance() { if (null == mContext) { mContext = new MyAppcation(); } return mContext; } }
ps:xml中别忘了添加name属性
<application
android:name=".MyAppcation">
2) 创建一个AppDataBase,用于创建数据库以及版本号
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION) public class AppDatabase { public static final String NAME = "AppDatabase"; //数据库版本号 public static final int VERSION = 1; }
3)
3)创建一个实体类,与数据类表字段相映射
@ModelContainer @Table(database =AppDatabase.class) public class Student extends BaseModel{ //自增ID @PrimaryKey(autoincrement = true) public Long id; @Column public String name; @Column public int age; @Column public Long time; }
4)创建一个管理类用于对数据的增删该查
public class DataBaseManager {
private Context mContext;
private Student mStudent; //
public DataBaseManager() {
this.mContext = MyAppcation.getInstance();
}
/**
* 插入一个学生信息
*
* @param name 姓名
* @param age 年龄
*/
public void insertData(String name, int age) {
mStudent = queryData(name, age);
if (mStudent == null) {
mStudent = new Student();
mStudent.name = name;
mStudent.age = age;
}
mStudent.time = System.currentTimeMillis();
mStudent.save();//如果插入数据已存在,则更新原数据
}
/**
* 查询所有的学生
*
* @param
* @return
*/
public List<Student> getData() {
List<Student> record = SQLite.select()
.from(Student.class)
.orderBy(Student_Table.time, false)
.queryList();
return record;
}
/**
* 删除学生信息
*
* @param
*/
public void deletAllData(String name, int age) {
List<Student> record = SQLite.select()
.from(Student.class)
.where(Student_Table.name.eq(name))
.and(Student_Table.age.eq(age))
.queryList();
for (Student student : record) {
student.delete();
}
}
public Student queryData(String name, int age) {
Student record = SQLite.select()
.from(Student.class)
.where(Student_Table.name.eq(name))
.and(Student_Table.age.eq(age))
.querySingle();
return record;
}
ps :Student_Table是如何出现的那?--》是自动编译生成的。
5)进行写一个activity进行验证即可。
b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("test", "-------------------insert"); Student s1 = new Student(); s1.name = "hello"; s1.age = 21; manager.insertData(s1.name, s1.age); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { students = manager.getAllData(); Log.d("test", "name:" + students.get(0).name.toString() + "age:" + students.get(0).age); });
ps其他:
1、名词:
Maven仓库:就是文件服务器,都是标准的 android library仓库 。
center():jcenter是一个由 bintray.com维护的Maven仓库
mavenCentral():Maven Central 则是由sonatype.org维护的Maven仓库
maven():是我们使用的library的作者是把该library放在自己的服务器上。
allprojects { repositories { mavenCentral()
center() } }
定义之后就可以使用maven服务器了。
详细内容可参考:http://www.cnblogs.com/sihaixuan/p/4852974.html
2、阅读了一些文章,简单的用自己的话描述一下自己的理解。
ps:android studio是如何加载类库的?
gradle根据你声明的maven仓库以及要引用仓库下面的某一些library,去对应的仓库中查询,然后在项目编译过去下载。so我们只是简单的告诉gradle我们需要什么,剩下的都是 gradle做的。