SlideShare a Scribd company logo
YAPP Advanced Android Study .
Sugar ORM
2013. 7. 25
이준영
13년 7월 26일 금요일
YAPP Advanced Android Study .
ORM 이란? (1)
이미지 출처 : 4 Benefits of Object-Relational Mapping (ORM)
Object-Relational Mapping
객체 지향의 객체(Object) 와
RDB의 데이터를 매핑
13년 7월 26일 금요일
YAPP Advanced Android Study .
ORM 이란? (2)
쉽게 얘기하면, SQL을 사용하지 않고도
DB의 데이터를 쉽게 객체로 만들어 주는 것
WishItem item = new WishItem(itemName, createdTime);
int rowId;
	 	
ContentValues values = new ContentValues();
values.put(WishItemsDbColumns.ITEM_NAME, itemName);
values.put(WishItemsDbColumns.CREATED_TIME, createdTime);
rowId = db.insert(WishItemsDbColumns.TABLE_NAME, null, values);
item.setId(rowId)
WishItem item = new WishItem(itemName,
createdTime);
item.save();
13년 7월 26일 금요일
YAPP Advanced Android Study .
Open Source Android
ORM Libraries
• ActiveAndroid
(https://www.activeandroid.com/)
• SqliteORM
(https://github.com/kremerk/SqliteORM/wiki)
• ORMLite
(http://ormlite.com)
• Storm
(https://code.google.com/p/storm-gen/)
• Green Dao
(http://greendao-orm.com)
• Sugar ORM
(https://github.com/satyan/sugar)
13년 7월 26일 금요일
YAPP Advanced Android Study .
오픈 소스... 마냥 좋은걸까?
13년 7월 26일 금요일
YAPP Advanced Android Study .
시작해 봅시다.
• 다른 ORM Library에 비해 비교적
사용 방법이 단순
• Sugar ORM Libarary 다운로드
https://github.com/satyan/sugar
13년 7월 26일 금요일
YAPP Advanced Android Study .
1.
3.
2.
13년 7월 26일 금요일
YAPP Advanced Android Study .
Sugar ORM 설정 요약
1. 안드로이드 프로젝트 libs 폴더에 라이브러리
를 추가한다.
2. 매니페스트에 Application 클래스를 설정한다
3. 매니페스트에 meta-data 태그를 이용하여 4개
의 프로퍼티를 추가한다.
4. 쓴다.
13년 7월 26일 금요일
YAPP Advanced Android Study .
1. 안드로이드 프로젝트 libs 폴더에 라이브러리를 추가한다.
13년 7월 26일 금요일
YAPP Advanced Android Study .
2. 매니페스트에 Application 클래스를 설정한다
<application
android:name="com.orm.SugarApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="yapp.aa.android.MainActivity_"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
13년 7월 26일 금요일
YAPP Advanced Android Study .
3. 매니페스트에 meta-data 태그를 이용하여
4개의 프로퍼티를 추가한다.
<application ...
// application 내용 생략..
<!-- 메타 데이터 추가 -->
	 <meta-data android:name="DATABASE" android:value="sugar_example.db"/>
	 <meta-data android:name="VERSION" android:value="1"/>
	 <meta-data android:name="QUERY_LOG" android:value="true"/>
	 <meta-data android:name="DOMAIN_PACKAGE_NAME"
	 android:value="yapp.aa.android"/> <!-- 프로젝트의 기본 패키지로 설정-->
</application>
13년 7월 26일 금요일
YAPP Advanced Android Study .
만만한 wishitem 테이블
wishitem tablewishitem table
열 이름 타입
name String
created_time String
13년 7월 26일 금요일
YAPP Advanced Android Study .
WishItem Class
• 테이블 (릴레이션)과 매핑할 클래스 작성
• com.org.SugarRecord를 상속해야 함
• Context 를 매개변수로 하는 생성자 작성
13년 7월 26일 금요일
YAPP Advanced Android Study .
com.orm.SugarRecord 상속
Constructors from superclass 체크
13년 7월 26일 금요일
YAPP Advanced Android Study .
public class WishItem extends SugarRecord {
	 private String name;
	
	 private String createdTime;
	 public WishItem(Context context) {
	 	 super(context);
	 }
	 public WishItem(Context context, String name, String createdTime) {
	 	 super(context);
	 	 this.name = name;
	 	 this.createdTime = createdTime;
	 }
	 public String getName() {
	 	 return name;
	 }
	 public void setName(String name) {
	 	 this.name = name;
	 }
	 public String getCreatedTime() {
	 	 return createdTime;
	 }
	 public void setCreatedTime(String createdTime) {
	 	 this.createdTime = createdTime;
	 }
}
name, createdTime 필드 추가
생성자, 게터/세터 추가
13년 7월 26일 금요일
YAPP Advanced Android Study .
SugarRecord
methods
• save()
DB에 없는 경우 insert,
DB에 있는 경우 update
• delete()
row 삭제
• get/set Id()
row ID get/set
13년 7월 26일 금요일
YAPP Advanced Android Study .
SugarRecord
static methods
13년 7월 26일 금요일
YAPP Advanced Android Study .
http://satyan.github.io/sugar/ 영어지만 쉽게 되어있음
13년 7월 26일 금요일

More Related Content

Yapp a.a 2 2 sugar orm

  • 1. YAPP Advanced Android Study . Sugar ORM 2013. 7. 25 이준영 13년 7월 26일 금요일
  • 2. YAPP Advanced Android Study . ORM 이란? (1) 이미지 출처 : 4 Benefits of Object-Relational Mapping (ORM) Object-Relational Mapping 객체 지향의 객체(Object) 와 RDB의 데이터를 매핑 13년 7월 26일 금요일
  • 3. YAPP Advanced Android Study . ORM 이란? (2) 쉽게 얘기하면, SQL을 사용하지 않고도 DB의 데이터를 쉽게 객체로 만들어 주는 것 WishItem item = new WishItem(itemName, createdTime); int rowId; ContentValues values = new ContentValues(); values.put(WishItemsDbColumns.ITEM_NAME, itemName); values.put(WishItemsDbColumns.CREATED_TIME, createdTime); rowId = db.insert(WishItemsDbColumns.TABLE_NAME, null, values); item.setId(rowId) WishItem item = new WishItem(itemName, createdTime); item.save(); 13년 7월 26일 금요일
  • 4. YAPP Advanced Android Study . Open Source Android ORM Libraries • ActiveAndroid (https://www.activeandroid.com/) • SqliteORM (https://github.com/kremerk/SqliteORM/wiki) • ORMLite (http://ormlite.com) • Storm (https://code.google.com/p/storm-gen/) • Green Dao (http://greendao-orm.com) • Sugar ORM (https://github.com/satyan/sugar) 13년 7월 26일 금요일
  • 5. YAPP Advanced Android Study . 오픈 소스... 마냥 좋은걸까? 13년 7월 26일 금요일
  • 6. YAPP Advanced Android Study . 시작해 봅시다. • 다른 ORM Library에 비해 비교적 사용 방법이 단순 • Sugar ORM Libarary 다운로드 https://github.com/satyan/sugar 13년 7월 26일 금요일
  • 7. YAPP Advanced Android Study . 1. 3. 2. 13년 7월 26일 금요일
  • 8. YAPP Advanced Android Study . Sugar ORM 설정 요약 1. 안드로이드 프로젝트 libs 폴더에 라이브러리 를 추가한다. 2. 매니페스트에 Application 클래스를 설정한다 3. 매니페스트에 meta-data 태그를 이용하여 4개 의 프로퍼티를 추가한다. 4. 쓴다. 13년 7월 26일 금요일
  • 9. YAPP Advanced Android Study . 1. 안드로이드 프로젝트 libs 폴더에 라이브러리를 추가한다. 13년 7월 26일 금요일
  • 10. YAPP Advanced Android Study . 2. 매니페스트에 Application 클래스를 설정한다 <application android:name="com.orm.SugarApp" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="yapp.aa.android.MainActivity_" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 13년 7월 26일 금요일
  • 11. YAPP Advanced Android Study . 3. 매니페스트에 meta-data 태그를 이용하여 4개의 프로퍼티를 추가한다. <application ... // application 내용 생략.. <!-- 메타 데이터 추가 --> <meta-data android:name="DATABASE" android:value="sugar_example.db"/> <meta-data android:name="VERSION" android:value="1"/> <meta-data android:name="QUERY_LOG" android:value="true"/> <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="yapp.aa.android"/> <!-- 프로젝트의 기본 패키지로 설정--> </application> 13년 7월 26일 금요일
  • 12. YAPP Advanced Android Study . 만만한 wishitem 테이블 wishitem tablewishitem table 열 이름 타입 name String created_time String 13년 7월 26일 금요일
  • 13. YAPP Advanced Android Study . WishItem Class • 테이블 (릴레이션)과 매핑할 클래스 작성 • com.org.SugarRecord를 상속해야 함 • Context 를 매개변수로 하는 생성자 작성 13년 7월 26일 금요일
  • 14. YAPP Advanced Android Study . com.orm.SugarRecord 상속 Constructors from superclass 체크 13년 7월 26일 금요일
  • 15. YAPP Advanced Android Study . public class WishItem extends SugarRecord { private String name; private String createdTime; public WishItem(Context context) { super(context); } public WishItem(Context context, String name, String createdTime) { super(context); this.name = name; this.createdTime = createdTime; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCreatedTime() { return createdTime; } public void setCreatedTime(String createdTime) { this.createdTime = createdTime; } } name, createdTime 필드 추가 생성자, 게터/세터 추가 13년 7월 26일 금요일
  • 16. YAPP Advanced Android Study . SugarRecord methods • save() DB에 없는 경우 insert, DB에 있는 경우 update • delete() row 삭제 • get/set Id() row ID get/set 13년 7월 26일 금요일
  • 17. YAPP Advanced Android Study . SugarRecord static methods 13년 7월 26일 금요일
  • 18. YAPP Advanced Android Study . http://satyan.github.io/sugar/ 영어지만 쉽게 되어있음 13년 7월 26일 금요일