public class DepartmentDAO { private SqlMapClientTemplate sqlMap; public DepartmentDAO(SqlMapClientTemplate sqlMap){ this.sqlMap = sqlMap; } public void insertDepartment(DepartmentDTO dto){ sqlMap.insert("insertDepartment", dto); System.out.println("1개의 부서가 삽입되었습니다."+dto); } public int updateDepartment(DepartmentDTO dto){ int cnt = sqlMap.update("updateDepartment", dto); return cnt; } public int deleteDepartmentById(String departmentId){ int cnt = sqlMap.delete("deleteDepartment", departmentId); return cnt;
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((departmentId == null) ? 0 : departmentId.hashCode()); result = prime * result + ((departmentName == null) ? 0 : departmentName.hashCode()); result = prime * result + ((location == null) ? 0 : location.hashCode()); return result; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; DepartmentDTO other = (DepartmentDTO) obj; if (departmentId == null) { if (other.departmentId != null) return false; } else if (!departmentId.equals(other.departmentId)) return false; if (departmentName == null) { if (other.departmentName != null) return false; } else if (!departmentName.equals(other.departmentName)) return false; if (location == null) { if (other.location != null) return false; } else if (!location.equals(other.location)) return false; return true; }
public class TestDepartment { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("dept/config/spring.xml"); DepartmentDAO dao = (DepartmentDAO)ctx.getBean("departmentDAO"); DepartmentDTO ddto = new DepartmentDTO("D0007","개발부","울산지점");
public class DepartmentDAO { private SqlMapClientTemplate sqlMap; public DepartmentDAO(SqlMapClientTemplate sqlMap){ this.sqlMap = sqlMap; } public void insertDepartment(DepartmentDTO dto){ System.out.println(sqlMap); } public int updateDepartment(DepartmentDTO dto){ return 0;
} public int deleteDepartmentById(String departmentId){ return 0;
} public DepartmentDTO selectDepartmentById(String departmentId){ return null;
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((departmentId == null) ? 0 : departmentId.hashCode()); result = prime * result + ((departmentName == null) ? 0 : departmentName.hashCode()); result = prime * result + ((location == null) ? 0 : location.hashCode()); return result; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; DepartmentDTO other = (DepartmentDTO) obj; if (departmentId == null) { if (other.departmentId != null) return false; } else if (!departmentId.equals(other.departmentId)) return false; if (departmentName == null) { if (other.departmentName != null) return false; } else if (!departmentName.equals(other.departmentName)) return false; if (location == null) { if (other.location != null) return false; } else if (!location.equals(other.location)) return false; return true; }
public class CollectionDTO { private List myList; private String[] names; private Set mySet; private Map myMap; private Properties myProps; public List getMyList() { return myList; }
public void setMyList(List myList) { this.myList = myList; }
public String[] getNames() { return names; }
public void setNames(String[] names) { this.names = names; }
public Set getMySet() { return mySet; }
public void setMySet(Set mySet) { this.mySet = mySet; }
public Map getMyMap() { return myMap; }
public void setMyMap(Map myMap) { this.myMap = myMap; }
public Properties getMyProps() { return myProps; }
public void setMyProps(Properties myProps) { this.myProps = myProps; }
}
package di.dto;
public class DirectorDTO { private String directorId; private String name; private int age; public DirectorDTO() { super(); } public DirectorDTO(String directorId, String name, int age) { super(); this.directorId = directorId; this.name = name; this.age = age; }
public String getDirectorId() { return directorId; } public void setDirectorId(String directorId) { this.directorId = directorId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "DirectorDTO [directorId=" + directorId + ", name=" + name + ", age=" + age + "]"; }
public class TestMovie { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("di/movie/config/movie.xml"); System.out.println("------director--------"); DirectorDTO dto = (DirectorDTO)ctx.getBean("director1"); System.out.println(dto); dto = (DirectorDTO)ctx.getBean("director2"); System.out.println(dto); dto = (DirectorDTO)ctx.getBean("director3"); System.out.println(dto);
System.out.println("------movie--------"); MovieDTO mto = (MovieDTO)ctx.getBean("movie1"); System.out.println(mto); mto = (MovieDTO)ctx.getBean("movie2"); System.out.println(mto); mto = (MovieDTO)ctx.getBean("movie3"); System.out.println(mto);
} }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- property 이용 --> <!-- Director 객체 생성 후 값 주입 --> <!-- property 태그 - 하위태그 이용해 값 설정 --> <bean id="director1" class="di.movie.dto.DirectorDTO"> <property name="directorId"> <value>d-0001</value> </property> <property name="name"> <value>임권택</value> </property> <property name="age"> <value>28</value> </property> </bean> <!-- property 태그 - 속성을 이용해 값 설정 --> <bean id="director2" class="di.movie.dto.DirectorDTO"> <property name="directorId" value="d-0001"/> <property name="name" value="임권택"/> <property name="age" value="28"/> </bean> <!-- property 태그 - p : namespace 이용해 값 설정 --> <bean id="director3" class="di.movie.dto.DirectorDTO" p:directorId="d-0001" p:name="임권택" p:age="28"/>
<!--MovieDTO 객체 생성 후 값 주입 --> <!-- property 태그 - 하위태그 이용해 값 설정 --> <bean id="movie1" class="di.movie.dto.MovieDTO"> <property name="movieId"> <value>0001</value> </property> <property name="title"> <value>간기남</value> </property> <property name="runningTime"> <value>60</value> </property> <property name="genre"> <value>멜로</value> </property> <property name="director" > <ref bean="director1"/> </property> </bean> <!-- property 태그 - 속성을 이용해 값 설정 --> <bean id="movie2" class="di.movie.dto.MovieDTO"> <property name="movieId" value="0001"/> <property name="title" value="간기남"/> <property name="runningTime" value="60"/> <property name="genre" value="멜로"/> <property name="director" ref="director1"/> </bean> <!-- property 태그 - p : namespace 이용해 값 설정 --> <bean id="movie3" class="di.movie.dto.MovieDTO" p:movieId="0001" p:title="간기남" p:runningTime="60" p:genre="멜로" p:director-ref="director1"/>
</beans>
package di.movie.dto;
public class DirectorDTO { private String directorId; private String name; private int age; public DirectorDTO() { super(); } public DirectorDTO(String directorId, String name, int age) { super(); this.directorId = directorId; this.name = name; this.age = age; }
public String getDirectorId() { return directorId; } public void setDirectorId(String directorId) { this.directorId = directorId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "DirectorDTO [directorId=" + directorId + ", name=" + name + ", age=" + age + "]"; }
}
package di.movie.dto;
public class MovieDTO { private String movieId; private String title; private double runningTime; private String genre; private DirectorDTO director;
1. property를 통해 객체 또는 값을 주입 받는다. - 주의 : setter를 통해서는 하나의 값만 받을 수 있다.
2. <property> : <bean>의 하위태그로 설정한 bean 객체 또는 값을 property를 통해 주입하도록 설정 - 속성 : name - 값을 주입할 property 이름(setter의 이름) - 설정 방법 - <ref>,<value>와 같은 하위태그를 이용하여 설정 - 속성을 이용해 설정 - xml namespace를 이용하여 설정
3. 하위태그를 이용한 설정 - <ref bean="bean name"/> - 객체를 주입 시 - <value>값</value> - 문자(String) Primitive data 주입 시 - type 속성 : 값의 타입을 명시해야 하는 경우 사용 - 속성 이용 - ref="bean 이름" - value="값" - XML Namespace를 이용 - <beans>태그의 스키마 설정에 namespace등록 - xmlns:p="http://www.springframework.org/schema/p - bean 주입 : p:propertyname-ref="bean_id" ex)<bean p:dao-ref="dao">
public class TestMovie { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("di/movie/config/movie.xml"); MovieDTO mto = (MovieDTO)ctx.getBean("movie1"); System.out.println(mto); mto = (MovieDTO)ctx.getBean("movie2"); System.out.println(mto); mto = (MovieDTO)ctx.getBean("movie3"); System.out.println(mto); mto = (MovieDTO)ctx.getBean("movie4"); System.out.println(mto); DirectorDTO dto = (DirectorDTO)ctx.getBean("director1"); System.out.println(dto); } }