-
JSP-JDBC/mybatis 연습예제기타/Servlet 2020. 12. 11. 11:26
미리 만들어 놓은 MYMEMBER 라는 테이블 정보를 출력해보자!
1. src 폴더 안에 패키지를 생성해 주고 jar파일을 라이브러리에 넣어놓는다
(패키지 이름은 다른 프로젝트와 겹치지 않는 편이 좋다)
데이터 베이스와 연결해주는 SMC.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD config 3.0//EN" "http://mybatis.apache.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.jdbcex2.bean.Mymember" alias="mymember" /> </typeAliases> <environments default="development"> <environment id ="development"> <transactionManager type="JDBC" /> <dataSource type = "UNPOOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/> <property name="username" value="madang"/> <property name="password" value="madang"/> </dataSource> </environment> </environments> <mappers> <mapper resource = "com/jdbcex2/xml/Mymember.xml" /> </mappers> </configuration>
MBUtils.java
package com.jdbcex2.common; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; //mybatis를 부릴떄 사용할 수 있는 유틸리티 클래스 public class MBUtils { public static SqlSessionFactory getSqlSessionFatory() { String resource = "com/jdbcex2/common/SMC.xml" ; InputStream reader = null; try { reader = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException(e); } return new SqlSessionFactoryBuilder().build(reader); } public static SqlSession getSession() { return getSqlSessionFatory().openSession(true); } }
기본으로 사용할 인터페이스 IDAO
package com.jdbcex2.dao; import java.sql.SQLException; import java.util.List; public interface IDAO<T, K> { public List<T> selectAll() throws SQLException; public int insert(T baen) throws SQLException; public int update(T bean) throws SQLException; public int delete(K key) throws SQLException; public T selectById (K key) throws SQLException; public int maxIdNum() throws SQLException; }
IDAO에 자손인 MymemberDAO
package com.jdbcex2.dao; import com.jdbcex2.bean.Mymember; public interface MymemberDAO extends IDAO<Mymember, Integer> { }
Mymember 테이블을 가져오는 클래스
package com.jdbcex2.bean; import java.sql.Timestamp; public class Mymember { private int num; private String id; private Timestamp jday; private String pw; private String tel; private String secession; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Timestamp getJday() { return jday; } public void setJday(Timestamp jday) { this.jday = jday; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getSecession() { return secession; } public void setSecession(String secession) { this.secession = secession; } @Override public String toString() { return "Mymember [num=" + num + ", id=" + id + ", jday=" + jday + ", pw=" + pw + ", tel=" + tel + ", secession=" + secession + "]"; } }
Mymember.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="com.jdbcex2.dao.MymemberDAO"> <resultMap id = "result" type="mymember"> <result property = "num" column="num" /> <result property = "id" column="id" /> <result property = "jday" column="jday" /> <result property = "pw" column="pw" /> <result property = "tel" column="tel" /> <result property = "secession" column="secession" /> </resultMap> <select id="selectAll" resultMap="result" resultType="Integer"> <![CDATA[ SELECT * FROM MYMEMBER ]]> </select> <insert id ="insert" parameterType="mymember"> <![CDATA[ INSERT INTO MYMEMBER (NUM, ID, PW, TEL, JDAY, SECESSION) VALUES (MYMEMBER_SEQ.NEXTVAL, #{id}, #{pw}, #{tel}, #{jday}, #{secession} ) ]]> </insert> <update id ="update" parameterType="mymember"> <![CDATA[ UPDATE MYMEMBER SET PW=#{pw}, TEL=#{tel}, JDAY=#{jday}, SECESSION=#{secession} WHERE ID = #{id} ]]> </update> <delete id="delete" parameterType="mymember"> <![CDATA[ DELETE FROM MYMEMBER WHERE WHERE ID = #{id} ]]> </delete> <select id="selectById" resultType="mymember" parameterType="int" > <![CDATA[ SELECT * FROM MYMEMBER WHERE ID = #{id} ]]> </select> <select id="maxIdNum" resultType="Integer"> <![CDATA[ SELECT MAX(NUM) FROM MYMEMBER ]]> </select> </mapper>
실제 데이터를 출력 및 테스트
package com.jdbcex2.mybatistest; import java.sql.SQLException; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.jdbcex2.bean.Mymember; import com.jdbcex2.dao.MymemberDAO; import com.jdbcex2.common.MBUtils; //마이바티스 구동 테스트 public class MainTest { public static void main(String[] args) throws SQLException { SqlSession session = MBUtils.getSession(); MymemberDAO mymemberDao = session.getMapper(MymemberDAO.class); //가장 큰 넘버값 int maxNum = mymemberDao.maxIdNum(); System.out.println(maxNum); //모든 리스트 불러오기 List<Mymember> allData = mymemberDao.selectAll(); for(Mymember x:allData) { System.out.println(x); } } }
'기타 > Servlet' 카테고리의 다른 글
MyBatis에 대한 설명 (0) 2020.12.15 JSP - 게시판 만들기 연습 (0) 2020.12.15 JSP - 자바빈(Bean) (0) 2020.12.10 JSP - 예외처리 (Exception) (0) 2020.12.10 JSP- 세션(Sesssion) (0) 2020.12.09