반응형

# Spring WEB MVC_03버전_02 게시판 만들기

  • 이클립스 전자정부 프레임워크 3.10.0
  • 데이터베이스 : MySQL

## SpringMVC03 생성

  • File > New > Spring Legacy Project > 프로젝트명은 SpringMVC03, Templates은 Spring MVC Project 선택 후 Next > 패키지 명 kr.inflearn.web 로 생성

## pom.xml

  • 스프링 버전 4.2.4로 변경
<properties>
  <java-version>1.6</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
  • 자바 버전 1.8로 변경
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.5.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <compilerArgument>-Xlint:all</compilerArgument>
    <showWarnings>true</showWarnings>
    <showDeprecation>true</showDeprecation>
  </configuration>
</plugin>
  • 설정 완료 후 생성한 프로젝트 우 클릭 > Maven > Update Project.. 진행하여 변경사항 적용.

## 패키지 생성

  • src/main/java 우 클릭 > New > Package > kr.inflearn.mapper 이름의 패키지 생성 (mapper)
  • src/main/java 우 클릭 > New > Package > kr.inflearn.model 이름의 패키지 생성 (model)
  • src/main/java 우 클릭 > New > Package > kr.inflearn.service 이름의 패키지 생성 (service)

## 폴더명 변경

  • src > main > webapp > WEB-INF > views 폴더명 board로 변경

## BoardController 생성

  • kr.inflearn.web 우 클릭 > New > Class > BoardController 이름으로 생성

## board.sql 생성

  • kr.inflearn.mapper 우 클릭 > New > Other > General > File > board.sql 생성
  • 생성 후 Data Source Exploere 탭에서 MySQL 연결된 상태에서 테이블 생성 작업 진행.
create table tb_board(
	idx int not null auto_increment,			-- 아이디 (자동증가)
	title varchar(100) not null,				-- 제목
	contents varchar(4000) not null,			-- 내용
	count int,						-- 조회수
	writer varchar(30) not null,				-- 등록자
	indate datetime default now() not null,			-- 등록일
	primary key(idx)
);
  • 테이블 생성 후 아래와 같이 임시 데이터 넣어서 테스트 진행
insert into tb_board(title, contents, count, writer)
values('게시판 만들기', '게시판 만들기', 0, '관리자');

select * from tb_board;

## BoardVO 생성

  • kr.inflearn.model 우 클릭 > New > Class > BoardVO 이름으로 생성
  • 테이블과 동일한 구조로 생성
package kr.inflearn.model;

public class BoardVO {
	private int idx;
	private String title;
	private String contents;
	private int count;
	private String writer;
	private String indate;
	
	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 getContents() {
		return contents;
	}
	public void setContents(String contents) {
		this.contents = contents;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getIndate() {
		return indate;
	}
	public void setIndate(String indate) {
		this.indate = indate;
	}
	
	@Override
	public String toString() {
		return "BoardVO [idx=" + idx + ", title=" + title + ", contents=" + contents + ", count=" + count + ", writer="
				+ writer + ", indate=" + indate + "]";
	}	
}

 

# Spring WEB MVC_03버전_03 게시판 영속계층 구현(Mapper Interface + XML)

## BoardMapper 인터페이스 생성

  • kr.inflearn.mapper 우 클릭 > New > Interface > BoardMapper 이름으로 생성
package kr.inflearn.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import kr.inflearn.model.BoardVO;

// 영속 계층
@Mapper
public interface BoardMapper {
	public List<BoardVO> getList();		// 게시물 리스트 가져오기
	public void insert(BoardVO board);	// 게시물 등록
	public BoardVO read(int bno);		// 게시물 상세보기
	public int delete(int bno);			// 게시물 삭제
	public int update(BoardVO board);	// 게시물 수정
}

## Mapper 생성

  • 아래 MyBatis 사이트에서 Exploring Mapped SQL Statements 참고하여 작성 진행
<?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="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>
 

mybatis – MyBatis 3 | Getting started

It's very important to understand the various scopes and lifecycles classes we've discussed so far. Using them incorrectly can cause severe concurrency problems. Dependency Injection frameworks can create thread safe, transactional SqlSessions and mappers

mybatis.org

  • kr.inflearn.mapper 우 클릭 > New > Other > XML File > BoardMapper 이름으로 생성
  • 일반적으로 Mapper 인터페이스의 이름과 Mapper 파일의 이름을 동일하게 만들어주는게 좋다.
  • Mapper 인터페이스와 Mapper 파일의 연동을 위해 namespace 일치 시키기.
<?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="kr.inflearn.mapper.BoardMapper">
	
    <select id="getList" resultType="boardVO">
        select * from tb_board
    </select>
    
    <insert id="insert" parameterType="boardVO">
        insert into tb_board(title, contents, count, writer)
        values(#{title}, #{contents}, #{count}, #{writer})
    </insert>
    
    <select id="read" parameterType="Integer" resultType="boardVO">
        select * from tb_board where idx = #{idx}
    </select>
    
    <delete id="delete" parameterType="Integer">
       delete * from tb_board where idx = #{idx} 
    </delete>
    
    <update id="update" parameterType="boardVO">
        update tb_board set title = #{title}, contents = #{contents}
        where idx = #{idx}
    </update>
    
</mapper>

## root-context

  • Mapper 패키지 스캔하여 BoardMapper 인터페이스와 BoardMapper.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"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
	http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	
	<!-- MyBatis SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource"  ref="dataSource"/>
	    <property name="configLocation" value="/WEB-INF/mybatis/config.xml"/>
	</bean>
	<!-- JDBC 연결(DataSource) -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${driver}"/>
		<property name="url" value="${url}"/>
		<property name="username" value="${username}"/>
		<property name="password" value="${password}"/>
	</bean>
	<!-- db.properties 파일 연결 -->
	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations" value="/WEB-INF/mybatis/db.properties"/>
	</bean>
	
	<mybatis-spring:scan base-package="kr.inflearn.mapper" />
</beans>

## config.xml

  • WEB-INF > New > Folder > mybatis 이름의 폴더 생성
  • mybatis 폴더에 config.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>
	<typeAliases><!-- 별칭 -->
	    <typeAlias type="kr.inflearn.model.BoardVO" alias="boardVO"/>
	</typeAliases>
</configuration>

## root-context

<?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"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
	http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	
	<!-- MyBatis SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource"  ref="dataSource"/>
	    <property name="configLocation" value="/WEB-INF/mybatis/config.xml"/>
	</bean>
	<!-- JDBC 연결(DataSource) -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${driver}"/>
		<property name="url" value="${url}"/>
		<property name="username" value="${username}"/>
		<property name="password" value="${password}"/>
	</bean>
	<!-- db.properties 파일 연결 -->
	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations" value="/WEB-INF/mybatis/db.properties"/>
	</bean>
	
	<mybatis-spring:scan base-package="kr.inflearn.mapper" />
</beans>
반응형

+ Recent posts