【Android】在Android上使用OrmLite数据库框架 之 使用表配置文件

转载自:http://blog.csdn.net/oo8_8oo/article/details/7302335

接上一篇:

《在Android上使用OrmLite数据库框架 之 基本用法》


说明:OrmLite可能通过实体类加注解的方式实现了对数据库的操作,这种方式非常方便,

不过我们可以让程序跑得更有效率,因为我们不需要让程序每次操作数据库都得解析一下实体的注解。

Android基情群:91488018 (安卓是会呼吸的痛)

----------------------------基情线------------------------------

1.先把Android项目调成可以跑main()方法

第一步不会的,点这里!


2.在res目录下建立raw目录(少了这步无法进行)

 

3.新建一个工具类,大概如下:

  1. publicclass DatabaseConfigUtil extends OrmLiteConfigUtil {  
  2.    
  3.   
  4. privatestatic final Class<?>[] classes = new Class[] { Department.class,  
  5.   
  6. Employee.class};  
  7.   
  8.    
  9.   
  10. publicstatic void main(String[] args) throws Exception {  
  11.   
  12. writeConfigFile("ormlite_config.txt",classes);  
  13.   
  14. }  
  15.   
  16. }  
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);

}

}
好吧,看我写的:

  1. package com.deng.orm;  
  2.   
  3. import com.deng.orm.vo.Department;  
  4. import com.deng.orm.vo.Employee;  
  5. import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;  
  6.   
  7. /** 
  8.  * @author 剑戟 
  9.  * @version 创建时间:2011-12-16 下午02:24:40 
  10.  */  
  11. public class TableUtil extends OrmLiteConfigUtil {  
  12.   
  13.     private static final Class<?>[] classes = new Class[] { Department.class,  
  14.             Employee.class };  
  15.   
  16.     public static void main(String[] args) throws Exception {  
  17.         System.out.println("### begin...");  
  18.         writeConfigFile("ormlite_config.txt", classes);  
  19.         System.out.println("### end...");  
  20.     }  
  21.   
  22. }  
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文件,内容大概如下:

  1. #  
  2.   
  3. # generated on2011/12/16 02:40:22  
  4.   
  5. #  
  6.   
  7. # --table-start--  
  8.   
  9. dataClass=com.deng.orm.vo.Department  
  10.   
  11. tableName=department  
  12.   
  13. #--table-fields-start--  
  14.   
  15. # --field-start--  
  16.   
  17. fieldName=id  
  18.   
  19. generatedId=true  
  20.   
  21. # --field-end--  
  22.   
  23. # --field-start--  
  24.   
  25. fieldName=departName  
  26.   
  27. unique=true  
  28.   
  29. # --field-end--  
  30.   
  31. #--table-fields-end--  
  32.   
  33. # --table-end--  
  34.   
  35. #################################  
  36.   
  37. # --table-start--  
  38.   
  39. dataClass=com.deng.orm.vo.Employee  
  40.   
  41. tableName=TB_EMP  
  42.   
  43. #--table-fields-start--  
  44.   
  45. # --field-start--  
  46.   
  47. fieldName=empID  
  48.   
  49. generatedId=true  
  50.   
  51. # --field-end--  
  52.   
  53. # --field-start--  
  54.   
  55. fieldName=name  
  56.   
  57. # --field-end--  
  58.   
  59. # --field-start--  
  60.   
  61. fieldName=age  
  62.   
  63. # --field-end--  
  64.   
  65. # --field-start--  
  66.   
  67. fieldName=depart  
  68.   
  69. foreign=true  
  70.   
  71. foreignAutoRefresh=true  
  72.   
  73. # --field-end--  
  74.   
  75. #--table-fields-end--  
  76.   
  77. # --table-end--  
  78.   
  79. #################################  
#

# 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类中自动生成如下:

  1. public static final class raw {  
  2.   
  3.     public static final intormlite_config=0x7f040000;  
  4.   
  5. }  
    public static final class raw {

        public static final intormlite_config=0x7f040000;

    }


 

4.在DatabaseHelper中改成如下:

  1. publicDatabaseHelper(Context context) {  
  2.   super(context, DATABASE_NAME, null,DATABASE_VERSION,  
  3.     R.raw.ormlite_config);  
  4. }  
publicDatabaseHelper(Context context) {
  super(context, DATABASE_NAME, null,DATABASE_VERSION,
    R.raw.ormlite_config);
}

让程序用上刚才我们生成的表配置文件

 

5.程序启动时,看到如下Log表明加载配置文件成功:

  1. 12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Department  
  2.   
  3. 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高清有码下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值