install lombok, mybatis in POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smhrd</groupId>
<artifactId>8.MyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--1. add my batis library -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Servlet api -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc6 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
Convert from dynamic project to maven project
Main page
<%@ 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>
<style type ="text/css">
fieldset{
display: inline;}
</style>
</head>
<body>
<%
String result = request.getParameter("result");
if (result.equals("success")){
out.print("join Success");
}else {
out.print("Join Fail");
}
%>
<fieldset align = "center">
<legend align = "center"> Main</legend>
<a href = "./Join.jsp"><button>ํ์๊ฐ์
</button></a>
<a href = "./Login.jsp"><button>๋ก๊ทธ์ธ</button></a>
</fieldset>
</body>
</html>
Join - login page
<%@ 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>
<fieldset>
<legend align = "center"> Join </legend>
<form action ="JoinProgram" method ="post">
<table>
<tr>
<td>ID :</td>
<td> <input type = "text" name = "id" ><br></td>
</tr>
<tr>
<td>PW :</td>
<td><input type = "text" name ="pw" ><br></td>
</tr>
<tr>
<td>NAME :</td>
<td><input type = "text" name = "name" ><br></td>
</tr>
<tr>
<td align = "center"><input type = "submit" value = "ํ์๊ฐ์
"></td></tr>
</table>
</form>
</fieldset>
</body>
</html>
<%@ 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>
<fieldset>
<legend>
Login</legend>
<form action ="LoginProgram" method ="post">
<table>
<tr>
<td>ID :</td>
<td> <input type = "text" name = "id" ><br></td>
</tr>
<tr>
<td>PW :</td>
<td><input type = "text" name ="pw" ><br></td>
</tr>
<tr>
<td><input type ="submit" value ="๋ก๊ทธ์ธ"></td></tr>
</table>
</form>
</fieldset>
</body>
</html>
package database;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionManager {
public static SqlSessionFactory sqlSessionFactory;
static {
try {
// mybatisํ๊ฒฝ์ค์ ํ์ผ ๊ฒฝ๋ก
String resource = "database/mybatis-config.xml";
// xmlํ์ผ์ ์ฝ์ด๋ด๊ธฐ ์ํด inputStream ์ฌ๋ ๊ฒ
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(resource);
// sqlsessionffactory: connection๊ฐ์ฒด๊ฐ ์ฌ๋ฌ๊ฐ์ธ connection pool
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSession() {
return sqlSessionFactory;
}
}
driver=oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@localhost:1521:xe
username = service
password = 12345
<?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="database.Mapper">
<insert id= "join" parameterType = "model.MemberDTO">
insert into MEMBER1 values(#{id},#{pw},#{name})
</insert>
<select id = "login" parameterType ="model.MemberDTO" resultType = "String">
select name from MEMBER1 where id =#{id} and pw = #{pw}
</select>
</mapper>
<?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>
<properties resource="database/db.properties"> </properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="database/Mapper.xml"/>
</mappers>
</configuration>
package model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
public class MemberDTO {
@NonNull
private String id;
@NonNull
private String pw;
private String name;
//ํ์๊ฐ์
ํ ๋ ์ฌ์ฉํ ์์ฑ์ ๋ฉ์๋
}
package model;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import database.SqlSessionManager;
public class MemberDAO {
// session์ ์์ฑํด์ค ์ ์๋ factory
SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession();
//Factory๋ฅผ ์ฌ์ฉํด์ ์ธ์
์์ฑ (DB์ฐ๊ฒฐ, close(),sql์คํ)
SqlSession sqlSession = sqlSessionFactory.openSession(true);// auto commit
//join
public int join(MemberDTO dto) {
int cnt = 0;
try {
cnt = sqlSession.insert("database.Mapper.join", dto);
} catch (Exception e) {
} finally {
System.out.println(cnt);
sqlSession.close();
}
return cnt;
}
//login
public String login(MemberDTO dto) {
String name = null;
try {
name=(String)sqlSession.selectOne("database.Mapper.login", dto);
}catch(Exception e) {
}finally {
sqlSession.close();
}
return name;
}
//ํ์์ ๋ณด ๋ฆฌ์คํธ ์ถ๋ ฅ
public List<MemberDTO> memberList() {
List<MemberDTO> list = null;
try {
list = sqlSession.selectList("database.Mapper.memberList");
}catch(Exception e){
}finally {
sqlSession.close();
}
return list;
}
}
JoinProgram
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.MemberDAO;
import model.MemberDTO;
@WebServlet("/JoinProgram")
public class JoinProgram extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post๋ฐฉ์ ์ธ์ฝ๋ฉ
request.setCharacterEncoding("UTF-8");
//์์ฒญ๋ฐ์ดํฐ 3๊ฐ ๊ฐ์ ธ์ค๊ธฐ
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String name = request.getParameter("name");
MemberDTO dto = new MemberDTO(id,pw,name);
int cnt = new MemberDAO().join(dto);
System.out.println(cnt);
String result = "";
if (cnt>0) {
result = "success";
response.sendRedirect("Main.jsp?result=" + result);
//response.sendRedirect("Login.jsp");
}else{
result = "fail";
response.sendRedirect("Main.jsp?result=" + result);
}
}
}
Login Program
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.MemberDAO;
import model.MemberDTO;
@WebServlet("/LoginProgram")
public class LoginProgram extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
MemberDTO dto= new MemberDTO(id,pw);
String name = new MemberDAO().login(dto);
System.out.println(name);
}
}