AndroidGreenDao 는 개인 적 인 소감 을 사용 합 니 다.

11262 단어 android
AndroidGreenDao 는 사용 하기에 편리 하 다 고 생각 합 니 다. 전체적으로 제3자 jar 가방 이 편리 하지 않 습 니 다. 제 가 게 으 른 것 을 용서해 주세요. 자, 이제 용법 을 말씀 드 리 겠 습 니 다.
우선 우 리 는 자바 프로젝트 를 건설 해 야 한다. 예 를 들 어 DaoExampleGenerator
libs 폴 더 를 만 들 고 jar 를 저장 합 니 다.
여기 jar 2 개가 필요 해 요.  freemarker - 2.3.21. jar greendao - generator - 1.3.1. jar, 예 를 들 어 2 개의 jar 를 그 안에 끌 어 옵 니 다.
http://search.maven.org/#search%7Cga%7C1%7CgreenDao 
greendao - generator - 1.3.0. jar 와 greendao - 1.3.7. jar 를 다운로드 합 니 다.
 
http://mvnrepository.com/artifact/org.freemarker/freemarker
freemarker - 2.3.20. jar 다운로드
/*
 * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.greenrobot.daogenerator.gentest;

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany;

/**
 * Generates entities and DAOs for the example project DaoExample.
 * 
 * Run it as a Java application (not Android).
 * 
 * @author Markus
 */
public class ExampleDaoGenerator {

	public static void main(String[] args) throws Exception {
//		//                   ,          DAO      。
//		Schema schema = new Schema(1000, "com.example.androidgreendao");
//		//             DAO            。
//		addNote(schema);
//		addCustomerOrder(schema);
//		//  src-gen               ,     。
//		new DaoGenerator().generateAll(schema, "../AndroidGreenDao/greendao");
		
		
		 Schema schema = new Schema(1, "com.example.androidgreendao.mode");
		 
	        addNote(schema);
	        addCustomerOrder(schema);
	        // set dao class generate package
	        schema.setDefaultJavaPackageDao("com.example.androidgreendao.dao");
	        // keep custom code block
	        schema.enableKeepSectionsByDefault();
	        new DaoGenerator().generateAll(schema, "../AndroidGreenDao/src");//     android   src   
	}

//	private static void addNote(Schema schema) {
//		//       
//		Entity note = schema.addEntity("Note");
//		note.addIdProperty();
//		note.addStringProperty("text").notNull();
//		note.addStringProperty("comment");
//		note.addDateProperty("date");
//	}
//
//	private static void addCustomerOrder(Schema schema) {
//		Entity customer = schema.addEntity("Customer");
//		customer.addIdProperty();
//		customer.addStringProperty("name").notNull();
//
//		Entity order = schema.addEntity("Order");
//		//        ,        
//		/**
//		 * greenDAO               ,
//		 *       。
//		 *                           。
//		 *                     ,    Java            。
//		 *   ,    “CREATIONDATE”           “CREATION_DATE”。
//		 */
//		order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
//		order.addIdProperty();
////		       ID    :
////		dao.addIdProperty().primaryKey().autoincrement();
//		//           :
//		Property orderDate = order.addDateProperty("date").getProperty();
//		Property customerId = order.addLongProperty("customerId").notNull()
//				.getProperty();
//		order.addToOne(customer, customerId);
//
//		ToMany customerToOrders = customer.addToMany(order, customerId);
//		customerToOrders.setName("orders");
//		customerToOrders.orderAsc(orderDate);
//	}

	  private static void addNote(Schema schema)
	    {
	        Entity note = schema.addEntity("Note");
	        note.addIdProperty();
	        note.addStringProperty("text").notNull();
	        note.addStringProperty("comment");
	        note.addDateProperty("date");
	    }
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
	    private static void addCustomerOrder(Schema schema)
	    {
	        Entity customer = schema.addEntity("Customer");
	        customer.addIdProperty();
	        customer.addStringProperty("name").notNull();
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
	        Entity order = schema.addEntity("Order");
	        order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
	        order.addIdProperty();
	        Property orderDate = order.addDateProperty("date").getProperty();
	        Property customerId = order.addLongProperty("customerId").notNull().getProperty();
	        order.addToOne(customer, customerId);
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
	        ToMany customerToOrders = customer.addToMany(order, customerId);
	        customerToOrders.setName("orders");
	        customerToOrders.orderAsc(orderDate);
	    }
}

