# 블로그 만들기: 데이터 등록
## 학습 목표
## 목차
1. 컨트롤러 만들기
2. 페이지 만들기
3. 테이블 만들기
4. VO 만들기
5. Mapper 만들기
## 컨트롤러 만들기
ArticlesController.java
```
@Controller
public class ArticlesController {
@RequestMapping(value = "/articles", method = RequestMethod.GET)
public String index() {
return "articles/index";
}
@RequestMapping(value = "/articles/new", method = RequestMethod.GET)
public String newArticle() {
return "articles/new";
}
@RequestMapping(value ="/articles", method = RequestMethod.POST)
public String create() {
return "redirect:/articles";
}
}
```
## 페이지 생성
articles/index.jsp
```
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page pageEncoding="utf-8" session="false"%>
<!doctype html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="<c:url value="/css/bootstrap.min.css" />" rel="stylesheet">
<title>Hello, world!</title>
</head>
<body class="container">
<div class="jumbotron">
<h1>Articles#index</h1>
<a href="<c:url value="/articles/new" />" class="btn btn-primary" role="button">글쓰기</a>
</div>
<!-- JS -->
<Script src="<c:url value="/js/jquery-3.3.1.min.js" />"></script>
<Script src="<c:url value="/js/bootstrap.min.js" />"></script>
</body>
</html>
```
articles/new.jsp
```
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page pageEncoding="utf-8" session="false"%>
<!doctype html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="<c:url value="/css/bootstrap.min.css" />" rel="stylesheet">
<title>Hello, world!</title>
</head>
<body class="container">
<div class="jumbotron">
<h1>Articles#new</h1>
</div>
<!-- form -->
<form action="<c:url value="/articles" />" method="post">
<div class="form-group">
<label>제목</label>
<input name="title" class="form-control" type="text" placeholder="제목을 입력하세요.">
</div>
<div class="form-group">
<label>내용</label>
<textarea name="body" class="form-control" rows="3" placeholder="내용을 입력하세요."></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<!-- JS -->
<Script src="<c:url value="/js/jquery-3.3.1.min.js" />"></script>
<Script src="<c:url value="/js/bootstrap.min.js" />"></script>
</body>
</html>
```
## 테이블 만들기
create table
```
create table articles (
id serial primary key,
title varchar(50),
body varchar(10000)
);
```
## VO 생성
```
package com.mycompany.vo;
public class Article {
private int id;
private String title;
private String body;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
return "Article [id=" + id + ", title=" + title + ", body=" + body + "]";
}
}
```
## form 전송 테스트
ArticlesController.java
```
@Controller
public class ArticlesController {
...
@RequestMapping(value ="/articles", method = RequestMethod.POST)
public String create(@ModelAttribute Article article) {
System.out.println(article); // form 데이터 전달 확인
return "redirect:/articles";
}
}
```
## form 데이터 한글 깨짐 방지
web.xml
```
<webapp>
...
<!-- UTF-8 Encoding: 한글 깨짐 방지. -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</webapp>
```
## Mapper 만들기
ArticleMapper.java
```
package com.mycompany.mapper;
import org.apache.ibatis.annotations.Insert;
import com.mycompany.vo.Article;
public interface ArticleMapper {
@Insert("insert into articles (title, body) values (#{title}, #{body})")
public boolean create(Article article);
}
```
매퍼등록: root-context.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:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- properties -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:/jdbc.properties" />
<property name="fileEncoding" value="UTF-8" />
</bean>
<!-- JDBC-PostgreSQL -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- MyBatis-Spring -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mappers: 해당 패키지에서 자동으로 매퍼 스캔 -->
<mybatis:scan base-package="com.mycompany.mapper" />
</beans>
```
ArticlesController.java
```
@Controller
public class ArticlesController {
@Autowired
private ArticleMapper articleMapper;
...
@RequestMapping(value ="/articles", method = RequestMethod.POST)
public String create(@ModelAttribute Article article) {
System.out.println(article); // form 전달 확인
articleMapper.create(article); // 매퍼를 통한 데이터 생성
return "redirect:/articles";
}
}
```
## 테스트
ArticleController.java
```
```