본문 바로가기

포트폴리오/Spring Framework

스프링 프레임워크 - 관리자 로그인

관리자 로그인을 위해

아래와 같은 테이블을 생성한다.

 

create table employee(
    id varchar2(20) primary key,
    pwd varchar2(20),
    name varchar2(40),
    email varchar2(40),
    phone varchar2(20));

 

관리자 데이터를 저장할 클래스를 만든다.

<AdminVO.java>

package com.portfolio.biz.employee.dto;

import lombok.Data;

@Data
public class AdminVO {
	private String id;
	private String pwd;
	private String name;
	private String email;
	private String phone;
}

 

그리고 관리자 테이블에서 데이터를 읽어올 수 있도록

mapping 파일을 작성한다.

<admin-mapping.xml>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="AdminDAO">
	<select id="getAdmin" resultType="admin">
		select * from employee where id= #{id} and pwd= #{pwd}
	</select>
</mapper>

 

그리고 관리자 데이터를 담을 프로퍼티를 생성한다.

 

<sql-map-config.xml>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties resource="config/database.properties"/>
	<typeAliases>
		<typeAlias type="com.portfolio.biz.user.dto.UserVO" alias="user"/>
		<typeAlias type="com.portfolio.biz.employee.dto.AdminVO" alias="admin"/>
	</typeAliases>
	
	<mappers>
		<mapper resource="mappings/user-mapping.xml"/>
		<mapper resource="mappings/admin-mapping.xml"/>
	</mappers>
</configuration>

 

그리고 데이터를 가져오는 SQL을 이용하여 데이터를 가져온다.

<AdminDAO.java>

package com.portfolio.biz.employee.impl;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.portfolio.biz.employee.dto.AdminVO;

@Repository
public class AdminDAO {
	
	@Autowired
	private SqlSessionTemplate mybatis;
	
	public AdminVO getAdmin(AdminVO vo) {
		System.out.println("==> Mybatis로 getAdmin() 기능 처리");
		AdminVO admin = mybatis.selectOne("AdminDAO.getAdmin", vo);
		System.out.println("adminDAO admin : " + admin);
		return admin;
	}
}

 

AdminDAO.java를 실행시킬 서비스파일과 서비스를 상속받은 파일을 작성한다.

 

<AdminService.java>

package com.portfolio.biz.employee;

import com.portfolio.biz.employee.dto.AdminVO;

public interface AdminService {

	AdminVO getAdmin(AdminVO vo);

}

<AdminServiceImpl.java>

package com.portfolio.biz.employee.impl;

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

import com.portfolio.biz.employee.AdminService;
import com.portfolio.biz.employee.dto.AdminVO;

@Service("adminService")
public class AdminServiceImpl implements AdminService {
	
	@Autowired
	private AdminDAO adminDAO;

	@Override
	public AdminVO getAdmin(AdminVO vo) {
		return adminDAO.getAdmin(vo);
	}

}

 

그리고 관리자 로그인 페이지와

관리자 로그인 기능을 컨트롤러로 작성한다.

<AdminController.java>

package com.portfolio.view.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.portfolio.biz.employee.AdminService;
import com.portfolio.biz.employee.dto.AdminVO;
import com.portfolio.biz.order.OrderService;
import com.portfolio.biz.order.dto.OrderVO;
import com.portfolio.biz.product.ProductService;
import com.portfolio.biz.product.dto.ProductVO;
import com.portfolio.biz.qna.QnaService;
import com.portfolio.biz.qna.dto.QnaVO;
import com.portfolio.biz.utils.Criteria;
import com.portfolio.biz.utils.PageMaker;

@Controller
@SessionAttributes("adminUser")
public class AdminController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private ProductService productService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private QnaService qnaService;

	@RequestMapping(value = "admin_form")
	public String adminView(HttpSession session, Model model) {
		AdminVO employee = (AdminVO) session.getAttribute("adminUser");

		if (employee == null) {
			return "admin/login";
		} else {
			int monthlyEarnings = orderService.monthlyEarnings();
			int annualEarnings = orderService.annualEarnings();
			int Tasks = orderService.beforeOrderHanling();
			int noReply = qnaService.beforeQnaAnswer();
			
			model.addAttribute("noReply", noReply);
			model.addAttribute("Tasks", Tasks);
			model.addAttribute("monthlyEarnings", monthlyEarnings);
			model.addAttribute("annualEarnings", annualEarnings);
			
			return "admin/index";
		}
	}

	@RequestMapping(value = "admin_login", method = RequestMethod.POST)
	public String adminLoginAction(AdminVO vo, Model model) {
		AdminVO adminUser = adminService.getAdmin(vo);

		if (adminUser == null) {
			model.addAttribute("message", "아이디와 비밀번호를 확인해주세요.");
			return "admin/login";
		} else {
			
			int monthlyEarnings = orderService.monthlyEarnings();
			int annualEarnings = orderService.annualEarnings();
			int Tasks = orderService.beforeOrderHanling();
			int noReply = qnaService.beforeQnaAnswer();
			
			model.addAttribute("noReply", noReply);
			model.addAttribute("Tasks", Tasks);
			model.addAttribute("monthlyEarnings", monthlyEarnings);
			model.addAttribute("annualEarnings", annualEarnings);
			model.addAttribute("adminUser", adminUser);
			return "admin/index";
		}
	}
}

 

관리자 메인 페이지에 노출해야할 부분들은 나중에 정리해서 포스팅한다.

728x90
반응형