* Spring ibatis 연동 구조
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
</sqlMap>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- BasicDataSource 생성 -->
<bean name="datasource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- SqlMapClient 생성 -->
<bean name="sqlMapclient" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClientFactoryBean"/>
</bean>
<!-- SqlMapClientTemplate 생성 -->
<bean name="sqlMapClientFactoryBean" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="dept/config/SqlMapConfig.xml"/>
<property name="dataSource" ref="datasource"/>
</bean>
<!-- DepartmentDAO 생성 -->
<bean name="departmentDAO" class="dept.dao.DepartmentDAO">
<constructor-arg ref="sqlMapclient"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- SqlMap파일 등록 -->
<sqlMap resource="dept/config/department.xml"/>
</sqlMapConfig>
package dept.dao;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import dept.dto.DepartmentDTO;
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;
}
public List selectAllDepartment(){
return null;
}
}
package dept.dto;
/*
* Table : Department(department_id(pk), department_name, location)
*/
public class DepartmentDTO {
private String departmentId;
private String departmentName;
private String location;
public DepartmentDTO() {
super();
}
public DepartmentDTO(String departmentId, String departmentName,
String location) {
super();
this.departmentId = departmentId;
this.departmentName = departmentName;
this.location = location;
}
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "DepartmentDTO [departmentId=" + departmentId
+ ", departmentName=" + departmentName + ", location="
+ location + "]";
}
@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;
}
}
package dept.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dept.dao.DepartmentDAO;
import dept.dto.DepartmentDTO;
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();
dao.insertDepartment(ddto);
}
}
'프로그래밍 > Spring Framework' 카테고리의 다른 글
Spring ibatis연동하기 실습 - spring-ibatis - insert, update, delete, select (0) | 2012.06.21 |
---|---|
Bean 객체의 생성 단위 / Factory 메소드를 통한 Bean 주입 (0) | 2012.06.21 |
bean scope 테스트(singleton : default, prototype) (0) | 2012.06.21 |
Collection 객체 주입하기 - spring_di_03 (0) | 2012.06.20 |
Collection 객체 주입하기 - Spring (0) | 2012.06.20 |