상세 컨텐츠

본문 제목

[1] [Spring] [Mybatis] 게시판 select 사용 예제

Spring

by kwanghyup 2019. 11. 5. 14:39

본문

데이터베이스 구성 

create table freeboard (
	idx int auto_increment primary key,
    title varchar(50),
    content varchar(300),
    rgdate varchar(30)
) 
default charset='utf8';

insert into freeboard (title,content,rgdate) values('aaa','bbb',now());

테스트를 위해 여러 개의 칼럼을 insert 한다. 

 

Spring MVC 프로젝트 생성

프로젝트명 : freeboard 

패키지 : com.kh.test

 

Mybatis 설정

https://k-develpoper.tistory.com/3

 

[Spring MVC Project] mybatis DB 연동 2

시작 Step1 : 데이터베이스 구성 테이블명 : test 칼럼 : id, pw Step2: 프로젝트 생성 프로젝트명 : mybatis 패키지 : com.kb.mybatis Step3 : pom.xml 파일 dependency 설정 pom.xml 파일 설정

k-develpoper.tistory.com

한글 설정 : web.xml 다음을 추가 

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

Step1.

패키지 com.kh.test.freeboard

FreeBoardDTO.java 파일 

package com.kh.test.freeboard;

public class FreeBoardDTO {
	
	private int idx; 
	private String title;
	private String content; 
	private String rgdate;
	
    public FreeBoardDTO(){}
	public FreeBoardDTO(int idx, String title, String content, String rgdate) {
		super();
		this.idx = idx;
		this.title = title;
		this.content = content;
		this.rgdate = rgdate;
	}
	
	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getRgdate() {
		return rgdate;
	}

	public void setRgdate(String rgdate) {
		this.rgdate = rgdate;
	}

	@Override
	public String toString() {
		return "FreeBoardDTO [idx=" + idx + ", title=" + title + ", content=" + content + ", date=" + rgdate + "]";
	} 
	
}

FreeBoardController.java 파일 

package com.kh.test.freeboard;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FreeBoardController {
	
	@Autowired
	SqlSession ss; 
		
	List<FreeBoardDTO> list = new ArrayList<>(); 
	@RequestMapping(value="freeboard/index.do")
	public String index(Model model) {
		list = ss.selectList("freeboard.selectlist");
		model.addAttribute("list",list);
		return "freeboard/index";
	}
	
}

[21행] selectList("freeboard.selectlist")

    freeboard :  freeboardMapper.xml파일 - mapper태그의 namespace에 해당

    selectlist : freeboardMapper.xml파일 - mpper태그 - select태그의 id에 해당 

 

[22행] model.addAttribute("list",list); 

   request.setAttribute("list",list); 와 같은 기능 


Step2 

/src/main/resources 아래 

freeboardMapper.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="freeboard">
  <select id="selectlist" resultType="com.kh.test.freeboard.FreeBoardDTO">
  	select * from freeboard
  </select>
</mapper>

Step3 : jsp파일 

 

views/head.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!-- path -->
<c:set var="path" value="${pageContext.request.contextPath}"/>
<!-- bootsTrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

views/home.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
	<meta charset="utf-8">
	<%@include file="head.jsp" %>
</head>
<body>
	<div class="container">
		<div class="row">
			<nav class="navbar navbar-default">
				<div class="container-fluid">
					<div class="navbar-header">
						<a class="navbar-brand" href="#">신입개발자</a>
					</div>
					<ul class="nav navbar-nav">
						<li class="active"><a href="#">Home</a></li>
						<li><a href="${path}/freeboard/index.do">게시판</a></li>
					</ul>
				</div>
			</nav>
		</div>		
		<div class="row">
			<div class="jumbotron">
				<h1>포트폴리오</h1>
				<p>신입개발자 준비 중 </p>
			</div>
			<p>java jsp 학습하고 spring 학습중  </p>
			<p>html css javascript jquery 학습 중</p>
		</div>
	</div>
</body>
</html>

 

views/freeboard폴더 생성

views/freeboard/index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Freeboard</title>
	<meta charset="utf-8">
	<%@include file="../head.jsp" %>
</head>
<body>
	<div class="container">
		
		<div class="row">
			<nav class="navbar navbar-default">
				<div class="container-fluid">
					<div class="navbar-header">
						<a class="navbar-brand" href="#">신입개발자</a>
					</div>
					<ul class="nav navbar-nav">
						<li ><a href="${path}">Home</a></li>
						<li class="active"><a href="${path}/freeboard/index.do">게시판</a></li>
					</ul>
				</div>
			</nav>
		</div>
		
		<!-- 광고 -->
		<div class="row">
			<div class="jumbotron">
				<h1>게시판</h1>
			</div>
			<p>mysql과 mybatis를 이용하여 게시판 구현</p>
			
		</div>
		
		<!--바디  -->
		<div class="row">
			<h2>게시판 목록</h2>
			<table class="table table-hover table-bordered">
				<tr>
					<th>순번</th>
					<th>제목</th>
					<th>내용</th>
					<th>날짜</th>
				</tr>
				<c:forEach items="${list}" var="fb">
				<tr>	
					<td>${fb.idx }</td>
					<td>${fb.title }</td>
					<td>${fb.content }</td>
					<td>${fb.rgdate }</td>
				</tr>	
				</c:forEach>
			</table>		
		</div>
		<div class="row">
			<div class="col-sm-1">
				<a class="btn btn-primary" href="#">글쓰기</a>
			</div>
		</div>
		
	</div>
</body>
</html>

 

관련글 더보기

댓글 영역