Skip to content

Commit

Permalink
MvcWeb-day01
Browse files Browse the repository at this point in the history
  • Loading branch information
respect98 committed Dec 16, 2022
1 parent 8ace355 commit e509f6b
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 0 deletions.
116 changes: 116 additions & 0 deletions MvcWeb/BoardDAOMyBatis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package board.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class BoardDAOMyBatis {

private final String NS="board.model.BoardMapper";
private String resource="common/config/mybatis-config.xml";//설계도
private SqlSession ses;

public SqlSessionFactory getSessionFactory() {
try {
InputStream is=Resources.getResourceAsStream(resource);
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
return factory;
}catch(IOException e) {
e.printStackTrace();
return null;
}
}//-------------------------------------

public int getTotalCount(String type, String keyword) {

Map<String, String> map=new HashMap<>();
map.put("findType", type);
map.put("findKeyword", keyword);

ses=this.getSessionFactory().openSession();
int count=ses.selectOne(NS+".totalCount" , map);
if(ses!=null) ses.close();
return count;
}//-------------------------------------

public int insertBoard(BoardVO vo) {
ses=this.getSessionFactory().openSession();//디폴트가 수동 커밋이다.
System.out.println("----insert전: vo의 num=>"+vo.getNum());

int n=ses.insert(NS+".insertBoard", vo);

System.out.println("----insert후: vo의 num=>"+vo.getNum());

if(n>0) {
ses.commit();
}else {
ses.rollback();
}
if(ses!=null) ses.close();
return n;
}//-------------------------------------

public List<BoardVO> listBoard(int start, int end, String type, String keyword) {
ses=this.getSessionFactory().openSession();
//다중행을 가져올 때는 selectList()
//단일행을 가져올 때는 selectOne()
Map<String,String> map=new HashMap<>();
map.put("start", String.valueOf(start));
map.put("end", end+"");
map.put("findType", type);
map.put("findKeyword", keyword);

List<BoardVO> arr=ses.selectList(NS+".listBoard", map);
if(ses!=null) ses.close();
return arr;
}//-------------------------------------

public BoardVO viewBoard(int num) {
try {
ses=this.getSessionFactory().openSession();
BoardVO vo=ses.selectOne(NS+".viewBoard", num);
return vo;
}finally {
close();
}
}//-------------------------------------

public void close() {
if(ses!=null) ses.close();
}

public int deleteBoard(int num) {
try {
ses=this.getSessionFactory().openSession(true);
//디폴트가 수동 커밋, 매개변수로 true를 넘기면 auto commit된다.
int n=ses.delete(NS+".deleteBoard", num);
return n;
} finally {
close();
}
}//-------------------------------------

public int updateBoard(BoardVO vo) {
try {
ses=this.getSessionFactory().openSession(true);
int n=ses.update(NS+".updateBoard", vo);
return n;
}finally {
close();
}
}//-------------------------------------

}/////////////////////////////////////////






78 changes: 78 additions & 0 deletions MvcWeb/BoardMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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="board.model.BoardMapper">
<sql id="findWhere">
<if test="findType!='' and findKeyword!=''">
<where>
<if test="findType==1"><!-- 글제목 -->
subject like '%'||#{findKeyword}||'%'
</if>
<if test="findType==2"><!-- 작성자 -->
userid like '%'||#{findKeyword}||'%'
</if>
<if test="findType==3"><!-- 글내용 -->
content like '%'||#{findKeyword}||'%'
</if>
</where>
</if>
</sql>