그리고 당신 의 안 드 로 이 드 프로젝트 를 만 듭 니 다. 예 를 들 어 AndroidGreenDao 는 greendao - 1.3.7. jar 를 실행 자바 로 가 져 옵 니 다. 안 드 로 이 드 프로젝트 를 새로 고치 면 dao 와 mode 패 키 지 를 볼 수 있 습 니 다. 안에 8 개의 파일 이 있 습 니 다. 그렇지 않 으 면 오 류 를 보고 할 수 있 습 니 다. 여 기 는 자바 오 류 를 보고 할 수 있 습 니 다. 폴 더 를 찾 을 수 없다 고 할 수 있 습 니 다
응용 프로그램 에서 DaoMaster 와 DaoSession 을 얻 는 방법 을 실현 하고 프로젝트 의 응용 프로그램 name
package com.example.androidgreendao;

import android.app.Application;
import android.content.Context;

import com.example.androidgreendao.dao.DaoMaster;
import com.example.androidgreendao.dao.DaoMaster.OpenHelper;
import com.example.androidgreendao.dao.DaoSession;
import com.example.androidgreendao.vo.Constants;

public class MyApplication extends Application {
	 
	private static MyApplication mInstance;
	private static DaoMaster daoMaster;
	private static DaoSession daoSession;
	
	@Override
	public void onCreate() {
		super.onCreate();
		if(mInstance == null)
			mInstance = this;
	}
	
	/**
	 *   DaoMaster
	 * 
	 * @param context
	 * @return
	 */
	public static DaoMaster getDaoMaster(Context context) {
		if (daoMaster == null) {
			OpenHelper helper = new DaoMaster.DevOpenHelper(context,Constants.DB_NAME, null);
			daoMaster = new DaoMaster(helper.getWritableDatabase());
		}
		return daoMaster;
	}
	
	/**
	 *   DaoSession
	 * 
	 * @param context
	 * @return
	 */
	public static DaoSession getDaoSession(Context context) {
		if (daoSession == null) {
			if (daoMaster == null) {
				daoMaster = getDaoMaster(context);
			}
			daoSession = daoMaster.newSession();
		}
		return daoSession;
	}
}

데이터베이스 이름 을 저장 할 Constants 클래스 를 만 듭 니 다.
package com.example.androidgreendao.vo;

public class Constants {  
    public static final String DB_NAME = "note_db";  
}  

DBHelper 를 쓰 지 않 으 면 Activity 에서... 이렇게 생 겼 어 요.
조회 표 의 앞 네 가지
		MyApplication app = (MyApplication) getApplication();
		DaoSession daoSession = app.getDaoSession(this);
		CustomerDao customerDao = daoSession.getCustomerDao();

		ListView lv = (ListView) findViewById(R.id.listView1);
		List data = new ArrayList();
        data.add(customerDao.loadAll().get(0).getName());
        data.add(customerDao.loadAll().get(1).getName());
        data.add(customerDao.loadAll().get(2).getName());
        data.add(customerDao.loadAll().get(3).getName());
		lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,data));
//		for (int i = 0; i < 10; i++) {
//			Customer customer = new Customer();
//			customer.setName(" S" + i);
//			customerDao.insertOrReplace(customer);
//		}

끼어들다
물론 정규 표기 법 은 DBHelper 를 사용 해 야 한다.
        MyApplication app = (MyApplication) getApplication();
		DaoSession daoSession = app.getDaoSession(this);
		CustomerDao customerDao = daoSession.getCustomerDao();

	
		
		for (int i = 0; i < 10; i++) {
			Customer customer = new Customer();
			customer.setName(" S" + i);
			customerDao.insertOrReplace(customer);
		}
	

activity 에서 실례 화 된 후에 바로 사용 할 수 있 습 니 다. 속도 와 효율 은 말 하지 않 겠 습 니 다. 의견 을 교환 하고 최적화 코드 를 도 울 수 있 기 를 바 랍 니 다.
파일 다운로드 도 해 드 렸 고요.

좋은 웹페이지 즐겨찾기