mySQL 설치 후 테이블 생성
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 테이블 생성 | |
CREATE TABLE board ( | |
board_no INT(10) NOT NULL COMMENT '글 번호', | |
board_title VARCHAR(50) NOT NULL COMMENT '제목', | |
board_content TEXT NOT NULL COMMENT '내용', | |
board_user VARCHAR(50) NOT NULL COMMENT '사용자', | |
board_pw VARCHAR(20) NOT NULL COMMENT '비밀번호', | |
board_date DATE NOT NULL COMMENT '작성날짜', | |
PRIMARY KEY (board_no) | |
)DEFAULT CHARSET=euckr | |
COMMENT '게시글DB'; | |
ALTER TABLE board | |
MODIFY COLUMN board_no INT NOT NULL AUTO_INCREMENT COMMENT '글 번호'; | |
--테스트를 위한 기본 값 추가 | |
INSERT INTO board | |
(board_title, board_content, board_user, board_pw, board_date) | |
VALUES ('test01', 'test01', 'test01', 'test01', NOW()) | |
INSERT INTO board | |
(board_title, board_content, board_user, board_pw, board_date) | |
VALUES ('test02', 'test02', 'test02', 'test02', NOW()) | |
INSERT INTO board | |
(board_title, board_content, board_user, board_pw, board_date) | |
VALUES ('test03', 'test03', 'test03', 'test03', NOW()) | |
INSERT INTO board | |
(board_title, board_content, board_user, board_pw, board_date) | |
VALUES ('test04', 'test04', 'test04', 'test04', NOW()) | |
web.xml 설정
spring안의 DispatcherServlet 을 appServlet이라 명하고 실행되면 첫 번째로 servlet-context.xml 객체를 호출한다
모든 요청을 appServlet 이 가로채게 매핑 해놓았다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
version="3.0"> | |
<!-- Processes application requests --> | |
<servlet> | |
<servlet-name>appServlet</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
<init-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>/WEB-INF/servlet-context.xml</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>appServlet</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
</web-app> | |
servlet-context.xml 설정
spring에서 사용하는 객체들을 bean과 닮았다 하여 bean이라 불리며 설정을 해놓으면 자동적으로 객체가 생성된다.
/WEB-INF/views/board/boardAdd.jsp 라 적어야 하지만 중복되는 것을 자동으로 붙게 설정해 놓아
우리가 /board/boardAdd 라고만 요청하더라도 주소를 완성할수 있게 해주고 매번 DB연결을 매번 하지 않도록 미리 객체를 생성해
mabatis에 주입해 조금더 편리하게 개발을 할수 있도록 도와준다.
<mvc:default-servlet-handler/>를 추가하면 모든 요청들을 dispatcherServlet이 가로채게 되는데
못찾으면 404를 내지 않고 톰캣한테 넘겨 호출한다.
(사이트에서 팝업창을 띄울때 편하게 사용하려고 많이 이용한다)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns="http://www.springframework.org/schema/beans" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd | |
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.xsd"> | |
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> | |
<!-- Enables the Spring MVC @Controller programming model --> | |
<mvc:annotation-driven /> | |
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> | |
<mvc:resources mapping="/resources/**" location="/resources/" /> | |
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> | |
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> | |
<property name="prefix" value="/WEB-INF/views/" /> | |
<property name="suffix" value=".jsp" /> | |
</bean> | |
<context:component-scan base-package="com.tistory.luahius" /> | |
<!--1) dataSource 객체를 생성 connection의 arrayList가 된다 하지만 우리는 mybatis가 할것 이니까 이건 mybatis가 가지면 된다 --> | |
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> | |
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- 프로퍼티란 주입, setter --> | |
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jjdev"/> | |
<property name="username" value="root"/> | |
<property name="password" value="java0000"/> | |
</bean> | |
<!--2) mybatis 세션생성을 위한 팩토리 생성 --> | |
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> | |
<!-- mybatis 세션생성시 사용할 dataSource주입 ref : 참조타입이 들어오는 것--> | |
<property name="dataSource" ref="dataSource" /> | |
<!-- mybatis 세션생성후 쿼리를 실행시킬때 사용할 쿼리위치(메퍼)설정 dao가 있는 곳에--> | |
<property name="mapperLocations"> | |
<list> | |
<value>classpath:com/tistory/luahius/service/BoardMapper.xml</value> | |
</list> | |
</property> | |
</bean> | |
<!--3) mybatis를 사용하여 db에접속하여 쿼리를 보내는 주체인 SqlSessionTemplate빈 생성--> | |
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> | |
<constructor-arg index="0" ref="sqlSessionFactory" /> <!-- 생성자의 첫번째의 매개변수로 주입된다 --> | |
</bean> | |
</beans> |
Board.java DTO 생성
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tistory.luahius.service; | |
public class Board { | |
private int boardNo; | |
private String boardPw; | |
private String boardTitle; | |
private String boardContent; | |
private String boardUser; | |
private String boardDate; | |
public int getBoardNo() { | |
return boardNo; | |
} | |
public void setBoardNo(int boardNo) { | |
this.boardNo = boardNo; | |
} | |
public String getBoardPw() { | |
return boardPw; | |
} | |
public void setBoardPw(String boardPw) { | |
this.boardPw = boardPw; | |
} | |
public String getBoardTitle() { | |
return boardTitle; | |
} | |
public void setBoardTitle(String boardTitle) { | |
this.boardTitle = boardTitle; | |
} | |
public String getBoardContent() { | |
return boardContent; | |
} | |
public void setBoardContent(String boardContent) { | |
this.boardContent = boardContent; | |
} | |
public String getBoardUser() { | |
return boardUser; | |
} | |
public void setBoardUser(String boardUser) { | |
this.boardUser = boardUser; | |
} | |
public String getBoardDate() { | |
return boardDate; | |
} | |
public void setBoardDate(String boardDate) { | |
this.boardDate = boardDate; | |
} | |
@Override | |
public String toString() { | |
return "Board [boardNo=" + boardNo + ",\n boardPw=" + boardPw + ",\n boardTitle=" + boardTitle + ",\n boardContent=" | |
+ boardContent + ",\n boardUser=" + boardUser + ",\n boardDate=" + boardDate + "]\n\n"; | |
} | |
} |
'프로그래밍 > 프로젝트' 카테고리의 다른 글
[MVC] 게시판 예제 2) 입력하기 (0) | 2017.01.06 |
---|---|
주제선정 최종 (0) | 2017.01.02 |
팀원 구성 ! (0) | 2016.12.26 |