728x90
반응형
mybatis에서는 일단 라이브러리를 추가해야 하며
더이상 servlet.xml에서 viewResolver와 컨트롤러를 제외하고는 필요하지 않다.
DAO를 필요로 하지 않는다.
■ src
□ Confifuration.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="student/mybatis/db.properties"/>
<typeAliases>
<typeAlias type="student.dto.StudentDTO" alias="studentDTO"/>
</typeAliases>
<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="student/mybatis/studentMapper.xml"/>
</mappers>
</configuration>
■ student.dto package
□ StudentDTO.java
package student.dto;
public class StudentDTO {
private String id;
private String name;
private String cname;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
■ student.controller package
□ StudentAnController.java
package student.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import student.dto.StudentDTO;
import student.mybatis.StudentMapper;
@Controller
public class StudentAnController {
@RequestMapping("/student.do")
public String student() {
return "student";
}
//리턴타입 : String, Model, ModelAndView, View객체, void
@RequestMapping("/insert.do")
public String insertStudent(HttpServletRequest req,
@ModelAttribute StudentDTO dto) {
int res = StudentMapper.insertStudent(dto);
if (res>0) {
req.setAttribute("msg", "학생등록 성공!! 학생목록페이지로 이동합니다.");
req.setAttribute("url", "list.do");
}else {
req.setAttribute("msg", "학생등록 실패!! 학생관리페이지로 이동합니다.");
req.setAttribute("url", "student.do");
}
return "forward:message.jsp";
}
@RequestMapping("/delete.do")
public ModelAndView deleteStudent(@RequestParam String id) {
int res = StudentMapper.deleteStudent(id);
ModelAndView mav = new ModelAndView();
if (res>0) {
mav.addObject("msg", "학생삭제 성공!! 학생목록페이지로 이동합니다.");
mav.addObject("url", "list.do");
}else {
mav.addObject("msg", "학생삭제 실패!! 학생관리페이지로 이동합니다.");
mav.addObject("url", "student.do");
}
mav.setViewName("forward:message.jsp");
return mav;
}
@RequestMapping("/find.do")
public String findStudent(HttpServletRequest req,
@RequestParam String cname) {
List<StudentDTO> find = StudentMapper.findStudent(cname);
req.setAttribute("listStudent", find);
return "list";
}
@RequestMapping("list.do")
public String listStudent(HttpServletRequest req) {
List<StudentDTO> list = StudentMapper.listStudent();
req.setAttribute("listStudent", list);
return "list";
}
}
■ student.mybatis package
□ db.properties
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:xe
username : web01
password = web01
□ studentMapper.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="student.mybatis.studentMapper">
<select id="listStudent" resultType="studentDTO">
select * from student
</select>
<insert id="insertStudent" parameterType="studentDTO">
insert into student values(#{id}, #{name}, #{cname})
</insert>
<delete id="deleteStudent" parameterType="String">
delete from student where id = #{id}
</delete>
<select id="findStudent" parameterType="String" resultType="studentDTO">
select * from student where cname = #{cname}
</select>
</mapper>
□ StudentMapper.java
package student.mybatis;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import student.dto.StudentDTO;
public class StudentMapper {
private static SqlSessionFactory sqlMapper;
static {
try {
String resource = "Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
}catch(IOException e) {
throw new RuntimeException("Something bad happened while "
+ "building the SqlMapClient instance." + e, e);
}
}
public static List<StudentDTO> listStudent(){
SqlSession session = sqlMapper.openSession();
try{
List<StudentDTO> list = session.selectList("listStudent");
return list;
}finally{
session.close();
}
}
public static int insertStudent(StudentDTO dto) {
SqlSession session = sqlMapper.openSession();
try{
int res = session.insert("insertStudent", dto);
session.commit();
return res;
}finally{
session.close();
}
}
public static int deleteStudent(String id) {
SqlSession session = sqlMapper.openSession();
try{
int res = session.delete("deleteStudent", id);
session.commit();
return res;
}finally{
session.close();
}
}
public static List<StudentDTO> findStudent(String cname){
SqlSession session = sqlMapper.openSession();
try{
List<StudentDTO> list = session.selectList("findStudent", cname);
return list;
}finally{
session.close();
}
}
}
■ WebContent
□ index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- index.jsp -->
<html>
<head>
<title>spring학생관리</title>
</head>
<body>
<h1 align="center">스프링으로 연습하는 학생관리 프로그램</h1>
<ul>
<li><a href="student.do">학생관리페이지로 이동</a>
</ul>
</body>
</html>
□ message.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- message.jsp -->
<script type="text/javascript">
alert("${msg}")
location.href="${url}"
</script>
■ WEB-INF
□ student-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/student/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class="student.controller.StudentAnController">
</bean>
</beans>
□ web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>student</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>student</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
■ WEB-INF/student
□ list.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- list.jsp -->
<html>
<head>
<title>학생목록</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>학 생 목 록 페 이 지</h2>
<hr color="green" width="300">
<table border="1" width="500">
<tr>
<th>아이디</th>
<th>학생명</th>
<th>학급명</th>
</tr>
<c:if test="${empty listStudent}">
<tr>
<td colspan="3">등록된 학생이 없습니다.</td>
</tr>
</c:if>
<c:forEach var="dto" items="${listStudent}">
<tr>
<td>${dto.id}</td>
<td>${dto.name}</td>
<td>${dto.cname}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
□ student.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- student.jsp -->
<html>
<head>
<title>학생관리</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>학 생 등 록 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="insert.do" method="post">
<table border="1">
<tr>
<td align="center">
아이디 : <input type="text" name="id"><br>
학생명 : <input type="text" name="name"><br>
학급명 : <input type="text" name="cname"><br>
<input type="submit" value="전송">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 삭 제 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="delete.do" method="post">
<table border="1">
<tr>
<td align="center">
아이디 : <input type="text" name="id">
<input type="submit" value="삭제">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 찾 기 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="find.do" method="post">
<table border="1">
<tr>
<td align="center">
학급명 : <input type="text" name="cname">
<input type="submit" value="찾기">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 목 록 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="list.do" method="post">
<table border="1">
<tr>
<td align="center">
<input type="submit" value="학생목록페이지로 이동">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
728x90
반응형
'IT&코딩 > 자바 프로젝트' 카테고리의 다른 글
자바 프로젝트 17일차 - mybatis 3 (springMember3) (0) | 2023.02.12 |
---|---|
자바 프로젝트 17일차 - mybatis 2 (springBoard3) (0) | 2023.02.11 |
자바 프로젝트 15일차 - Annotation 3 (springMember2) (0) | 2023.02.10 |
자바 프로젝트 14일차 - Annotation 2 (springBoard2) (0) | 2023.02.07 |
자바 프로젝트 13일차 - Annotation 1 (springStudent2) (0) | 2023.02.06 |