Spring練習(1)


掲示板リストの読み込み


Model


データベースにTABLEを作成する
// incWEB_db.sql
CREATE TABLE INTERN_BOARD_TBL(
SEQ		NUMBER			PRIMARY KEY,
TITLE	VARCHAR2(50)	NOT NULL,
CONTENT	VARCHAR2(4000),
WRITER	VARCHAR2(50)	REFERENCES INTERN_USER_TBL(ID),
REGDATE	DATE DEFAULT SYSDATE,
VIEWCNT	NUMBER DEFAULT 0
);

CREATE SEQUENCE BOARD_SEQ;

INSERT INTO EMPLOYEE (EMP_ID, EMP_NO, EMP_NAME)
VALUES (TO_CHAR(SEQ_ID.NEXTVAL), 123, 'KIM');

INSERT INTO INTERN_USER_TBL
VALUES(BOARD_SEQ.NEXTVAL, '공지', '내일 휴강입니다', 'jslim', DEFAULT, DEFAULT);

INSERT INTO INTERN_USER_TBL
VALUES(BOARD_SEQ.NEXTVAL, '종강안내', '뻥이야~~~~~', 'jslim', DEFAULT, DEFAULT);
Board Mapperの作成
// boardMapper.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.sinc.intern.board">
	<select id="list" resultType="board">
		SELECT SEQ, TITLE, CONTENT, WRITER, REGDATE, VIEWCNT
		FROM INTERN_BOARD_TBL
	</select>
</mapper>
VOの作成
MyBatisとマッピングしSpringにVOオブジェクトの生成を依頼したものをbeanと呼ぶ
->Springがデフォルトジェネレータを呼び出し、setメソッドを使用して値を割り当てるので、デフォルトジェネレータの作成を忘れないでください
// BoardVO.java
package com.sinc.intern.board.model.vo;

public class BoardVO {

	private int seq;
	private String title, content, writer, regdate;
	private int viewcnt;

	public BoardVO() {
		// TODO Auto-generated constructor stub
	}

	public BoardVO(int seq, String title, String content, String writer, String regdate, int viewcnt) {
		super();
		this.seq = seq;
		this.title = title;
		this.content = content;
		this.writer = writer;
		this.regdate = regdate;
		this.viewcnt = viewcnt;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	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 String getRegdate() {
		return regdate;
	}

	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}

	public int getViewcnt() {
		return viewcnt;
	}

	public void setViewcnt(int viewcnt) {
		this.viewcnt = viewcnt;
	}

	@Override
	public String toString() {
		return "BoardVO [seq=" + seq + ", title=" + title + ", content=" + content + ", writer=" + writer + ", regdate="
				+ regdate + ", viewcnt=" + viewcnt + "]";
	}

}
mybatisプロファイルにVOとaliasを登録する
// 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="com.sinc.intern.user.model.vo.UserDTO"
			alias="userdto" />
		<typeAlias type="com.sinc.intern.user.model.vo.UserVO"
			alias="uservo" />
		<typeAlias type="com.sinc.intern.board.model.vo.BoardVO"
			alias="board" />
	</typeAliases>

</configuration>
Daoの作成
// BoardDao.java
package com.sinc.intern.board.model.sql;

import java.util.List;

public interface BoardDao {

	public List<Object> listRow();
}

// BoardDaoImpl.java
package com.sinc.intern.board.model.sql;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

@Repository("boardD")
public class BoardDaoImpl implements BoardDao {

	public static final String BOARD_PREFIX = "com.sinc.intern.board";

	@Resource(name = "sqlSession")
	private SqlSession session;

	@Override
	public List<Object> listRow() {
		System.out.println("board dao listRow");
		List<Object> result = session.selectList(BOARD_PREFIX + ".list");
		System.out.println("dao listRow result: " + result);

		return result;
	}

}
サービスの作成
// BoardService.java
package com.sinc.intern.board.service;

import java.util.List;

public interface BoardService {

	public List<Object> list();
}

// BoardServiceImpl.java
package com.sinc.intern.board.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sinc.intern.board.model.sql.BoardDao;

@Service("boardS")
public class BoardServiceImpl implements BoardService {

	@Resource(name = "boardD")
	private BoardDao dao;

	@Override
	public List<Object> list() {
		System.out.println("board service list");
		return dao.listRow();
	}

}

View

