android开发中类似hibernate的orm框架-ormlite

19 篇文章 0 订阅
9 篇文章 0 订阅

为框架挂了,这个问题调了一上午

/**
	 * 一定要在close方法中这里把Dao指向null,不然会出现Most one of id,generatedId,generatedIdSequence
	 */
	@Override
	public void close() {
		// TODO Auto-generated method stub
		super.close();
		specialtyDao = null;
		doctorDao = null;
		departmentDao = null;
		departDoctorDao = null;
	}
	



官网上面有hellodemo,可以用jpa也可以用raw下面的txt文件配置。

DBHelper类:

1.需要继承OrmLiteSqliteOpenHelper类;

2.里面还有一个Dao<Hello,Integer> hellodao的引用数据域变量;

3.需要重写三个方法onCreate,onUpgrade,close;还需要添加hellodao的引用数据域变量的getHelloDao()方法;

package com.zyl.ormLiteDemo;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.zyl.ormLiteDemo.pojo.Hello2;

public class DBHelper extends OrmLiteSqliteOpenHelper {

	private final static String DATABASE_NAME = "MyDB.db";
	private final static int DATABASE_VERSION = 1;
	/**
	 * dao引用
	 */
	private Dao<Hello2, Integer> hello2dao = null;
	
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		// TODO Auto-generated method stub
		try {
			TableUtils.createTable(connectionSource, Hello2.class);
		} catch (SQLException e) {
			Log.e(DBHelper.class.getName(), "创建数据库失败", e);
			e.printStackTrace();
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		try {
			TableUtils.dropTable(connectionSource, Hello2.class, true);
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			Log.e(DBHelper.class.getName(), "更新数据库失败", e);
			e.printStackTrace();
		}

	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		super.close();
		hello2dao = null;
	}
	/**
	 * 
	 * @Title: getHello2Dao 
	 * @Description: TODO(获得dao的调用方法) 
	 * @return
	 * @throws SQLException    设定文件 
	 * @return Dao<Hello2,Integer>    返回类型
	 */
	public Dao<Hello2,Integer> getHello2Dao() throws SQLException{
		if (hello2dao == null) {
			hello2dao = getDao(Hello2.class);
		}
		return hello2dao;
	}

}

Hello2类:

可以用注解来映射,但必须保留Bean的无参数构造器;

package com.zyl.ormLiteDemo.pojo;

import com.j256.ormlite.field.DatabaseField;

public class Hello2 {
	@DatabaseField(generatedId=true)
	private int id;
	@DatabaseField
	private String word;
	public Hello2() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Hello2(String word){
		this.word = word;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("id=").append(id);
		sb.append(" ,word=").append(word);
		return sb.toString();
	}
	
}

Activity类:

有两种方式:一种是继承OrmLiteBaseActivity类,就可以直接使用getHelper()方法无需直接自己写;

                        另外一种是还是继承Android的Activity类,但需要自己提供getHelper()方法;

下为第二种:

package com.zyl.ormLiteDemo;

import java.sql.SQLException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.zyl.ormLiteDemo.pojo.Hello2;
/**
 * 
 * @ClassName: OrmLiteTest 
 * @Description: TODO(测试OrmLite框架类) 
 * @author zyl 
 * @date 2012-6-8 下午7:30:12
 */
public class OrmLiteTest extends Activity {

	private DBHelper dbHelper = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView tv = (TextView) findViewById(R.id.output);
		try {
			Dao<Hello2,Integer> hello2dao = getHelper().getHello2Dao();
			for (int i = 0; i < 2; i++) {
				Hello2 hello2 = new Hello2("Hello2" + i);
				hello2dao.create(hello2);//添加数据
				tv.setText(tv.getText() + "\n" + "添加数据完成");
			}
		} catch (SQLException e) {
			Log.e(OrmLiteTest.class.getName(), "获取hello2Dao失败", e);
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (dbHelper != null) {
			OpenHelperManager.releaseHelper();
			dbHelper = null;
		}
	}
	/**
	 * 
	 * @Title: getHelper 
	 * @Description: TODO(获得自定义数据库小助手类DBHelper) 
	 * @return    设定文件 
	 * @return DBHelper    返回类型
	 */
	public DBHelper getHelper(){
		if (dbHelper == null) {
			dbHelper = OpenHelperManager.getHelper(this, DBHelper.class);
		}
		return dbHelper;
	}
}

第一种:

package com.zyl.ormLiteDemo;

import java.sql.SQLException;
import java.util.List;

import android.os.Bundle;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;
import com.zyl.ormLiteDemo.pojo.Hello;




public class OrmliteLoginActivity extends OrmLiteBaseActivity<DataHelper>
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView tv = (TextView) this.findViewById(R.id.output);
		try
		{
			Dao<Hello, Integer> helloDao = getHelper().getHelloDataDao();
			// 添加数据
			for (int i = 0; i < 2; i++)
			{
				Hello hello = new Hello("Hello" + i);
				helloDao.create(hello);
			}
			tv.setText(tv.getText() + "\n" + "添加数据完成");
			// 查询添加的数据
			List<Hello> hellos = helloDao.queryForAll();
			for (Hello h : hellos)
			{
				tv.setText(tv.getText() + "\n" + h.toString());
			}
//			 删除数据第一条数据
			helloDao.delete(hellos.get(0));
			tv.setText(tv.getText() + "\n" + "删除数据完成");
			// 重新查询数据
			hellos = helloDao.queryForAll();
			for (Hello h : hellos)
			{
				tv.setText(tv.getText() + "\n" + h.toString());
			}
			// 修改数据
			Hello h1 = hellos.get(0);
			h1.setWord("这是修改过的数据");
			tv.setText(tv.getText() + "\n" + "修改数据完成");
			helloDao.update(h1);
			// 重新查询数据
			hellos = helloDao.queryForAll();
			for (Hello h : hellos)
			{
				tv.setText(tv.getText() + "\n" + h.toString());
			}

		} catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

参考1:http://chennaigong.iteye.com/blog/1161867

官方网站:http://ormlite.com/


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值