JSP
[JSP] 42. <JSP와 MVC 패턴을 사용한 to-do 프로젝트> (1)
Song hyun
2024. 7. 9. 10:56
728x90
반응형
[JSP] 42. <JSP와 MVC 패턴을 사용한 to-do 프로젝트> (1)
1. MVC 패턴을 활용한 코드 설계
(1) 회원가입 + 로그인
(2) todo작성, 조회, 수정, 삭제 (CRUD)
2. 필요 라이브러리 확인 : HikariCP의 의존성
3. 코드 작성
(1) BasicDBUtil.java
package com.tenco.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BasicDBUtil {
private static final String URL="jdbc:mysql://localhost:3306/m_todo?serverTimezone=Asia/Seoul";
private static final String USER="root";
private static final String PASSWORD="asd123";
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection(URL,USER,PASSWORD);
}
}
(2) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/MyDB"
auth="Container"
factory="com.zaxxer.hikari.HikariJNDIFactory"
uniqueResourceName="MyDB"
minimumIdle="5"
maximumPoolSize="10"
connectionTimeOut="30000"
idleTimeOut="60000"
maxLifeTime="1800000"
jdbcUrl="jdbc:mysql://localhost:3306/m_todo?serverTimezon=Asia/Seoul"
driverClassName="com.mysql.cj.jdbc.Driver"
username="root"
password="asd123"
/>
</Context>
*META_INF 폴더는 보안으로 감춰져 있기 때문에, 외부 URL 사용이 불가능하다!
**왜 context.xml을 사용하는걸까?**
-> 자원 공유의 목적
-> 환경 설정 분리
=> 즉, 대규모 애플리케이션에서는 많은 설정이 필요하다.
***context.xml의 역할과 JNDI의 개념***
-> context.xml이란?
-> JNDI(Java Naming and Directory Interface)
! 중요
(3) DBUtil.java
package com.tenco.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBUtil {
private static DataSource dataSource;
// 정적 초기화 블록, static block
static {
// TODO - 삭제 예정
System.out.println("111111111111111");
try {
// InitialContext 객체를 생성하여 JNDI API기술을 통해
// 존재하는 리소스를 찾는 방법!
InitialContext ctx = new InitialContext();
dataSource=(DataSource) ctx.lookup("java:comp/env/jdbc/MySQL");
} catch (NamingException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
*InitialContext 객체의 역할?
-InitialContext 객체는
(4) m_todo.sql
create database If NOT EXISTS m_todo;
use m_todo;
-- 정규화 1,2,3 정규 테이블 설계
-- users 테이블을 생성
-- SQL에도 코딩 컨벤션이 있지만, 사마다 다르다!
create table if not exists users(
id int auto_increment primary key,
username varchar(50) not null,
password varchar(255) not null,
email varchar(100) not null,
created_at timestamp default current_timestamp
);
desc users;
alter table users add constraint unique(username);
-- todos 테이블 생성
create table if not exists todos(
id int auto_increment primary key,
title varchar(100) not null,
description text,
created_at timestamp default current_timestamp,
due_date date,
completed boolean default false,
user_id int not null,
foreign key(user_id) references users(id)
);
-- 샘플 데이터 삽입
-- users 테이블에 데이터 삽입
INSERT INTO users (username, password, email) VALUES
('홍길동', 'asd123', 'hong@example.com'),
('김철수', 'asd123', 'kim@example.com'),
('이영희', 'asd123', 'lee@example.com');
-- todos 테이블에 데이터 삽입
INSERT INTO todos (user_id, title, description, due_date, completed) VALUES
(1, '할 일 1', '할 일 1에 대한 설명입니다.', '2023-12-31', FALSE),
(1, '할 일 2', '할 일 2에 대한 설명입니다.', '2024-01-15', TRUE),
(2, '할 일 3', '할 일 3에 대한 설명입니다.', '2024-02-28', FALSE),
(3, '할 일 4', '할 일 4에 대한 설명입니다.', '2024-03-10', TRUE);
select * from users;
select * from todos;
728x90
반응형