사용자 로그인 여부 알리기

 

💡 테이블 만든 후 vo만들기 → UserDAO.java → UserService(인터페이스) → 인터페이스를 상속 받는 서비스 계층 만들기 → 테스트 클래스 만들기

 

SQL, scott 계정에서 )

create TABLE users(
id varchar2(8) primary key,
password varchar2(8),
name varchar2(20),
role varchar2(5)
);

 

 

 

스프링에서 

 

UserVO.java - 직접 생성, Bean으로 관리하지 않는다

package com.springbook.biz.user;

public class UserVO {
	private String id;
	private String password;
	private String name;
	private String role;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	
	@Override
	public String toString() {
		return "UserVO [id=" + id + ", password=" + password + ", name=" + name + ", role=" + role + "]";
	}
	
	
}

 

UserDAO.java

package com.springbook.biz.user.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.springframework.stereotype.Repository;

import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.JDBCUtil;
import com.springbook.biz.user.UserVO;

// 빈으로 등록하기 위해선 @Repository("userDAO")를 빼먹으면 안된다.
@Repository("userDAO")
public class UserDAO {
	private Connection conn = null;
	private PreparedStatement stmt = null;
	private ResultSet rs = null;
	
	private final String USER_GET = " select * from users where id=? and password=? ";
	
	// 사용자 정보 조회
	public UserVO getUser(UserVO vo) {
			System.out.println("===> JDBC로 getUser() 기능 처리");
			UserVO user = null;
			
			try {
				
				conn = JDBCUtil.getConnection();
				stmt = conn.prepareStatement(USER_GET);
				stmt.setString(1, vo.getId());
				stmt.setString(2, vo.getPassword());
				rs = stmt.executeQuery();
				
				if (rs.next()) {
					user = new UserVO();
					user.setId(rs.getString("ID"));
					user.setPassword(rs.getString("PASSWORD"));
					user.setName(rs.getString("NAME"));
					user.setRole(rs.getString("ROLE"));
				}
				
			} catch (Exception e){
				e.printStackTrace();
			} finally {
				JDBCUtil.close(stmt, conn);
			}

		
		return user;
	}
}

 

UserService.java (인터페이스)

package com.springbook.biz.user;

public interface UserService {
	public UserVO getUser(UserVO vo);
}

 

UserServiceImpl.java

package com.springbook.biz.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.user.UserService;
import com.springbook.biz.user.UserVO;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO UserDAO;
	
	@Override
	public UserVO getUser(UserVO vo) {
		
		return UserDAO.getUser(vo);
	}

}

 

UserServiceClient.java

package com.springbook.biz.user;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class UserServiceClient {

	public static void main(String[] args) {
		AbstractApplicationContext container = new GenericXmlApplicationContext("applicationContext.xml");

		UserService userService = (UserService) container.getBean("userService");

		UserVO vo = new UserVO();
		vo.setId("test");
		vo.setPassword("test1234");

		UserVO user = userService.getUser(vo);

		if (user != null) {
			System.out.println(user.getName() + " 님 환영합니다.");
		} else {
			System.out.println("로그인 실패");
		}
	}

}

 

 

결과 화면 )

 

 

오늘만든 파일들과 파일의 구조

 

 

게시판 만들었던 걸 비즈니스 계층 넣어서 연습해보기

+ Recent posts