#afinal#是一个android的orm、ioc快速开发框架,里面包含了四大功能:空间的id绑定和事件绑定功能;网络图片的显示功能(里面包含了强大的缓存框架);数据库sqlite的操作功能;http数据的读取功能(支持ajax方式读取);
#afinal#开源网址:https://github.com/yangfuhai/afinal
这篇文章主要是介绍afinal的功能之一FinalDb组件,其他组件请关注我的博客吧,以后将会一一介绍:
# afinal#的FinalDb组件是android的一个轻量级的orm框架,使用简单,一行代码就可以完成数据库的各种操作功能。
AfinalOrmDemoActivity.java
package com.devchina.ormdemo; import java.util.Date; import java.util.List; import net.tsz.afinal.FinalActivity; import net.tsz.afinal.FinalDb; import net.tsz.afinal.annotation.view.ViewInject; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class AfinalOrmDemoActivity extends FinalActivity { @ViewInject(id=R.id.textView) TextView textView; //这里使用了afinal的ioc功能,以后将会讲到 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FinalDb db = FinalDb.create(this); User user = new User(); user.setEmail("afinal@tsz.net"); user.setName("探索者"); user.setRegisterDate(new Date()); user.setMoney(12.11); db.save(user); List<User> userList = db.findAll(User.class); Log.e("AfinalOrmDemoActivity","用户数量:"+ (userList!=null?userList.size():0)); for (int i = 0; i < userList.size(); i++) { System.out.println(userList.get(i).getName()+":"+userList.get(i).getRegisterDate()+"----"+userList.get(i).getMoney()); } textView.setText(userList.get(0).getName()+":"+userList.get(0).getRegisterDate()+"----"+userList.get(0).getMoney()); } }
测试实体类 User.java
package com.devchina.ormdemo; import java.util.Date; import net.tsz.afinal.annotation.sqlite.Table; import net.tsz.afinal.annotation.sqlite.Id; @Table(name="user_test") public class User { @Id(column="userId") private int id; private String name; private String email; private Date registerDate; private Double money; /////////////getter and setter/////////////// 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 String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getRegisterDate() { return registerDate; } public void setRegisterDate(Date registerDate) { this.registerDate = registerDate; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.devchina.ormdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".AfinalOrmDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
jar包: https://github.com/yangfuhai/afinal
转自: http://my.oschina.net/yangfuhai/blog/87459