android orm映射框架(类似hibernate)基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。http://blog.csdn.net/LuoXianXion https://blog.csdn.net/LuoXianXion/article/details/7898372

android  orm映射框架,可像hibernate一样操作数据库。  以下代码是我从网上摘录下来的,仅供参考.


  1. package com.cng.utils;  
  2.   
  3. import java.sql.SQLException;  
  4.   
  5. import android.content.Context;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.util.Log;  
  8.   
  9. import com.cng.modal.Hello;  
  10.   
  11. import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;  
  12. import com.j256.ormlite.dao.Dao;  
  13. import com.j256.ormlite.support.ConnectionSource;  
  14. import com.j256.ormlite.table.TableUtils;  
  15.   
  16. public class DataHelper extends OrmLiteSqliteOpenHelper  
  17. {  
  18.   
  19.     private static final String DATABASE_NAME = "HelloOrmlite.db";  
  20.     private static final int DATABASE_VERSION = 1;  
  21.     private Dao<Hello, Integer> helloDao = null;  
  22.   
  23.     public DataHelper(Context context)  
  24.     {  
  25.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)  
  30.     {  
  31.         try  
  32.         {  
  33.             TableUtils.createTable(connectionSource, Hello.class);  
  34.         } catch (SQLException e)  
  35.         {  
  36.             Log.e(DataHelper.class.getName(), "创建数据库失败", e);  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,  
  43.             int arg2, int arg3)  
  44.     {  
  45.         try  
  46.         {  
  47.             TableUtils.dropTable(connectionSource, Hello.classtrue);  
  48.             onCreate(db, connectionSource);  
  49.         } catch (SQLException e)  
  50.         {  
  51.             Log.e(DataHelper.class.getName(), "更新数据库失败", e);  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.   
  56.     @Override  
  57.     public void close()  
  58.     {  
  59.         super.close();  
  60.         helloDao = null;  
  61.     }  
  62.   
  63.     public Dao<Hello, Integer> getHelloDataDao() throws SQLException  
  64.     {  
  65.         if (helloDao == null)  
  66.         {  
  67.             helloDao = getDao(Hello.class);  
  68.         }  
  69.         return helloDao;  
  70.     }  
  71. }  

 

  1. package com.cng;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.List;  
  5.   
  6. import com.cng.modal.Hello;  
  7. import com.cng.utils.DataHelper;  
  8. import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;  
  9. import com.j256.ormlite.dao.Dao;  
  10.   
  11. import android.os.Bundle;  
  12. import android.widget.TextView;  
  13.   
  14.   
  15.   
  16.   
  17. public class OrmliteLoginActivity extends OrmLiteBaseActivity<DataHelper>  
  18. {  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState)  
  21.     {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         TextView tv = (TextView) this.findViewById(R.id.output);  
  25.         try  
  26.         {  
  27.             Dao<Hello, Integer> helloDao = getHelper().getHelloDataDao();  
  28.             // 添加数据  
  29.             for (int i = 0; i < 2; i++)  
  30.             {  
  31.                 Hello hello = new Hello("Hello" + i);  
  32.                 helloDao.create(hello);  
  33.             }  
  34.             tv.setText(tv.getText() + "\n" + "添加数据完成");  
  35.             // 查询添加的数据  
  36.             List<Hello> hellos = helloDao.queryForAll();  
  37.             for (Hello h : hellos)  
  38.             {  
  39.                 tv.setText(tv.getText() + "\n" + h.toString());  
  40.             }  
  41. //           删除数据第一条数据  
  42.             helloDao.delete(hellos.get(0));  
  43.             tv.setText(tv.getText() + "\n" + "删除数据完成");  
  44.             // 重新查询数据  
  45.             hellos = helloDao.queryForAll();  
  46.             for (Hello h : hellos)  
  47.             {  
  48.                 tv.setText(tv.getText() + "\n" + h.toString());  
  49.             }  
  50.             // 修改数据  
  51.             Hello h1 = hellos.get(0);  
  52.             h1.setWord("这是修改过的数据");  
  53.             tv.setText(tv.getText() + "\n" + "修改数据完成");  
  54.             helloDao.update(h1);  
  55.             // 重新查询数据  
  56.             hellos = helloDao.queryForAll();  
  57.             for (Hello h : hellos)  
  58.             {  
  59.                 tv.setText(tv.getText() + "\n" + h.toString());  
  60.             }  
  61.   
  62.         } catch (SQLException e)  
  63.         {  
  64.             // TODO Auto-generated catch block  
  65.             e.printStackTrace();  
  66.         }  
  67.   
  68.     }  
  69. }  
 
  1. package com.cng.modal;  
  2.   
  3. import android.R.integer;  
  4.   
  5. import com.j256.ormlite.field.DatabaseField;  
  6.   
  7. public class Hello  
  8. {  
  9.     @DatabaseField(generatedId = true,unique=true)  
  10.     int id;  
  11.     @DatabaseField  
  12.     String word;  
  13.     //这是必须加的,否则会出错  
  14.     public Hello(){}  
  15.     public int getId()  
  16.     {  
  17.         return id;  
  18.     }  
  19.     public Hello(String word)  
  20.     {  
  21.         super();  
  22.         this.word = word;  
  23.     }  
  24.     public void setId(int id)  
  25.     {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getWord()  
  30.     {  
  31.         return word;  
  32.     }  
  33.   
  34.     public void setWord(String word)  
  35.     {  
  36.         this.word = word;  
  37.     }  
  38.   
  39.     @Override  
  40.     public String toString()  
  41.     {  
  42.         StringBuilder sb = new StringBuilder();  
  43.         sb.append("id=").append(id);  
  44.         sb.append(" ,word=").append(word);  
  45.         return sb.toString();  
  46.     }  
  47.   
  48. }  

 

 

就这三个类,datahelper是操作数据库的类,可新建,更新表,Hello是一个映射到数据库表的类,具体的看api  文档的下载地址是http://ormlite.com/releases/(其中的jar包也在这里下载)   OrmliteLoginActivity就是activity类,它没继承activity,而是继承了OrmLiteBaseActivity类。


猜你喜欢

转载自blog.csdn.net/LuoXianXion/article/details/7898372