Android之ORMLite使用(1)

ORMLite介绍: 一个轻量级的Java对象关系映射持久层框架。支持包括 MySQL、Postgres、Microsoft SQL Server、H2、Derby、HSQLDB和Sqlite等在内的数据库。提供灵活的QueryBuilder来构建复杂的数据查询。强大的抽象DAO类,只需5行代码便能够自动生成SQL来创建和删除数据库表格。

ORMLite使用:
(1)在android中使用ORMLite,首先下载所需的ormlite-android-4.41.jarormlite-core-4.41.jar两个文件,导入到项目中。
(2)通过ORMLite注解,将数据持久化。@DatabaseTable注解配置(@DatabaseTable(tableName = "accounts"))
以下是一个简单的ORMLite使用DEMO

countryInfo类
@DatabaseTable (tableName = "country" )
public class CountryInfo {

      @DatabaseField(generatedId = true) // 主键 自增
      private int id;
      @DatabaseField(columnName = "name") // 表中列名
      private String name;
      @DatabaseField(columnName = "no")
      private String no;
      public CountryInfo() {
            super();
     }
      public CountryInfo(String name, String no) {
            super();
            this. name = name;
            this. no = no;
     }
      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 getNo() {
            return no;
     }
      public void setNo(String no) {
            this. no = no;
     }
     
}


DBHelper类
public class DBHelper extends OrmLiteSqliteOpenHelper {

      private static final String DB_NAME = "app_noname.db";
      private static final int DB_VERSION = 1;
      private static DBHelper instance;
      private Map<String, Dao> daos = new HashMap<String, Dao>();
     
      public DBHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION); // 创建数据库
     }

      public static synchronized DBHelper getInstance(Context context){
            if ( instance == null) {
                 synchronized (DBHelper. class) {
                      if ( instance == null) {
                            instance = new DBHelper(context);
                     }
                }
           }
            return instance;
     }

      @Override
      public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
            try {
                TableUtils. createTable(connectionSource, CountryInfo.class );
           } catch (SQLException e) {
                 // TODO
                e.printStackTrace();
           }
     }

      @Override
      public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
                 int arg3) {
            try {
                TableUtils. dropTable(arg1, CountryInfo. class, true);
           } catch (SQLException e) {
                 // TODO
                e.printStackTrace();
           }
     }    

      public synchronized Dao getDao(Class clazz) throws SQLException{
            Dao dao = null;
           String className = clazz.getSimpleName();
            if ( daos.containsKey(className)) {
                dao = daos.get(className);
           }
            if (dao == null) {
                dao = super .getDao(clazz);
                 daos.put(className, dao);
           }
            return dao;
     }
     
      @Override
      public void close() {
            for (String key : daos.keySet()) {
                 Dao dao = daos .get(key);
                dao = null;
           }
            super.close();
     }
}


CountryDao类
public class CountryDao {

      private Context mContext;
      private DBHelper mDbHelper;
      private Dao<CountryInfo, Integer> mCountryDao;

      public CountryDao(Context context) {
            mContext = context;
            mDbHelper = DBHelper. getInstance( mContext);
            try {
                 mCountryDao = mDbHelper .getDao(CountryInfo. class);
           } catch (SQLException e) {
                 // TODO
                e.printStackTrace();
           }
     }
     
      /**
     * @Title : addCountry
     * @Description : 新增一个country信息
     * @param   countryInfo
     * @return void    返回类型
     * @throws
     */
      public void addCountry(CountryInfo countryInfo){
            try {
                 mCountryDao.create(countryInfo);
                System. out.println( "country数据添加成功" );
           } catch (SQLException e) {
                 // TODO
                e.printStackTrace();
           }
     }
     
      public List<CountryInfo> findByNo(String value){
           List<CountryInfo> countryInfos = null;
            try {
                countryInfos = mCountryDao.queryBuilder().where().eq( "no", value).query();
                System. out.println( "country数据查询成功" );
           } catch (SQLException e) {
                e.printStackTrace();
           }
            return countryInfos;
     }  
}



测试代码:
 CountryDao countryDao = new CountryDao( this);
           countryDao.addCountry(getCountryData());
           List<CountryInfo> findByNos = countryDao.findByNo("12345" );
            if (findByNos.size() > 0) {
                 for (CountryInfo countryInfo : findByNos) {
                     System. err.println(countryInfo.getName());
                }
           }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。