패키지 구성이다
addMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 여기 복붙!! -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 여기 복붙!! -->
<mapper namespace="addMapper">
<select id="allAddress" resultType="db.AddressDto">
select * from addressbook
</select>
<select id="selectAddress" parameterType="Integer" resultType="java.util.HashMap">
select *
from addressbook
where num=#{num}
</select>
<insert id="insertAddress" parameterType="db.AddressDto">
insert into
addressbook(NUM, NAME, ADDRESS, BIRTHDAY)
values
(#{num},#{name},#{address},#{birthday})
</insert>
<delete id="deleteAddress" parameterType="Integer">
DELETE FROM AddressBook
WHERE NUM = #{num}
</delete>
<update id="updateAddress" parameterType="java.util.HashMap" >
update addressbook
set birthday = #{birthday}, name = #{name}, address =#{address}
where num = #{num}
</update>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="아이디"/>
<property name="password" value="비밀번호"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="/db/addMapper.xml"/>
</mappers>
</configuration>
dao
package db; import java.io.*; import java.util.*; import org.apache.ibatis.io.*; import org.apache.ibatis.session.*; public class BatisDao { // sql 세션을 생성해 줄 수 있는 세션 팩토리변수 private SqlSessionFactory sqlSessionFactory; // 세션 팩토리 객체를 생성하고 그 결과를 리턴해주는 메서드 private boolean connect() { boolean result = false; try { // 환경 설정 파일의 경로를 문자열로 저장 String resource = "/db/mybatis-config.xml"; // 문자열로 된 경로의파일 내용을 읽을 수 있는 Reader 객체 생성 Reader reader = Resources.getResourceAsReader(resource); // reader 객체의 내용을 가지고 SqlSessionFactory 객체 생성 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); result = true; } catch (Exception e) { System.out.println("세션 팩토리 생성 실패:" + e.getMessage()); } return result; } // 전체 리스트 출력 public ArrayListviewMember() { // 데이터를 저장할 변수 생성 ArrayList list = null; try { connect(); // 세션팩토리를 이용해서 세션 객체를 생성 SqlSession session = sqlSessionFactory.openSession(); list = (ArrayList) session.selectList("addMapper.allAddress"); session.close(); } catch (Exception e) { System.out.println("데이터 가져오기 실패 : " + e.getMessage()); } return list; } // 하나의 목록 찾기(HashMap 사용) public HashMap selectAddress(int num) { HashMap temp = null; if (connect()) { SqlSession session = sqlSessionFactory.openSession(); try { temp = (HashMap) session.selectOne("addMapper.selectAddress", new Integer(num)); } finally { session.close(); } } return temp; } public boolean insertAddress(AddressDto dto) { boolean result = false; if (connect()) { SqlSession session = sqlSessionFactory.openSession(); try { int r = session.insert("addMapper.insertAddress", dto); if (r > 0) { result = true; session.commit(); } } catch (Exception e) { System.out.println("삽입 실패: " + e.getMessage()); } finally { session.close(); } } return result; } //삭제 public boolean deleteAddress(int num) { boolean result = false; if (connect()) { SqlSession session = sqlSessionFactory.openSession(); try { int imsi = session.delete("addMapper.deleteAddress", new Integer(num)); if (imsi > 0) result = true; session.commit(); } finally { session.close(); } } return result; } //수정 public boolean UpdateAddress(HashMap map) { boolean result = false; if (connect()) { SqlSession session = sqlSessionFactory.openSession(); try { int imsi = session.update("addMapper.updateAddress", map); if (imsi > 0) result = true; session.commit(); } finally { session.close(); } } return result; } }
dto
package db; import java.security.*; import java.sql.*; public class AddressDto { private int num; private String name; private String address; private Date birthday; public AddressDto() { super(); // TODO 자동 생성된 생성자 스텁 } public AddressDto(int num, String name, String address, Date birthday) { super(); this.num = num; this.name = name; this.address = address; this.birthday = birthday; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "AddressDto [num=" + num + ", name=" + name + ", address=" + address + ", birthday=" + birthday + "]"; } }
'Web개발 > JSP, Web' 카테고리의 다른 글
ext js 환경 (0) | 2016.06.03 |
---|---|
CSS 란 (0) | 2014.11.18 |
[JSP] json 파싱, xml 파싱 예제 (0) | 2014.11.18 |
[JSP] 파일 업로드 예제(File Upload) (0) | 2014.11.18 |
[JSP] Sevlet 사용하는 예제 (0) | 2014.11.18 |