<select id="totalCount" resultType="_int" parameterType="map" >
<!-- resultType : _int (기본자료형 int형), int (래퍼클래스 java.lang.Integer) -->
select count(num) cnt from board
<include refid="findWhere"/>
</select>
<!-- selectKey를 사용하면 시퀀스 증가한 값을 파라미터로 넘어온 vo에 객체에 setting해주는 기능을 갖는다 -->
<insert id="insertBoard" parameterType="Board">
<selectKey keyProperty="num" resultType="int" order="BEFORE">
select board_seq.nextval from dual
</selectKey>
insert into board(num, subject, content, userid, wdate, filename, filesize)
values(#{num},#{subject},#{content:VARCHAR},#{userid}, systimestamp,#{filename:VARCHAR},#{filesize:NUMERIC})
</insert>
<insert id="insertBoard_old" parameterType="board.model.BoardVO">
insert into board(num, subject, content,userid,wdate, filename, filesize)
values(board_seq.nextval, #{subject}, #{content:VARCHAR}, #{userid}, systimestamp,#{filename:VARCHAR},#{filesize:NUMERIC})
<!-- BoardVO객체의 getSubject()를 호출해서 #{subject} 값으로 셋팅한다
ps.setString(1, vo.getSubject())
-->
</insert>
<!-- #{num} #{value} -->

<select id="listBoard" resultType="Board" parameterType="map">
select * from(
select a.*, rownum rn from
(select * from board
<include refid="findWhere"/>
order by num desc) a
)
where rn between #{start} and #{end}
</select>

<select id="listBoard_old" resultType="board.model.BoardVO">
select * from board order by num desc
</select>

<select id="viewBoard" resultType="board.model.BoardVO" parameterType="_int">
select * from board where num=#{value}
</select>
<!-- [글삭제] -->
<delete id="deleteBoard" parameterType="_int">
delete from board where num=#{value}
</delete>
<!-- [글수정] -->
<update id="updateBoard" parameterType="Board">
update board set subject=#{subject}, content=#{content:VARCHAR},
wdate=systimestamp
<if test="filename!=null and filename!=''">
, filename=#{filename}, filesize=#{filesize}
</if>
where num=#{num}
</update>
</mapper>



87 changes: 87 additions & 0 deletions MvcWeb/BoardVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package board.model;

import java.sql.Timestamp;

public class BoardVO {

private int num;
private String userid;
private String subject;
private String content;
private java.sql.Timestamp wdate;
private String filename;
private long filesize;

public BoardVO() {

}

public BoardVO(int num, String userid, String subject, String content, Timestamp wdate, String filename,
long filesize) {
super();
this.num = num;
this.userid = userid;
this.subject = subject;
this.content = content;
this.wdate = wdate;
this.filename = filename;
this.filesize = filesize;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public String getUserid() {
return userid;
}

public void setUserid(String userid) {
this.userid = userid;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getContent() {
return content;
}

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

public java.sql.Timestamp getWdate() {
return wdate;
}

public void setWdate(java.sql.Timestamp wdate) {
this.wdate = wdate;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public long getFilesize() {
return filesize;
}

public void setFilesize(long filesize) {
this.filesize = filesize;
}

}///////////////////////////////////////
59 changes: 59 additions & 0 deletions MvcWeb/foot.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- foot.jsp -->
<%
String myctx=request.getContextPath();
%>
<style>
.mydiv{
width:90%;
margin:auto;
padding:1em;
height:130px;
background-color:#efefef;
display:table;
}
.mycell{
text-align:center;
width:100%;
height:100%;
display: table-cell;
vertical-align: middle;
}
</style>
</article>
<!-- 사이드 영역 -------------------------------->
<aside>
<!-- 사이드
request에 저장한 값=> requestScope.key
session에 저장한 값=> sessionScope.key
${key}
-->
<nav>
<c:if test="${sessionScope.loginUser!=null}">
<div class="mydiv">
<div class="mycell">
<h3>${loginUser.name}[${loginUser.userid}]</h3>
<h3>로그인 중...</h3>
<br>
<h4><a href="${pageContext.request.contextPath}/logout.do">로그아웃</a></h4>
</div>
</div>
</c:if>

</nav>

</aside>
<div class="clear"></div>
<!-- 푸터 영역 ------------------------------>
<footer>
<!-- footer ----------------------->
&copy;Copyright/회사소개
</footer>

</div>
<!-- div#wrap. container end -->
</body>
</html>
17 changes: 17 additions & 0 deletions MvcWeb/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:include page="/top.jsp"/>
<div class="container">
<h1>Index</h1>
<p>
<%=request.getAttribute("msg") %> <br>

<div style='color:tomato;font-size:2em'>
el 표현식을 이용해서 출력할 수 있다.<br>
${msg}
</div>

</p>

</div>
<jsp:include page="/foot.jsp"/>
6 changes: 6 additions & 0 deletions MvcWeb/message.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<script>
alert('${msg}');
location.href='${loc}';
</script>

0 comments on commit e509f6b

Please sign in to comment.