JSP - MyBatis ๐Ÿฆƒ

ํ™”์ดํ‹ฐ ยท2023๋…„ 12์›” 22์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
12/12

MyBatis๐Ÿ•ŠLogin/Join

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

Untitled

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>
  • Create package database
  1. java class SqlSessionManager
  2. file db.properties
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
  1. Mapper xml file
<?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>
  1. xml file mybatis-config
<?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>
  • Create model package
  1. MemberDTO (use Lombok)
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;
	//ํšŒ์›๊ฐ€์ž…ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ
	
}
  1. MemberDAO
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;
}
}
  • Create controller package

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);
	
	}
	
	

}
profile
์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•ฉ์‹œ๋‹ค! The best is yet to come! ๐Ÿ’œ

0๊ฐœ์˜ ๋Œ“๊ธ€