JSP

가. 다운로드

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 문장

 

 

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- xml 지시어, xml directive -->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 알리아스 설정 -->
	<typeAliases>
<!-- typeAlias type="전체경로" alias="별칭" -->	
	 <!--  <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/myoracle" />
			</dataSource>
		</environment>
	</environments> -->
	
<!-- 	
	    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/testDB" />
                <property name="username" value="test" />
                <property name="password" value="test12" />
            </dataSource>
        </environment>
    </environments>

 -->

	
	<environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe" />
                <property name="username" value="java" />
                <property name="password" value="1111" />
            </dataSource>
        </environment>
    </environments>



	
	
	
	<!-- 실제 sql query -->
<!-- 	<mappers>
		<mapper resource="emp/mapper/emp.xml" />
		<mapper resource=
			"student/mapper/student.xml" />		
		<mapper resource=
			"student/mapper/dept.xml" />	
		<mapper resource=
			"student/mapper/prof.xml" />	
		<mapper resource=
			"student/mapper/lecture.xml" />		
		<mapper resource=
			"memo/mapper/memo.xml" />	
		<mapper resource=
			"board/mapper/board.xml" />												
	</mappers> -->
	
</configuration>

 

 

class MybatisManager

package sqlmap;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;



public class MybatisManager {
//싱글톤 패턴으로 SqlSessionFactory 생성
//SqlSessionFactoryBuilder => SqlSessionFactory
//=>SqlSession
//(sql 실행 객체 : JDBC의 Statement + Result 객체 )
	
	private static SqlSessionFactory instance;
	private MybatisManager() {} //private 생성자
	
	public static SqlSessionFactory getInstance(){
		Reader reader=null;
		try {
			// Java Resources의 src
			//mybatis 설정파일 정보를 읽음
			reader = Resources.getResourceAsReader(
					"sqlmap/sqlMapConfig.xml");
			//SqlSessionFactory 생성기
			instance = new SqlSessionFactoryBuilder().build(reader);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(reader!=null)reader.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return instance;
	}
	
	
	
}

 

 

 

 

 

 

 

 

 

jsp

 

about author

PHRASE

Level 60  머나먼나라

No rose, without a thorn. (세상에 완전한 행복은 없다.)

댓글 ( 4)

댓글 남기기

작성