public static void main(String[] args)throws Exception { DepartmentDTO deptDTO = new DepartmentDTO(); deptDTO.setDepartmentId("D0001"); EmployeeDTO empDTO = new EmployeeDTO("00013", "김건모", "abc@abc.com", "과장", 403000, deptDTO); EmployeeDAO dao = EmployeeDAO.getInstance();
// dao.insertEmployee(empDTO); empDTO.setSalary(5405050); empDTO.setJobPosition("부장"); int cnt = dao.updateEmployee(empDTO); System.out.println("변경된 row 수 : "+cnt); cnt = dao.deleteEmployeeById("00013"); System.out.println("삭제된 row 수 : "+cnt); System.out.println("-----전체 직원 부서 조회하기------"); List list = dao.selectAllEmployee(); for(Object obj:list){ EmployeeDTO dto = (EmployeeDTO)obj; System.out.println(dto.getEmployeeName()+" - "+dto.getDepartmentDTO().getDepartmentName()); } System.out.println("-----부서 조회하기-------"); list = dao.selectAllDepartment(); for(Object obj:list){ System.out.println(obj); } System.out.println("------봉급으로 조회-------"); HashMap map = new HashMap(); //map.put("start_salary", 2000000); map.put("end_salary", 10000000);
List list1 = dao.selectEmployeeBySalaryRange(map); for(Object obj:list1){ System.out.println(obj); } System.out.println("-----이름과 부서이름으로 조회--------"); EmployeeDTO edto1 = new EmployeeDTO(); // edto1.setEmployeeName("장"); edto1.setJobPosition("부"); List list2 = dao.selectEmployeeByNameOrPosition(edto1); for(Object obj:list2){ System.out.println(obj); }
System.out.println("-----iterate사용해서 여러명의 이름 중 있는 것만 조회하기------"); HashMap names = new HashMap(); String[] ns = {"한석규","길무성","김건모"}; names.put("names", ns); list = dao.selectEmployeeByNames(names); System.out.println(list.size()+"명 조회"); for(Object obj:list){ System.out.println(obj); } } }
<?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 namespace="dept"> <select id="selectAllDepartment" resultClass="hr.dto.DepartmentDTO"> select department_id as departmentId, department_name as departmentName, location from department </select> </sqlMap>
<?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 namespace="hr"> <typeAlias alias="empDTO" type="hr.dto.EmployeeDTO"/> <typeAlias alias="deptDTO" type="hr.dto.DepartmentDTO"/> <!-- insert --> <insert id="insertEmployee" parameterClass="empDTO"> <!-- <selectKey resultClass="int" keyProperty="employeeId"> select aaaa from dual </selectKey> --> insert into employee (employee_id,employee_name, email, job_position, salary, department_id) values(#employeeId#,#employeeName#,#email#,#jobPosition#,#salary#,#departmentDTO.departmentId#) </insert> <update id="updateEmployee" parameterClass="empDTO"> update employee set employee_name=#employeeName#, email=#email#, job_position=#jobPosition#, salary=#salary#, department_id=#departmentDTO.departmentId# where employee_id=#employeeId# </update> <delete id="deleteEmployeeById"> delete from employee where employee_id=#id# </delete> <resultMap class="empDTO" id="employeeResultMap"> <result property="employeeId" columnIndex="1"/><!--<result property="employeeId" column="employee_id"/>이렇게 줘도 되고 --> <result property="employeeName" columnIndex="2"/> <result property="email" columnIndex="3"/> <result property="jobPosition" columnIndex="4"/> <result property="salary" columnIndex="5"/> <result property="departmentDTO.departmentId" columnIndex="6"/> <result property="departmentDTO.departmentName" columnIndex="7"/> <result property="departmentDTO.location" columnIndex="8"/> </resultMap> <select id="selectAllEmployee" resultMap="employeeResultMap"> select emp.employee_id , emp.employee_name, emp.email, emp.job_position, emp.salary, dept.department_id, dept.department_name, dept.location from employee emp, department dept where emp.department_id = dept.department_id </select> <select id="selectEmployeeBySalaryRange" resultMap="employeeResultMap" parameterClass="hashmap"> select emp.employee_id , emp.employee_name, emp.email, emp.job_position, emp.salary, dept.department_id, dept.department_name, dept.location from employee emp, department dept where emp.department_id = dept.department_id <!-- and emp.salary > #start_salary# and emp.salary < #end_salary# -->
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <settings useStatementNamespaces="true"/><!-- namespace를 사용하겠다. true --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <!-- <sqlMap resource="config/member.xml"/> --> <sqlMap resource="hr/config/hr.xml"/> <sqlMap resource="hr/config/dept.xml"/> </sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
public static void main(String[] args)throws Exception { DepartmentDTO deptDTO = new DepartmentDTO(); deptDTO.setDepartmentId("D0001"); EmployeeDTO empDTO = new EmployeeDTO("00013", "김건모", "abc@abc.com", "과장", 403000, deptDTO); EmployeeDAO dao = EmployeeDAO.getInstance();
// dao.insertEmployee(empDTO); empDTO.setSalary(5405050); empDTO.setJobPosition("부장"); int cnt = dao.updateEmployee(empDTO); System.out.println("변경된 row 수 : "+cnt); cnt = dao.deleteEmployeeById("00013"); System.out.println("삭제된 row 수 : "+cnt); System.out.println("-----전체 직원 부서 조회하기------"); List list = dao.selectAllEmployee(); for(Object obj:list){ EmployeeDTO dto = (EmployeeDTO)obj; System.out.println(dto.getEmployeeName()+" - "+dto.getDepartmentDTO().getDepartmentName()); } System.out.println("-----부서 조회하기-------"); list = dao.selectAllDepartment(); for(Object obj:list){ System.out.println(obj); } } }
<?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 namespace="dept"> <select id="selectAllDepartment" resultClass="hr.dto.DepartmentDTO"> select department_id as departmentId, department_name as departmentName, location from department </select> </sqlMap>
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <settings useStatementNamespaces="true"/><!-- namespace를 사용하겠다. true --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <!-- <sqlMap resource="config/member.xml"/> --> <sqlMap resource="hr/config/hr.xml"/> <sqlMap resource="hr/config/dept.xml"/> </sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <!-- <sqlMap resource="config/member.xml"/> --> <sqlMap resource="config/member2.xml"/> </sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <!-- <sqlMap resource="config/member.xml"/> --> <sqlMap resource="config/member2.xml"/> </sqlMapConfig>
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="config/member.xml"/>
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + mileage; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((registerNumber1 == null) ? 0 : registerNumber1.hashCode()); result = prime * result + ((registerNumber2 == null) ? 0 : registerNumber2.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; MemberDTO other = (MemberDTO) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (mileage != other.mileage) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (registerNumber1 == null) { if (other.registerNumber1 != null) return false; } else if (!registerNumber1.equals(other.registerNumber1)) return false; if (registerNumber2 == null) { if (other.registerNumber2 != null) return false; } else if (!registerNumber2.equals(other.registerNumber2)) return false; return true; }
<!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/> <property name="JDBC.Username" value="scott"/> <property name="JDBC.Password" value="tiger"/> </dataSource> </transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="config/member.xml"/>