department.xml보기 접기
<?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> <typeAlias alias="ddto" type="dept.dto.DepartmentDTO"/> <parameterMap class="ddto" id="insertPM"> <parameter property="departmentId"/> <parameter property="departmentName"/> <parameter property="location"/> </parameterMap> <sql id="select"> select department_id as departmentId, department_name as departmentName, location from department </sql> <insert id="insertDepartment" parameterMap="insertPM"> insert into department (department_id, department_name, location) values(?,?,?) </insert> <update id="updateDepartment" parameterClass="ddto"> update department set department_name=#departmentName#, location=#location# where department_id=#departmentId# </update> <delete id="deleteDepartment" parameterClass="string"> delete from department where department_id=#departmentId# </delete> <select id="selectDepartmentById" parameterClass="string" resultClass="ddto"> <include refid="select"/> where department_id=#departmentId# </select> <select id="selectAllDepartment" resultClass="ddto"> <include refid="select"/> order by department_id asc </select> </sqlMap>
접기
spring.xml보기 접기
<?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>
접기
SqlMapConfig.xml보기 접기
<?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>
접기
DepartmentDAO.java보기 접기
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){ 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; } public DepartmentDTO selectDepartmentById(String departmentId){ DepartmentDTO ddt = (DepartmentDTO) sqlMap.queryForObject("selectDepartmentById", departmentId); return ddt; } public List selectAllDepartment(){ return sqlMap.queryForList("selectAllDepartment"); } }
접기
DepartmentDTO.java보기 접기
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; } }
접기
TestDepartment.java보기 접기
package dept.main;
import java.util.List;
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("D0007","개발부","울산지점"); System.out.println("-------부서 삽입하기-------"); dao.insertDepartment(ddto); System.out.println("-------부서 수정하기-----------"); ddto = new DepartmentDTO("D0004", "수정부", "수정지점"); int cnt = dao.updateDepartment(ddto); System.out.println(cnt+"개의 행이 수정되었습니다."+ddto); System.out.println("---------부서 삭제하기---------"); String departmentId = "D0005"; int cnt1 = dao.deleteDepartmentById(departmentId); System.out.println(cnt1+"개의 행이 삭제되었습니다."+departmentId); System.out.println("--------------ID로 조회하기(select)-----------"); String selectDepartmentId = "D0007"; ddto = dao.selectDepartmentById(selectDepartmentId); System.out.println(ddto); System.out.println("--------전체 조회하기------------"); List list = dao.selectAllDepartment(); for(Object obj: list){ System.out.println(obj); } }
}
접기
* 결과보기
-------부서 삽입하기-------
1개의 부서가 삽입되었습니다.DepartmentDTO [departmentId=D0007, departmentName=개발부, location=울산지점]
-------부서 수정하기-----------
1개의 행이 수정되었습니다.DepartmentDTO [departmentId=D0004, departmentName=수정부, location=수정지점]
---------부서 삭제하기---------
1개의 행이 삭제되었습니다.D0005
--------------ID로 조회하기(select)-----------
DepartmentDTO [departmentId=D0007, departmentName=개발부, location=울산지점]
--------전체 조회하기------------
DepartmentDTO [departmentId=D0001, departmentName=개발부, location=서울지점]
DepartmentDTO [departmentId=D0002, departmentName=영업1부, location=서울지점]
DepartmentDTO [departmentId=D0003, departmentName=영업2부, location=부산지점]
DepartmentDTO [departmentId=D0004, departmentName=수정부, location=수정지점]
DepartmentDTO [departmentId=D0006, departmentName=개발부, location=울산지점]
DepartmentDTO [departmentId=D0007, departmentName=개발부, location=울산지점]