// header.jsp
<li><a href="/listPage.do"><i class="fa fa-clipboard"aria-hidden="true"></i>게시판</a></li>
// listPage.jsp
<tbody id="tbody">
  <c:forEach items="${list}" var="boardVO">
    <tr>
      <td>${boardVO.seq}</td>
      <td><a href="readPage.sinc?seq=${boardVO.seq}">${boardVO.title}</a></td>
      <td>${boardVO.writer}</td>
      <td>${boardVO.regdate}</td>
      <td><span class="badge bg-red">${boardVO.viewcnt }</span></td>
    </tr>
  </c:forEach>
</tbody>

Controller


コントローラインプラントモデルから送信
// BoardCtrl.java
package com.sinc.intern.board.ctrl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sinc.intern.board.service.BoardService;

@Controller
public class BoardCtrl {

	@Resource(name = "boardS")
	private BoardService service;

	@RequestMapping(value = "/listPage.do")
	public String list(Model model) {
		System.out.println("board ctrl list");
		List<Object> list = service.list();
		if (list != null) {
			model.addAttribute("list", list);
		}
		return "board/listPage";
	}
}

投稿を掲示板に追加


Model

// boardMapper.xml
<insert id="register" parameterType="board">
  INSERT INTO INTERN_BOARD_TBL VALUES(BOARD_SEQ.NEXTVAL, #{title}, #{content},
  #{writer}, DEFAULT, DEFAULT)
</insert>
// BoardDao.java
public int registerRow(Object obj);

// BoardDaoImpl.java
@Override
public int registerRow(Object obj) {
	System.out.println("board dao registerRow");
	return session.insert(BOARD_PREFIX + ".register", obj);
}
// BoardService.java
public int register(Object obj);

// BoardServiceImpl.java
@Override
public int register(Object obj) {
	System.out.println("board service register");
	return dao.registerRow(obj);
}

View

// listPage.jsp
$("#newBtn").on("click", function() {
    location.href = "registerForm.do";
});
// register.jsp
<div class="box-body">
  <div class="form-group">
    <label for="exampleInputEmail1">Title</label>
    <input
      type="text"
      name="title"
      class="form-control"
      placeholder="Enter Title"
    />
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Content</label>
    <textarea
      class="form-control"
      name="content"
      rows="3"
      placeholder="Enter ..."
    ></textarea>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Writer</label>
    <input
      type="text"
      name="writer"
      class="form-control"
      readonly
      value="${loginUser.id}"
    />
  </div>
</div>

Controller

// BoardCtrl.java
@RequestMapping(value = "/registerFrom.do")
public String register() {
	System.out.println("board ctrl register");
    return "board/register";
}

詳細ページの表示


Model

// boardMapper.xml
<select id="read" parameterType="int" resultType="board">
  SELECT SEQ, TITLE, CONTENT, WRITER, TO_CHAR(REGDATE, 'YYYY-MM-DD') AS REGDATE,
  VIEWCNT FROM INTERN_BOARD_TBL WHERE SEQ = #{seq}
</select>
// BoardDao.java
public Object readRow(int seq);

// BoardDaoImpl.java
@Override
public Object readRow(int seq) {
	System.out.println("board dao readRow");
	Object result = session.selectOne(BOARD_PREFIX + ".read", seq);
	System.out.println("dao readRow result: " + result);

	return result;
}
// BoardService.java
public Object read(int seq);
    
// BoardServiceImpl.java
@Override
public Object read(int seq) {
	System.out.println("board service read");
	return dao.readRow(seq);
}

View

// listPage.jsp
<td><a href='readPage.do?seq=${boardVO.seq}'>${boardVO.title}</a></td>
// readPage.jsp
<div class="box-body">
  <div class="form-group">
    <label for="exampleInputEmail1">Title</label>
    <input
      type="text"
      name="title"
      class="form-control"
      value="${board.title}"
      readonly="readonly"
    />
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Content</label>
    <textarea class="form-control" name="content" rows="3" readonly="readonly">
${board.content}</textarea
    >
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Writer</label>
    <input
      type="text"
      name="writer"
      class="form-control"
      value="${board.writer}"
      readonly="readonly"
    />
  </div>
</div>

controller

// BoardCtrl.java
@RequestMapping(value = "/readPage.do", method = RequestMethod.GET)
public String read(@RequestParam("seq") int seq, Model model) {
	System.out.println("board ctrl read");
	Object obj = service.read(seq);
	if (obj != null) {
		model.addAttribute("board", obj);
	}
	return "board/readPage";
}
出典:SHINSEGAE I&C実習