context의 상세 내용은 [Servers]에서 해당 프로젝트의 config 폴더에서 수정할 수 있어요.
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- 아래 내용 추가 -->
<!--
username, password에는 본인의 db 계정을 입력해주면 돼요.
-->
<Resource
name="jdbc/oracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:xe"
username="jsp"
password="jsp"
maxActive="20"
maxIdle="20"
maxWait="-1"
/>
<!-- 아래 내용 추가 끝-->
</Context>
<%@page import="java.sql.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
// dbcp를 이용하는 방법
Context context = new InitialContext(null);
DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
Connection conn = dataSource.getConnection();
String sql = "select sysdate from dual";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
Date date = null;
if(rs.next()){
date = rs.getDate(1);
}
%>
<%=date %>
<%-- <%=conn %> --%>
</body>
</html>
아래 링크를 통해 마이바티스를 다운받습니다.
blog.mybatis.org/p/products.html
(1)
(2)
(3)
(1)
[해당프로젝트] - [src] - [main] - [webapp] - [WEB-INF] - [lib] 폴더에 다운로드 받은 [mybatis-3.5.11.jar] 파일을 추가해줘요.
(2)
해당 프로젝트에 오른쪽 버튼을 클릭하여 [Propertis]에 들어갑니다.
(3)
사진과 같은 경로로 접근하여 추가한 라이브러리를 Classpath에 추가해줘요.
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlMapConfig {
private static SqlSessionFactory factory;
// 클래스 초기화 블럭(클래스가 처음 로딩될 때 한번만 수행)
static {
try {
String resource = "./com/koreait/web/mybatis/config.xml";
Reader reader = Resources.getResourceAsReader(resource);
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getFactory() {
return factory;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<!--
configuration의 상세 내용은 본인의 db 환경대로 작성해줍니다.
-->
<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:@localhost:1521:xe"/>
<property name="username" value="jsp"/>
<property name="password" value="jsp"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/koreait/web/sql/user.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="User">
<select id="login" parameterType="hashmap" resultType="com.koreait.web.beans.UserBean">
SELECT * FROM TBL_USER WHERE USERID = #{userid} AND USERPW = #{userpw}
</select>
</mapper>
출처
https://media.giphy.com/media/dwmNhd5H7YAz6/giphy.gif
https://media.giphy.com/media/3o6Mb9EC7mNqXl9x7y/giphy.gif