0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring 開発

概要

Java開発でも人気のある「Spring Framework」ざっくりだが、
その開発での処理を記載していく。

・自分たちで開発したものをベースに記載をしていく
・SpringBootも導入しているので、bean、Daoの記載は省略

目次

  1. Spring開発の概要(あくまで個人開発ベース)
  2. プロジェクトにおけるアノテーションの役割

1.Spring開発の概要(あくまで個人開発ベース)

MVCモデルで実装

  • Controller
     →ここでModel,Viewに指示
  • Model
     →データのやり取り
      →DBなどから
  • View
     →ユーザーインターフェース、いわゆるHTML部分

処理の流れのイメージとしては、
Controllerで
表示したいパス(HTMLファイル)を指定し、画面の表示をViewに命令
画面表示に必要なデータを取得(更新や、削除含め)するようにServiceへ命令

上記を繰り返すことで、Webアプリとして完成する

2.プロジェクトにおけるアノテーションの役割

@Contoroller

  • コントローラーとなる部分
  • GetMapping()で指定されたパスに遷移する際に、処理が走る
  • returnで表示するHTMLファイル名を指定する
    →例だとindex.htmlを表示する
    (例)
@Controller
public class TestController extends Common {

    @GetMapping("/index")
    public String indexDisplay() {
    	
    	/* *****************************
    	  HTMLに渡すデータなどを生成、
    	  Serviceに渡したりなどの処理を記載
    	***************************** */
    	return "index"
    

@Service

  • Modelとなる部分
  • データのやり取りを行う
    →DB操作や、純粋にリスト操作などデータ操作を行う
    (例)
     CHATHISTORY型のArrayList作成
     Mapper(xml)ファイルのselectHistoryメソッドを実行し
     DBからメッセージを取得
@Service
public class MessageService {
    /* メッセージの履歴取得 */
    public List<CHATHISTORY> getMessageLog(int roomId) {
        /* メッセージ履歴格納用 */
        List<CHATHISTORY> messageLogList = new ArrayList<>();
        /* Mapper(xml)にてSQL実行し、メッセージ履歴を取得 */
        messageLogList = messageMapper.selectHistory(roomId);
		/* メッセージが格納されたリストを返す */
        return messageLogList;
    }

@Mapper

  • Daoとなる部分
  • DBアクセスする
  • XMLファイル(MyBatis)を実行
@Mapper
public interface messageMapper {
    /* 
    * メッセージ履歴の取得用のSQLを実行
    * SQLはXMLファイルに記載
    */
    public List<CHATHISTORY> selectHistory(int roomId);

まとめ

 Springの開発のイメージとしては、上記のような構成と処理の内容である。
これによって、画面遷移ごとに処理が走り、期待するデータを取得、画面へ受け渡しを行い、Webアプリとして機能する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?