728x90
Spring Legacy · STS · OracleDB · SQLDeveloper
저번에 정리했던 게시판 만들기 총정리 겸 복습 포스팅입니다.
자세한 설명은 아래 링크참고
1번:https://rebornbb.tistory.com/entry/Spring-Spring-DTO-DAO-%EC%83%9D%EC%84%B1%ED%95%98%EA%B8%B0
게시판 게시글등록,삭제,수정,확인 총 흐름 정리
- pom.xml 데이터베이스 설정 (DB에 필요 dependency 추가, 자바버전 properties 확인)
- DB담을 DTO 생성
- DB작업할 DAO 생성
- DAO에서 사용할 mapper (Mapper.xml과 mybatis-config.xml) 생성
- root-context.xml 에 실제 DB에 연결하는 작업 + service package 와 Namespace context 설정
- Service (비즈니스로직) 실제 실행되는 프로그램을 만든다 DAO 호출
- 최종적으로 사용자가 요청하면 Controller에서 서비스를 호출하여 view로 넘겨준다
- Views에 jsp를 작성해 화면을 출력한다
1.pom.xml 데이터베이스 설정 (DB에 필요 dependency 추가, 자바버전 properties 확인)
<!-- 자바버전확인 -->
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.3.5.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- DB설정 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
2.DB담을 DTO 생성
package com.human.dto;
import java.util.Date;
public class BoardDto {
private int bno;
private String title;
private String content;
private String writer;
private Date regdate;
private int viewcnt;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
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 getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public int getViewcnt() {
return viewcnt;
}
public void setViewcnt(int viewcnt) {
this.viewcnt = viewcnt;
}
@Override
public String toString() {
return "BoardDto [bno=" + bno + ", title=" + title + ", content=" + content + ", writer=" + writer
+ ", regdate=" + regdate + ", viewcnt=" + viewcnt + "]";
}
}
3.DB작업할 DAO 생성
package com.human.dao;
import java.util.List;
import com.human.dto.BoardDto;
public interface IBoardDao {
public void create(BoardDto dto) throws Exception;
public BoardDto read(int bno) throws Exception;
public void delete(int bno) throws Exception;
public void update(BoardDto dto) throws Exception;
public List<BoardDto> listAll() throws Exception;
}
4.DAO에서 사용할 mapper (Mapper.xml과 mybatis-config.xml) 생성
Mapper.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="com.human.dao.IBoardDao">
<insert id="create">
insert into tbl_board(bno,title,content,writer)
values(seq_board.nextval,#{title},#{content},#{writer})
</insert>
<select id="read" resultType="BoardDto">
select * from tbl_board where bno=#{bno}
</select>
<delete id="delete">
delete from tbl_board where bno=#{bno}
</delete>
<update id="update">
update tbl_board set title=#{title},content=#{content} where bno=#{bno}
</update>
<select id="listAll" resultType="com.human.dto.BoardDto">
select * from tbl_board order by bno desc,regdate desc
</select>
</mapper>
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>
<package name="com.human.dto"/>
</typeAliases>
</configuration>
5.root-context.xml 에 실제 DB에 연결하는 작업 + service package 와 Namespace 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- Namespace-context설정 -->
<!-- Root Context: defines shared resources visible to all other web components -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
name="dataSource">
<property value="oracle.jdbc.driver.OracleDriver" name="driverClassName"/>
<property value="jdbc:oracle:thin:@localhost:1521:xe" name="url"/>
<property value="c##human" name="username"/>
<property value="human" name="password"/>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:/mybatis-config.xml"></property>
<property name="mapperLocations"
value="classpath:mappers/**/*Mapper.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory"
ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- Service설정 -->
<context:component-scan base-package="com.human.service"></context:component-scan>
</beans>
6.Service (비즈니스로직) 실제 실행되는 프로그램을 만든다 DAO 호출
package com.human.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.human.dao.IBoardDao;
import com.human.dto.BoardDto;
@Service
public class BoardServiceImpl implements IBoardService {
@Autowired
private SqlSession sqlSession;
@Override
public void create(BoardDto dto) throws Exception {
//추가 비지니스로직을 기술하기 위해서 service 인터페이스를 추가하였다.
IBoardDao dao=sqlSession.getMapper(IBoardDao.class);
dao.create(dto);
}
@Override
public BoardDto read(int bno) throws Exception {
// TODO Auto-generated method stub
IBoardDao dao=sqlSession.getMapper(IBoardDao.class);
BoardDto r=dao.read(bno);
return r;
}
@Override
public void delete(int bno) throws Exception {
// TODO Auto-generated method stub
IBoardDao dao=sqlSession.getMapper(IBoardDao.class);
dao.delete(bno);
}
@Override
public void update(BoardDto dto) throws Exception {
// TODO Auto-generated method stub
IBoardDao dao=sqlSession.getMapper(IBoardDao.class);
dao.update(dto);
}
@Override
public List<BoardDto> listAll() throws Exception {
IBoardDao dao=sqlSession.getMapper(IBoardDao.class);
List<BoardDto> dtos=dao.listAll();
return dtos;
}
}
package com.human.service;
import java.util.List;
import com.human.dto.BoardDto;
public interface IBoardService {
public void create(BoardDto dto) throws Exception;
public BoardDto read(int bno) throws Exception;
public void delete(int bno) throws Exception;
public void update(BoardDto dto) throws Exception;
public List<BoardDto> listAll() throws Exception;
}
7.최종적으로 사용자가 요청하면 Controller에서 서비스를 호출하여 view로 넘겨준다
package com.human.ex;
import java.util.Locale;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.human.dto.BoardDto;
import com.human.service.IBoardService;
@Controller
public class BoardController {
@Inject
private IBoardService service;
//@Inject = @Autowired 와 기능동일
@RequestMapping(value = "/board/remove", method = RequestMethod.GET)
public String remove(@RequestParam("bno")int bno
,RedirectAttributes rttr) throws Exception {
service.delete(bno);
rttr.addFlashAttribute("msg","success");
return "redirect:/board/listAll";
}
@RequestMapping(value = "/board/modify", method = RequestMethod.POST)
public String modify(BoardDto boardDto,Model model
,RedirectAttributes rttr) throws Exception {
service.update(boardDto);
rttr.addFlashAttribute("msg","success");
return "redirect:/board/listAll";
}
@RequestMapping(value = "/board/modify", method = RequestMethod.GET)
public void modify(@RequestParam("bno")int bno,Model model) throws Exception {
model.addAttribute(service.read(bno));
//model.addAttribute("boardDto",service.read(bno));
}
@RequestMapping(value = "/board/read", method = RequestMethod.GET)
public void read(@RequestParam("bno")int bno,Model model) throws Exception {
model.addAttribute(service.read(bno));
//model.addAttribute("boardDto",service.read(bno));
}
@RequestMapping(value = "/board/register", method = RequestMethod.GET)
public void register(Model model) throws Exception {
}
@RequestMapping(value = "/board/register", method = RequestMethod.POST)
public String register(BoardDto boardDto,Model model,RedirectAttributes rttr) throws Exception {
service.create(boardDto);
rttr.addFlashAttribute("msg","success");
return "redirect:/board/listAll";
}
@RequestMapping(value = "/board/listAll", method = RequestMethod.GET)
public void listAll(Model model) throws Exception {
//DB에 있는 모든데이터를 화면에 보여주기
//DB에 있는 모든데이터를 읽어다주는 service를 실해서
//모델의 list 에 담는다.
model.addAttribute("list",service.listAll());
}
}
8.Views에 jsp를 통해 화면을 출력한다
DB 데이터 삽입 SQL
create table tbl_board (
bno number,
title varchar2(200) not null,
content varchar2(4000) not null,
writer varchar2(50) not null,
regdate date default sysdate,
viewcnt number default 0
);
alter table tbl_board add constraint pk_board
primary key(bno);
create sequence seq_board;
insert into tbl_board (bno, title,content,writer)
values (seq_board.nextval,'제목','내용','user00');
728x90
'STUDY > SpringLegacy' 카테고리의 다른 글
[Spring] JOIN table 생성과 설명 이미지 (0) | 2022.09.21 |
---|---|
[JSP] 예외처리 연습 / JSP include 태그 (1) | 2022.09.19 |
[Spring] 글 목록·상세 출력 / 입력·등록·삭제·수정 만들기 (0) | 2022.09.17 |
[Spring] DTO,DAO,mapper생성과 DB연결 설정하기 (0) | 2022.09.17 |
[JSP] JSTL · EL 설명 예제 (0) | 2022.09.16 |