因为这个项目的业务层很薄,因此想在架构上尽量保持着「轻」,不会把创建DbHelper的interface。
而是直接用DAO创建DbHelper对象。
DAO和DbHelper也是同样使用懒汉模式。
1. UserDAO懒汉模式。
public class UserDAO { private Configuration config = new Configuration(); protected SQLiteDatabase db; private DbHelper dbHelper; public UserDAO(Context context) { this.dbHelper = DbHelper.getInstance(context); this.db = this.dbHelper.getWritableDatabase(); } }
2. DAO负责处理Query SQlite的业务。这里分别有6个方法。
1. boolean setUser() 2. UserDTO getUser() 3. int getUserCount() 4. boolean setUserPortrait(Bitmap) 5. Bitmap getUserPortrait() 6. byte[] getUserPortraitBYTE()
3. 完整的DAO代码如下。
package com.paradise.electronic.eparadise2.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.paradise.electronic.eparadise2.dto.UserDTO; import com.paradise.electronic.eparadise2.sqlite.DbHelper; import com.paradise.electronic.eparadise2.util.Configuration; import java.io.ByteArrayOutputStream; /** * Created by Administrator on 17-11-17. */ public class UserDAO { private Configuration config = new Configuration(); protected SQLiteDatabase db; private DbHelper dbHelper; public UserDAO(Context context) { this.dbHelper = DbHelper.getInstance(context); this.db = this.dbHelper.getWritableDatabase(); } /* * @return 1: Successful 0: Failed */ public boolean setUser (UserDTO user) { ContentValues values = new ContentValues(); boolean result = true; values.put(config.USER_USERID, user.getId()); values.put(config.USER_NAME, user.getName()); values.put(config.USER_PASSWORD, user.getPassword()); values.put(config.USER_EMAIL, user.getEmail()); values.put(config.USER_DESCRIPTION, user.getDescription()); values.put(config.USER_TEAM, user.getTeam()); values.put(config.USER_ACADEMIC, user.getAcademic()); values.put(config.USER_PORTRAIT, user.getPortrait()); values.put(config.USER_REGISTER_DATE, user.getRegisterDate()); try { db.insert(config.DB_USER, null, values); } catch (SQLException e) { result = false; } return result; } /* * @return UserDTO */ public UserDTO getUser() { String query = " SELECT "+config.USER_USERID+ ", "+config.USER_NAME+ ", "+config.USER_PASSWORD+ ", "+config.USER_EMAIL+ ", "+config.USER_DESCRIPTION+ ", "+config.USER_TEAM+ ", "+config.USER_ACADEMIC+ ", "+config.USER_PORTRAIT+ ", "+config.USER_REGISTER_DATE+ " FROM "+config.DB_USER+ " ORDER BY "+config.USER_ID+" DESC LIMIT 1 "; Cursor cursor = db.rawQuery(query, null); if (cursor != null) { cursor.moveToFirst(); } UserDTO user = new UserDTO(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8)); return user; } public void updateUser() { } public void deleteUser() { } public int getUserCount() { String query = " SELECT * FROM "+config.DB_USER; Cursor cursor = db.rawQuery(query, null); int count = cursor.getCount(); return count; } /* * @param [Bitmap]portrait * @return 1: Successful 0: Failed */ public boolean setUserPortrait(Bitmap portrait) { ContentValues values = new ContentValues(); boolean result = true; ByteArrayOutputStream stream = new ByteArrayOutputStream(); portrait.compress(Bitmap.CompressFormat.JPEG, 0, stream); values.put(config.USER_PORTRAIT_DATA, stream.toByteArray()); try { db.insert(config.DB_USER, null, values); } catch (SQLException e) { result = false; } return result; } /* * @return [Bitmap]portrait */ public Bitmap getUserPortrait() { String query = " SELECT "+config.USER_USERID+ ", "+config.USER_PORTRAIT_DATA+ " FROM "+config.DB_USER+ " ORDER BY "+config.USER_ID+" DESC LIMIT 1 "; Cursor cursor = db.rawQuery(query, null); if (cursor != null) { cursor.moveToFirst(); } byte[] bytePortrait = cursor.getBlob(0); Bitmap portrait = BitmapFactory.decodeByteArray(bytePortrait, 0, bytePortrait.length); return portrait; } /* * @reyurn [byte]protrait */ public byte[] getUserPortraitBYTE() { String query = " SELECT "+config.USER_USERID+ ", "+config.USER_PORTRAIT_DATA+ " FROM "+config.DB_USER+ " ORDER BY "+config.USER_ID+" DESC LIMIT 1 "; Cursor cursor = db.rawQuery(query, null); if (cursor != null) { cursor.moveToFirst(); } return cursor.getBlob(0); } }