[置顶] android orm映射框架(类似hibernate)基本使用
16lz
2021-12-04
android orm映射框架,可像hibernate一样操作数据库。 以下代码是我从网上摘录下来的,仅供参考.
- packagecom.cng.utils;
- importjava.sql.SQLException;
- importandroid.content.Context;
- importandroid.database.sqlite.SQLiteDatabase;
- importandroid.util.Log;
- importcom.cng.modal.Hello;
- importcom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
- importcom.j256.ormlite.dao.Dao;
- importcom.j256.ormlite.support.ConnectionSource;
- importcom.j256.ormlite.table.TableUtils;
- publicclassDataHelperextendsOrmLiteSqliteOpenHelper
- {
- privatestaticfinalStringDATABASE_NAME="HelloOrmlite.db";
- privatestaticfinalintDATABASE_VERSION=1;
- privateDao<Hello,Integer>helloDao=null;
- publicDataHelper(Contextcontext)
- {
- super(context,DATABASE_NAME,null,DATABASE_VERSION);
- }
- @Override
- publicvoidonCreate(SQLiteDatabasedb,ConnectionSourceconnectionSource)
- {
- try
- {
- TableUtils.createTable(connectionSource,Hello.class);
- }catch(SQLExceptione)
- {
- Log.e(DataHelper.class.getName(),"创建数据库失败",e);
- e.printStackTrace();
- }
- }
- @Override
- publicvoidonUpgrade(SQLiteDatabasedb,ConnectionSourceconnectionSource,
- intarg2,intarg3)
- {
- try
- {
- TableUtils.dropTable(connectionSource,Hello.class,true);
- onCreate(db,connectionSource);
- }catch(SQLExceptione)
- {
- Log.e(DataHelper.class.getName(),"更新数据库失败",e);
- e.printStackTrace();
- }
- }
- @Override
- publicvoidclose()
- {
- super.close();
- helloDao=null;
- }
- publicDao<Hello,Integer>getHelloDataDao()throwsSQLException
- {
- if(helloDao==null)
- {
- helloDao=getDao(Hello.class);
- }
- returnhelloDao;
- }
- }
- packagecom.cng;
- importjava.sql.SQLException;
- importjava.util.List;
- importcom.cng.modal.Hello;
- importcom.cng.utils.DataHelper;
- importcom.j256.ormlite.android.apptools.OrmLiteBaseActivity;
- importcom.j256.ormlite.dao.Dao;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassOrmliteLoginActivityextendsOrmLiteBaseActivity<DataHelper>
- {
- @Override
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextViewtv=(TextView)this.findViewById(R.id.output);
- try
- {
- Dao<Hello,Integer>helloDao=getHelper().getHelloDataDao();
- //添加数据
- for(inti=0;i<2;i++)
- {
- Hellohello=newHello("Hello"+i);
- helloDao.create(hello);
- }
- tv.setText(tv.getText()+"\n"+"添加数据完成");
- //查询添加的数据
- List<Hello>hellos=helloDao.queryForAll();
- for(Helloh:hellos)
- {
- tv.setText(tv.getText()+"\n"+h.toString());
- }
- //删除数据第一条数据
- helloDao.delete(hellos.get(0));
- tv.setText(tv.getText()+"\n"+"删除数据完成");
- //重新查询数据
- hellos=helloDao.queryForAll();
- for(Helloh:hellos)
- {
- tv.setText(tv.getText()+"\n"+h.toString());
- }
- //修改数据
- Helloh1=hellos.get(0);
- h1.setWord("这是修改过的数据");
- tv.setText(tv.getText()+"\n"+"修改数据完成");
- helloDao.update(h1);
- //重新查询数据
- hellos=helloDao.queryForAll();
- for(Helloh:hellos)
- {
- tv.setText(tv.getText()+"\n"+h.toString());
- }
- }catch(SQLExceptione)
- {
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }
- packagecom.cng.modal;
- importandroid.R.integer;
- importcom.j256.ormlite.field.DatabaseField;
- publicclassHello
- {
- @DatabaseField(generatedId=true,unique=true)
- intid;
- @DatabaseField
- Stringword;
- //这是必须加的,否则会出错
- publicHello(){}
- publicintgetId()
- {
- returnid;
- }
- publicHello(Stringword)
- {
- super();
- this.word=word;
- }
- publicvoidsetId(intid)
- {
- this.id=id;
- }
- publicStringgetWord()
- {
- returnword;
- }
- publicvoidsetWord(Stringword)
- {
- this.word=word;
- }
- @Override
- publicStringtoString()
- {
- StringBuildersb=newStringBuilder();
- sb.append("id=").append(id);
- sb.append(",word=").append(word);
- returnsb.toString();
- }
- }
就这三个类,datahelper是操作数据库的类,可新建,更新表,Hello是一个映射到数据库表的类,具体的看api 文档的下载地址是http://ormlite.com/releases/(其中的jar包也在这里下载) OrmliteLoginActivity就是activity类,它没继承activity,而是继承了OrmLiteBaseActivity类。
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- 安卓9.0 http请求数据失败解决办法
- Android保存32位BMP格式图片
- Android(安卓)基础UI编程2
- android之launcher时序图and图标的建立
- android bluetooth 移植相关注意事项