간단하게 index.jsp 파일에 MRS 데이터베이스의 Employee 테이블의 정보를 출력하는 과정을 통해 DB 연동 방법을 살펴보자.
package dto;
import java.sql.Date;
public class EmployeeVO {
private String employee_id;
private String employee_password;
private String employee_department;
private String employee_manager;
private String employee_email;
private String employee_contact;
private Date employee_last_access_date;
public String getEmployee_id() {
return employee_id;
}
public void setEmployee_id(String employee_id) {
this.employee_id = employee_id;
}
public String getEmployee_password() {
return employee_password;
}
public void setEmployee_password(String employee_password) {
this.employee_password = employee_password;
}
public String getEmployee_department() {
return employee_department;
}
public void setEmployee_department(String employee_department) {
this.employee_department = employee_department;
}
public String getEmployee_manager() {
return employee_manager;
}
public void setEmployee_manager(String employee_manager) {
this.employee_manager = employee_manager;
}
public String getEmployee_email() {
return employee_email;
}
public void setEmployee_email(String employee_email) {
this.employee_email = employee_email;
}
public String getEmployee_contact() {
return employee_contact;
}
public void setEmployee_contact(String employee_contact) {
this.employee_contact = employee_contact;
}
public Date getEmployee_last_access_date() {
return employee_last_access_date;
}
public void setEmployee_last_access_date(Date employee_last_access_date) {
this.employee_last_access_date = employee_last_access_date;
}
}
package dao;
import java.util.List;
import dto.EmployeeVO;
public interface EmployeeDAO {
public List<EmployeeVO> selectEmployee() throws Exception;
}
package dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import dao.EmployeeDAO;
import dto.EmployeeVO;
@Repository("EmployeeDAO")
public class EmployeeDAOImpl implements EmployeeDAO{
@Resource(name = "sqlSession")
private SqlSession sqlSession;
@Override
public List<EmployeeVO> selectEmployee() throws Exception {
return sqlSession.selectList("mapper.employeeMapper.selectEmployee");
}
}
package service;
import java.util.List;
import dto.EmployeeVO;
public interface EmployeeService {
public List<EmployeeVO> selectEmployee() throws Exception;
}
package service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import dao.EmployeeDAO;
import dto.EmployeeVO;
import service.EmployeeService;
@Service("EmployeeService")
public class EmployeeServiceImpl implements EmployeeService {
@Resource(name = "EmployeeDAO")
private EmployeeDAO employeeDao;
@Override
public List<EmployeeVO> selectEmployee() throws Exception {
return employeeDao.selectEmployee();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.employeeMapper">
<select id="selectEmployee" resultType="EmployeeVO">
SELECT *
FROM employee
</select>
</mapper>
package controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import dto.EmployeeVO;
import service.impl.EmployeeServiceImpl;
@Controller
public class EmployeeController {
@Resource(name = "EmployeeService")
private EmployeeServiceImpl employeeService;
// 직원 데이터
@RequestMapping(value = "emp")
public String employee(Model model) throws Exception {
List<EmployeeVO> employeeList = employeeService.selectEmployee();
model.addAttribute("employeeList", employeeList);
return "/index.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Employee Info</title>
</head>
<body>
<h1>Employee Information</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Password</th>
<th>Department</th>
<th>Manager</th>
<th>Email</th>
<th>Contact</th>
<th>Last Access Date</th>
</tr>
</thead>
<tbody>
<c:forEach items="${employeeList}" var="employee">
<tr>
<td>${employee.employee_id}</td>
<td>${employee.employee_password}</td>
<td>${employee.employee_department}</td>
<td>${employee.employee_manager}</td>
<td>${employee.employee_email}</td>
<td>${employee.employee_contact}</td>
<td>${employee.employee_last_access_date}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>