Spring Bootプロジェクトの作成と検索機能の実装(IntelliJ IDEA 2021使用)

1. Spring Bootプロジェクトの新規作成

IntelliJ IDEA 2021を使用してSpring Bootプロジェクトを新規作成します。プロジェクト作成後、必ず設定画面から自身がインストールしたMavenを指定してください。

2. データベース準備

MySQLデータベースを使用し、成語辞書用のテーブルを作成します。


CREATE TABLE idioms (
  ID DOUBLE DEFAULT NULL,
  name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  spell VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  content VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  derivation VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  samples VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  

初期データを挿入します:


INSERT INTO idioms VALUES (1, '陂湖禀量', 'bēi hú bǐng liáng', '比喻度量宽广恢弘。', '《后汉书·黄宪传》:"叔度汪汪若千顷陂,澄之不清,淆之不浊,不可量也。"', NULL);
INSERT INTO idioms VALUES (2, '北道主人', 'běi dào zhǔ rén', '北道上接待过客的主人。与"东道主人"同义。', '《后汉书·邓晨传》:"更始北都洛阳,以晨为常山太守。会王郎反,光武自蓟走信都,晨亦间行会于巨鹿下,自请从击邯郸。光武曰:‘伟卿(邓晨)以一从我,不如以一郡为我北道主人。’"', NULL);
  

3. 設定ファイルとコードの実装

3.1 application.ymlの設定


server:
  port: 8087

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.entity

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/idiom_dict?serverTimezone=GMT%2B8
    username: dict
    password: 123456
  

3.2 エンティティクラスの作成


package com.example.demo.entity;

import lombok.Data;

@Data
public class Idiom {
  private Double id;
  private String name;
  private String pronunciation;
  private String meaning;
  private String origin;
  private String example;
}
  

3.3 Mapperインターフェースの実装


package com.example.demo.mapper;

import com.example.demo.entity.Idiom;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface IdiomMapper {
  Idiom searchByIdiomName(@Param("keyword") String keyword);
}
  

3.4 Mapper XMLファイルの作成




<mapper namespace="com.example.demo.mapper.IdiomMapper">
  <select id="searchByIdiomName" parameterType="string" resultType="com.example.demo.entity.Idiom">
    SELECT * FROM idioms WHERE name LIKE CONCAT('%', #{keyword}, '%') GROUP BY name
  </select>
</mapper>
  

3.5 サービスクラスの実装


package com.example.demo.service;

import com.example.demo.entity.Idiom;
import com.example.demo.mapper.IdiomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IdiomService {
  @Autowired
  private IdiomMapper idiomMapper;

  public Idiom findIdiom(String keyword) {
    return idiomMapper.searchByIdiomName(keyword);
  }
}
  

3.6 コントローラクラスの実装


package com.example.demo.controller;

import com.example.demo.entity.Idiom;
import com.example.demo.service.IdiomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/idiom")
public class IdiomController {
  @Autowired
  private IdiomService idiomService;

  @GetMapping("/search")
  public Idiom search(@RequestParam String keyword) {
    return idiomService.findIdiom(keyword);
  }
}
  

タグ: Spring Boot MyBatis MySQL REST API IntelliJ IDEA

6月1日 18:46 投稿