이걸로 xml만들어놓으면 그나마 나중에 resultMapping시킬때 편할것 같아 하나 만들어 봤음.
* 완전편한건아니지만 그나마 쓸만함.
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.hynix.ecms.login.model.LoginDTO;
public class CreateXMLDemo {
/**
* Reflection(리플레션)사용하여, Class Field 로 XML 문서 형태 만들기
* @return
* @throws Exception
*/
protected static String className="";
private static Document createDocument() throws Exception {
/*
* resultMap XML description
*/
LoginDTO loginDto = new LoginDTO();
Class<? extends LoginDTO> cls = loginDto.getClass();
Field[] fields = cls.getDeclaredFields();
List<String> fieldNamesList = new ArrayList<String>();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
fieldNamesList.add(field.getName());
}
for(int i=0;i<cls.getSimpleName().length();i++){
String str = String.valueOf(cls.getSimpleName().charAt(i));
switch (i) {
case 0:
className += str.toLowerCase();
break;
default:
className += str;
break;
}
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("resultMap");
root.addAttribute("id", className);
root.addAttribute("class", cls.getName());
for (String fieldNames : fieldNamesList) {
Element result = root.addElement("result");
result.addAttribute("property", fieldNames);
result.addAttribute("column", "");
//result.addText("TextContents");
}
return document;
}
/**
* XML 문서 파일로 쓰기,
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String locationPath = System.getProperty("user.dir");
Document document = createDocument();
OutputFormat format = new OutputFormat("\t", true);
FileWriter out = new FileWriter(locationPath + "/WebContent/z.XML_SAMPLE/"+className+".xml");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
out.close();
System.out.println("파일이 생성되었습니다." + locationPath);
}
}
LoginDTO Class는 Model로써 대충 아무거나 끄적이면 되는거니 따로 적지 않겠다?라고 하려 했으나,
행여 삽질할 사람들을 위해 일부 적어 놓겠음
*LoginDTO
* $Id: LoginDTO.java,v 1.2 2010/03/24 18:07:10 smrscvs3 Exp $
* created by : AN SANG KIL
* creation-date : 2010. 3. 16.
* =========================================================
* Copyright (c) 2010 ManInSoft, Inc. All rights reserved.
*/
package com.hynix.ecms.login.model;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* ��� DTO
* @author AN SANG KIL
* @version $Id: LoginDTO.java,v 1.2 2010/03/24 18:07:10 smrscvs3 Exp $
*/
public class LoginDTO {
private String access_id;
private String access_pass;
private String code;
private String area1;
private String area2;
private String area3;
//... getter, setter는 생성하시오
}
나중에 DB Connection 까지해서 DB Field명까지 이어준다면 완성된 resultMap xml이 생성되겠네.
'Spring' 카테고리의 다른 글
Spring Quartz 1-1 (0) | 2010.12.14 |
---|---|
Spring Timer Sample (0) | 2010.12.14 |
Mail - Spring JavaMailSender(2/2) - Mail sender Test (0) | 2010.09.14 |
Junit Spring ApplicationContext (Applacation Bean Test, Spring Bean Call) (0) | 2010.09.08 |
SpringFramework *.property 사용방법 (0) | 2010.05.06 |