SSMフレームワーク統合 - 機能モジュールの開発

プロジェクトには以下のモジュールが含まれており、configはSpringの設定などを行うモジュールです。controllerはSpringMVCを使用してフロントエンドからのリクエストを処理します。daoはデータベースとのやりとりを行い、データ操作を行います。domainはエンティティクラスを格納するためのモジュールです。serviceはビジネスロジックを実装するモジュールです。

まず、データベースのテーブルに基づいてエンティティクラスのプロパティとgetter/setterメソッドを定義します。

package com.mingyu.domain;

public class Account {
    private String name;
    private int balance;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", balance=" + balance +
                '}';
    }
}

daoモジュールではデータベース操作を行うためのインターフェースを定義します。

package com.mingyu.dao;

import com.mingyu.domain.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AccountDao {
    @Select("select * from account")
    List<Account> findAll();

    @Select("select * from account where name=#{name};")
    Account findByName(String name);

    @Insert("insert into account values(#{name},#{balance})")
    void save(Account account);

    @Delete("delete from account where name=#{name}")
    void remove(String name);

    @Update("update account set name=#{name},balance=#{balance} where name=#{oldName}")
    void modify(String oldName, Account account);
}

サービス層にはインターフェースと実装クラスがあり、サービス層はdao層のメソッドを呼び出してデータベース操作を実現します。

package com.mingyu.service.impl;

import com.mingyu.dao.AccountDao;
import com.mingyu.domain.Account;
import com.mingyu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    
    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account findByName(String name) {
        return accountDao.findByName(name);
    }

    @Override
    public boolean save(Account account) {
        accountDao.save(account);
        return true;
    }

    @Override
    public boolean remove(String name) {
        accountDao.remove(name);
        return true;
    }

    @Override
    public boolean modify(String oldName, Account account) {
        accountDao.modify(oldName, account);
        return true;
    }
}

controller層はSpringMVCを使用して開発されます。

package com.mingyu.controller;

import com.mingyu.domain.Account;
import com.mingyu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    
    public List<Account> findAll() {
        return accountService.findAll();
    }

    public Account findByName(String name) {
        return accountService.findByName(name);
    }

    public boolean save(Account account) {
        accountService.save(account);
        return true;
    }

    public boolean remove(String name) {
        accountService.remove(name);
        return true;
    }

    public boolean modify(String oldName, Account account) {
        accountService.modify(oldName, account);
        return true;
    }
}

モジュール開発が完了したら、JUnitを使用してサービス層のテストを行い、Postmanを使ってコントローラー層のテストを行います。

さらにトランザクション管理も行う必要があります。

タグ: SSM Spring MyBatis SpringMVC Java

5月17日 02:14 投稿