sqlite 数据库驱动框架

面向对象数据库框架设计 

public void saveUser(User user){
	DBHelper dbHelper=new DBHelper(this);
	SQLiteDatabase db=dbHelper.getWritableDatabase();
	db.execSQL("create table if not exists db_user(name,varchar(20),password varchar(10),)");
	ContentValues values=new ContentValues();
	values.put("name",user.getName());
	values.put("password",user.getPassword());
	db.beginTransaction();
	int scheduleID=-1;
	try{
	db.insert("schedule",null,values);
	db.setTransactionSuccessful();
	}finally{
	}
	db.endTransaction();
	db.close();
}

 

sqlite 数据库驱动框架_第1张图片

1.调用层不关心数据的创建和在哪里创建
2.关心表的创建
3.不关心sql语句的拼写,对象丢给你,你帮我存入数据库

hashMap=getValues(User)
ContentValues values=getContentValues();

目录结构

db--|
db--|---annotion(注解目录)
db--|-------|
db--|-----DbTable
 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DbTable(){
	String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DbField{
	String value();
}

public interface IBaseDao{
	Long insert(T entity);
	Long update(T entity,T where);
	delete();	
	select();
}
public abstract class BaseDao implements IBaseDao{
	private SQLiteDatabase database;
	protected boolean init(Class entity,SQLiteDatabase sqLiteDatabase){
		database=sqLiteDatabase;
	}
	public Long insert(T entity){
		return null;
	}
	public Long update(T entity,T where){
		return null;
	}
}
public class BaseDaoFactory{
	private String sqliteDatabasePath;
	private SQLiteDatabase sqlLiteDatabase;
	public BaseDaoFactory(){
		sqliteDatabasePath=Enviroment.getDataDirectory().getAbsolutePath()+"user.db";
		openDatabase();
	}	
	public void openDatabase(){
		this.sqLiteDatabase=SQLiteDatabase.openOrCreateDatabase(sqliteDatabasePath,null);
	}
	public synchronized,M> T getDataHelper(Class class,Class  entityClass );
	BaseDao baseDao=null;
	try{
		baseDao=Class.newInstance();
		baseDao.init();
	}catch(){
	
	}
}

@DbTable("tb_common_user")
public class User{
	@DbField("tb_name")
	public String name;

	@DbField("tb_password")
	public String password;
}

 

你可能感兴趣的:(java)