본문 바로가기
개발일기

학원 수업 29일차 220929

by hhana 2022. 10. 27.

9-10


DBCP(DataBase Conncetion Pool), 커넥션풀 이란?
https://zzang9ha.tistory.com/376

프로젝트 만들고 서버 연결, 드라이버 lib폴더에 넣고 시작해
Properties > Java Build Path > Libraries > Classpath에 만들어줘도 되긴하지만 귀찮잖아

src > main > webapp > META-INF 우클릭 > new > other > XML File > context.xml

<Context> 태그 만들고
https://tomcat.apache.org/tomcat-10.0-doc/jndi-datasource-examples-howto.html#Oracle_8i,_9i_&_10g
1. Context configuration 에 있는 태그 복붙

2. web.xml configuration
You should ensure that you respect the element ordering defined by the DTD when you create you applications web.xml file.
프로젝트 우클릭 > Java EE Tools > Generate Deployment Descriptor Stub > web.xml 생성!
상자 안에 있는 내용 <web-app>태그 안에 복붙
web.xml 파일 열었을 때 엑박뜨는 오류 있을수도 있음

3. Code example
html 부분 시작하기 전에 <% %>열고 그 안에 상자 내용 복붙
임포트 단축키가 먹히지 않아요! -> Context단어 맨 뒤에서 ctrl+스페이스바 누르면 클래스 임포트 가능

xml이 뭐지?
https://aws.amazon.com/ko/what-is/xml/
XML(Extensible Markup Language) 데이터를 정의하는 규칙을 제공하는 마크업 언어
다른 프로그래밍 언어와 달리 XML은 자체적으로 컴퓨팅 작업을 수행할 수 없지만 대신 구조적 데이터 관리를 위해 모든 프로그래밍 언어 또는 소프트웨어를 구현할 수 있음

 

 

  • 10-11

DB에 접근 연결
(DBTest.jsp)(OracleConfig.java)

<% 
/* Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/mydb");
Connection conn = ds.getConnection(); */
//두번에 걸쳐 검색

/* Context initContext = new InitialContext();
//Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
Connection conn = ds.getConnection();
//한번에 검색 가능(복붙해서 쓸거면 굳이 수정 안해도 됨) */

Connection conn = OracleConfig.getConnection(); //반복적인 접속, close를 간편하게 하기위해 class를 분리시켜놓음(OracleConfig.java)
%>

 

 

  • 11-12

MyBatis 기본적으로 무조건 사용할 줄 알아야돼 웹프로젝트의 기본
JPA 
동일 기능을 이 두가지로 다 구현해봐야돼
https://goodteacher.tistory.com/344

(web05Mybatis)
https://blog.mybatis.org/
Products > MyBatis 3 | SQL Mapping Framework for Java --docs : https://mybatis.org/mybatis-3/
> Getting Started (To use MyBatis you just need to include the mybatis-x.x.x.jar file in the classpath.)
> mybatis-x.x.x.jar 누르면 깃 나옴 > tools에 압축풀기 > jar파일 있는지 확인 
> jar 파일은 어디다 둬? > (src>main>webapp>WEB-INF>lib)에 복붙

https://mybatis.org/mybatis-3/getting-started.html
Getting started > Installation 완료 후 > Building SqlSessionFactory from XML
(Every MyBatis application centers around an instance of SqlSessionFactory. A SqlSessionFactory instance can be acquired by using the SqlSessionFactoryBuilder. )
상자내용 복사 후 webapp에 jsp파일 생성(MybatisTest.jsp) > 바디에 <% %> 열어주고 붙여넣기
> 임포트는 org.apache.ibatis로 > mybatis-config.xml 생성 (Java Resources>src/main/java에 nowon.mybatis패키지 만들고 파일(mybatis-config.xml) 만들어주면 됨(배포하면 WEB-INF안에서 xml 파일 찾을 수 있음)
> MybatisTest.jsp파일 바디에 String resource = "nowon/mybatis/mybatis-config.xml"; 위치 잡아줘야 함
> ?xml version 으로 시작하는 박스 내용 복사하여 xml파일에 붙여넣기(기존 버전 내용 삭제)

 

 

  • 12-1

<dataSource type="POOLED">?
https://mybatis.org/mybatis-3/configuration.html#environments
dataSource (UNPOOLED /POOLED /JNDI )
JNDI?(톰캣에서 하던것처럼)
POOLED? --Properties사용 https://mybatis.org/mybatis-3/configuration.html#properties 

<configuration>태그 맨첫줄에 <properties>작성 <properties />이렇게 쓰면 싱글태그 됨
src/main/java > nowon.mybatis 우클릭 > new > 파일 (dataSource.properties) 생성
<properties resource="nowon/mybatis/dataSource.properties"/>로 수정(mybatis-config.xml)

(dataSource.properties)
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:xe
username=nowon
password=1234
작성(맨 뒤에 공백 안생기게 주의해)
주석은#사용(한글불가.유니코드가 되어버림)

src/main/java > nowon.mybatis에 mapper패키지 만들고 > xml파일 생성(memo-mapper.xml)
https://mybatis.org/mybatis-3/getting-started.html > Exploring Mapped SQL Statements 상자 내용 복붙 <select>태그는 삭제, <mapper namespace="MemoMapper"> 수정
> mybatis-config.xml에서 <mapper resource="nowon/mybatis/mapper/memo-mapper.xml"/> 수정
> SqlSession sqlSession=sqlSessionFactory.openSession();
   System.out.println(sqlSession); //접속정보
   sqlSession.close();

https://mybatis.org/mybatis-3/getting-started.html#sqlsessionfactory
SqlSessionFactory
(Once created, the SqlSessionFactory should exist for the duration of your application execution. There should be little or no reason to ever dispose of it or recreate it. It's a best practice to not rebuild the SqlSessionFactory multiple times in an application run.)

클래스 멤버에 접근하려면 인스턴스화해야함
객체 없이 외부에서도 자원(멤버, 필드)에 접근하고 싶으면 static

 

 

  • 3-4

(index.jsp)(MemoController.java)(list.jsp)


'개발일기' 카테고리의 다른 글

학원 수업 33일차 221006  (0) 2022.10.31
학원 수업 32일차 221005  (0) 2022.10.27
학원 수업 31일차 221004  (0) 2022.10.27
학원 수업 30일차 220930  (0) 2022.10.27
학원 수업 28일차 220928  (0) 2022.10.26
학원 수업 27일차 220927  (0) 2022.10.26
학원 수업 26일차 220926  (0) 2022.10.26
학원 수업 25일차 220923  (0) 2022.10.26

댓글