1. MyBatis
가. 다운로드
http://blog.mybatis.org/p/products.html
나. mybatis 설정 방법
1) mybatis-3.4.4.jar : lib 폴더에 복사
2) MybatisManager.java : mybatis framework을 실행할 수 있는 세션 생성
3) sqlMapConfig.xml : mybatis 기본설정 파일
4) mapper 파일 : 실제 sql query 문장
* 특징
- 프로그래밍 코드로부터 SQL 코드를 분리
- 입력 매개변수를 라이브러리 클래스로 전달하고 출력
- 비즈니스 로직 클래스로부터 데이터액세스클래스를 분리
- 트랜잭션 관리 가능
* 사용 방법
mybatis-3.4.1.jar 파일을 WEB-INF/lib 디렉토리에 복사
MybatisService.java와 sqlMapConfig.xml 파일을 Java Resource/src/config 패키지에 복사
sample.xml 파일을 활용하여 SQL 명령어 작성
2. ojdbc7 다운로드
다운로드 사이트 :
ojdbc6 : https://mvnrepository.com/artifact/com.oracle/ojdbc6/12.1.0.1-atlassian-hosted
ojdbc7 : https://mvnrepository.com/artifact/com.github.noraui/ojdbc7/12.1.0.2
ojdbc8 : https://mvnrepository.com/artifact/com.github.noraui/ojdbc8
4. server.xml
<Context docBase="macaronics-shoes-Shopping" path="/macaronics-shoes-Shopping" reloadable="true" source="org.eclipse.jst.jee.server:macaronics-shoes-Shopping" > <Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" loginTimeout="10" maxWait="5000" name="jdbc/pool" password="1111" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:XE" username="system" /> </Context>
5. sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- xml 지시어 --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 알리아스 설정 --> <!-- typeAlias type="전체경로" alias="별칭" --> <!-- <typeAliases> <typeAlias type="emp.dto.EmpDTO" alias="e" /> </typeAliases> --> <!-- db연결 참조코드 --> <environments default=""> <environment id=""> <transactionManager type="JDBC" /> <dataSource type="JNDI"> <property name="data_source" value="java:comp/env/jdbc/pool" /> </dataSource> </environment> </environments> <!-- 실제 sql query --> <mappers> <!-- 샘플 설정 --> <!-- <mapper resource="emp/mapper/emp.xml" /> --> <mapper resource="mapper/board.xml"/> </mappers> </configuration>
6. class MybatisService
package config; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisService { // SqlSessionFactoryBuilder => SqlSessionFactory // => SqlSession //SqlSession 객체 생성기 private static SqlSessionFactory factory; static { try { // Java Resources의 src Reader r = Resources.getResourceAsReader("config/sqlMapConfig.xml"); //SqlSessionFactory 생성기 factory = new SqlSessionFactoryBuilder().build(r); r.close(); } catch (Exception e) { e.printStackTrace(); } } public static SqlSessionFactory getFactory() { return factory; } }
7. 연결 확인 테스트
board.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="member"> <!-- id="태그의 식별자" resultType="sql 명령어의 리턴타입(레코드의 자료형)" 샵{변수} => 입력매개변수 --> <select id="memberlist" resultType="model.BoardBean"> <![CDATA[ select * from (select A.* , Rownum Rnum from (select * from board order by ref desc, re_step asc) A ) where Rnum >= 1 and Rnum <= 5 ]]> </select> </mapper>
class BoardBean
public class BoardBean { private int num; private String writer; private String email; private String subject; private String password; private String reg_date; private int ref; private int re_step; private int re_level; private int readcount; private String content;
class BoardListTest
package model; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import config.MybatisService; public class BoardListTest { SqlSession session=MybatisService.getFactory().openSession(); public List<BoardBean> memberList(){ List<BoardBean> items=new ArrayList<BoardBean>(); Map<String,Object> map= new HashMap<String,Object>(); items = session.selectList("member.memberlist",map); return items; } }
index.jsp
<%@page import="model.BoardListTest"%> <%@page import="config.MybatisService"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%= MybatisService.getFactory() %> <br> Mybais 연결 테스트 <% BoardListTest test =new BoardListTest(); out.print(test.memberList().toString()); %> </body> </html>
macaronics.net
소스 : https://github.com/braverokmc79/jsp_sin
댓글 ( 4)
댓글 남기기