读书人

android orm投射框架(类似hibernate)

发布时间: 2012-09-10 11:02:33 作者: rapoo

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

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


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

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

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


读书人网 >Android

热点推荐