GreenDao 공식 자습서 확장판(2)

1801 단어
GreenDao 공식 자습서 확장판(1)
지난번에 그린다이아 생성 프로젝트 다이오의 내용을 제공했는데 이번에는 주로 다이오의 인용으로 다이오가 조작하는 것을 잘 아는 어린이 신발은 스스로 생략할 수 있다.
1. 의존 추가
두 가지 방법
1. Android Studio에서 Maven Central 검색을 사용하여 추가:
[Project Structure] --> [Your Module Name] --> [Dependencies] --> [Library Dependencies]가 Maven Central 검색 상자에서 검색greenDao하고 선택de.greenrobot:greendao:2.1.0합니다.
2.build을 직접 수정합니다.gradle
구체적인 절차는 앞의 문장을 참고하시오
2. 생성된 Dao 클래스 호출
공식 Example 참조
  • 데이터 테이블 생성 new DaoMaster.DevOpenHelper(this, "notes-db", null)
  • 데이터베이스 연결daoMaster 구축 = new DaoMaster(db).daoSession = daoMaster.newSession(); noteDao = daoSession.getNoteDao();
  • 구체적 조작private void addNote() {String noteText = editText.getText().toString(), editText.setText("),
      final DateFormat df =  DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    
      String comment = "Added on " + df.format(new Date());
    
      Note note = new Note(null, noteText, comment, new Date());
    
      noteDao.insert(note);//     
    
      Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
    
      cursor.requery();
    
    }
    
  • insert 외에update,delete 방법이 제공되고, 조회는 getWritableDatabase()로 대상 조작을 획득한다.DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null); SQLiteDatabase noteDb = helper.getWritableDatabase(); Cursor cursor = noteDb.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);//query 구문은 스스로 Google or Baidu
    3. 마지막 건의는 구조의 느슨한 결합을 감안하여 가능한 한 자신의 클래스로 이런Dao류를 봉인하는 것을 권장한다. 그렇지 않으면 후기 유지보수가 매우 어려울 수 있다.
    참조:
  • 그린다오 홈페이지
  • GreenDaoExample
  • 좋은 웹페이지 즐겨찾기