转载自:http://blog.csdn.net/oo8_8oo/article/details/7302335
接上一篇:
《在Android上使用OrmLite数据库框架 之 基本用法》
说明:OrmLite可能通过实体类加注解的方式实现了对数据库的操作,这种方式非常方便,
不过我们可以让程序跑得更有效率,因为我们不需要让程序每次操作数据库都得解析一下实体的注解。
Android基情群:91488018 (安卓是会呼吸的痛)
----------------------------基情线------------------------------
1.先把Android项目调成可以跑main()方法
2.在res目录下建立raw目录(少了这步无法进行)
3.新建一个工具类,大概如下:
- publicclass DatabaseConfigUtil extends OrmLiteConfigUtil {
- privatestatic final Class<?>[] classes = new Class[] { Department.class,
- Employee.class};
- publicstatic void main(String[] args) throws Exception {
- writeConfigFile("ormlite_config.txt",classes);
- }
- }
publicclass DatabaseConfigUtil extends OrmLiteConfigUtil {
privatestatic final Class<?>[] classes = new Class[] { Department.class,
Employee.class};
publicstatic void main(String[] args) throws Exception {
writeConfigFile("ormlite_config.txt",classes);
}
}
好吧,看我写的:
- package com.deng.orm;
- import com.deng.orm.vo.Department;
- import com.deng.orm.vo.Employee;
- import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;
- /**
- * @author 剑戟
- * @version 创建时间:2011-12-16 下午02:24:40
- */
- public class TableUtil extends OrmLiteConfigUtil {
- private static final Class<?>[] classes = new Class[] { Department.class,
- Employee.class };
- public static void main(String[] args) throws Exception {
- System.out.println("### begin...");
- writeConfigFile("ormlite_config.txt", classes);
- System.out.println("### end...");
- }
- }
package com.deng.orm;
import com.deng.orm.vo.Department;
import com.deng.orm.vo.Employee;
import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;
/**
* @author 剑戟
* @version 创建时间:2011-12-16 下午02:24:40
*/
public class TableUtil extends OrmLiteConfigUtil {
private static final Class<?>[] classes = new Class[] { Department.class,
Employee.class };
public static void main(String[] args) throws Exception {
System.out.println("### begin...");
writeConfigFile("ormlite_config.txt", classes);
System.out.println("### end...");
}
}
说明:classes数组里把要映射的实体类装入;本类是在开发环境下运行使用,运行后会在raw目录下生成ormlite_config.txt文件,内容大概如下:
- #
- # generated on2011/12/16 02:40:22
- #
- # --table-start--
- dataClass=com.deng.orm.vo.Department
- tableName=department
- #--table-fields-start--
- # --field-start--
- fieldName=id
- generatedId=true
- # --field-end--
- # --field-start--
- fieldName=departName
- unique=true
- # --field-end--
- #--table-fields-end--
- # --table-end--
- #################################
- # --table-start--
- dataClass=com.deng.orm.vo.Employee
- tableName=TB_EMP
- #--table-fields-start--
- # --field-start--
- fieldName=empID
- generatedId=true
- # --field-end--
- # --field-start--
- fieldName=name
- # --field-end--
- # --field-start--
- fieldName=age
- # --field-end--
- # --field-start--
- fieldName=depart
- foreign=true
- foreignAutoRefresh=true
- # --field-end--
- #--table-fields-end--
- # --table-end--
- #################################
#
# generated on2011/12/16 02:40:22
#
# --table-start--
dataClass=com.deng.orm.vo.Department
tableName=department
#--table-fields-start--
# --field-start--
fieldName=id
generatedId=true
# --field-end--
# --field-start--
fieldName=departName
unique=true
# --field-end--
#--table-fields-end--
# --table-end--
#################################
# --table-start--
dataClass=com.deng.orm.vo.Employee
tableName=TB_EMP
#--table-fields-start--
# --field-start--
fieldName=empID
generatedId=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=age
# --field-end--
# --field-start--
fieldName=depart
foreign=true
foreignAutoRefresh=true
# --field-end--
#--table-fields-end--
# --table-end--
#################################
此时,R类中自动生成如下:
- public static final class raw {
- public static final intormlite_config=0x7f040000;
- }
public static final class raw {
public static final intormlite_config=0x7f040000;
}
4.在DatabaseHelper中改成如下:
- publicDatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null,DATABASE_VERSION,
- R.raw.ormlite_config);
- }
publicDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null,DATABASE_VERSION,
R.raw.ormlite_config);
}
让程序用上刚才我们生成的表配置文件
5.程序启动时,看到如下Log表明加载配置文件成功:
- 12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Department
- 12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Employee
12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Department
12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Employee
Demo高清有码下载地址: