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 1 year has passed since last update.

SpringでMirage-SQLを使ってみた

Last updated at Posted at 2023-02-22

前提条件

WSLのUbuntuにMySQLを導入し、
ユーザー作成、DB作成、テーブル作成、データ挿入まで完了していること

今回作成したデータベースの詳細は以下となります。

項目
ユーザ名 user
パスワード pass
データベース名 prefecture
テーブル名 prefecture_list
カラム名1 prefecture_name(県名)
カラム名2 area(地方区分)
カラム名3 population(人口)

プロジェクト作成

Eclipseを使います。
「新規」 > 「Springスタータープロジェクト」を選択

項目 選択値
タイプ Gradle - Groovy
パッケージング War
Javaバージョン 17
言語 Java

選択する依存関係

  • Lombok
  • Spring Boot Dev Tool(任意)
  • Spring Web

build.gradle

build.gradleのdependenciesに

  • implementation group: 'com.mysql', name: 'mysql-connector-j', version: '8.0.32'
  • implementation group: 'jp.sf.amateras', name: 'mirage', version: '1.2.5'
    を追加して、以下のようにする
dependencies {
	implementation group: 'com.mysql', name: 'mysql-connector-j', version: '8.0.32'
	implementation group: 'jp.sf.amateras', name: 'mirage', version: '1.2.5'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

追加したら、念のために以下を実行

「プロジェクトを右クリック」 > 「Gradle」 > 「Gradleプロジェクトのリフレッシュ」

DB接続とSQLの準備

src/main/resourcesの中に

  • jdbc.properties
  • prefectureListAll.sql
    の二つを作成
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/prefecture
jdbc.user=user
jdbc.password=pass
prefectureListAll.sql
SELECT * FROM prefecture_list;

実装

エンティティ、リポジトリー、コントローラーを作成します。

Model.java
import jp.sf.amateras.mirage.annotation.Column;
import jp.sf.amateras.mirage.annotation.Table;
import lombok.Data;

@Data
@Table(name="prefecture_list")
public class Model {
	@Column(name="prefecture_name")
	String prefectureName;
	@Column(name="area")
	int area;
	@Column(name="population")
	int population;
}
SampleController.java
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path="sample")
public class SampleController {

	@Autowired
	Repository repository;
	
	@GetMapping(path="all")
	public @ResponseBody List<Model> getAll(){
		List<Model> list = repository.selectAll();
		return list;
	}
}
SampleRepository.java
import java.util.List;

public interface Repository {
public List<Model> selectAll();
}
SampleRepositoryImpl
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import jp.sf.amateras.mirage.ClasspathSqlResource;
import jp.sf.amateras.mirage.SqlManager;
import jp.sf.amateras.mirage.SqlResource;
import jp.sf.amateras.mirage.session.Session;
import jp.sf.amateras.mirage.session.SessionFactory;

@Repository
public class SampleRepositoryImpl implements SampleRepository {

	@Override
	public List<Model> selectAll() {
		Map<String, Object> params = new HashMap<>();
		params.put("Status", 0);
		Session session = SessionFactory.getSession();
		session.begin();
		SqlManager sqlManager = session.getSqlManager();
		try {
			SqlResource sql = new ClasspathSqlResource("prefectureListAll.sql");
			List<Model> list = sqlManager.getResultList(Model.class, sql, params);
			session.commit();
			session.release();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
			session.release();
			return null;
		}
	}
}

動作確認

「プロジェクト右クリック」 > 「実行」 > 「4 Spring Boot App」

"http://localhost:8080/sample/all"にアクセス
image.png

上記画像のようにブラウザ上に表示されていれば、DBからフィールドを取得できています。

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?