0
2

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 5 years have passed since last update.

Visual Studio CodeによるSpring Boot2 Webアプリ開発 SQLServer接続編

Last updated at Posted at 2020-05-06

はじめに

こちらで作成したSQLServer接続WebアプリのSpring Boot版です。

環境

OS:Windows 10 Pro 64bit
DB:SQL Server 2019(Cent OS 8 on Hyper-V)
Editor:Visual Studio Code 1.44.2
JDK:AdoptOpenJDK 11.0.6+10 x64

ひな型作成

spring initializr(https://start.spring.io/)で、以下の設定で作成しました。

2020-05-05 (1).png

作成したひな型を「D:\JAVA\Project」に展開します。

pom.xml

SQLServerのJDBCがjre8版になっていますので、jre11版に変更します。
propertiesの中に「mssql-jdbc.version」を追加します。
(変更しなくても動作しましたが念のため)

<properties>
	<java.version>11</java.version>
	<mssql-jdbc.version>8.2.2.jre11</mssql-jdbc.version>
</properties>

application.properties

DB接続情報を記述します。

application.properties
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://xxx:1433;databaseName=Training01;QuotedID=NO
spring.datasource.username=xxx
spring.datasource.password=xxx

entity作成

DBからのデータを受けるentityを作成します。

D:\JAVA\Project\bootSample2\src\main\java\com\example\bootSample2\persistence
└─entity
  └─ProductsMaster.java
ProductsMaster.java
package com.example.bootSample2.persistence.entity;

import lombok.Data;

@Data
public class ProductsMaster {
    private String ProductsCode;
    private String ProductsName;
    private Integer UnitPrice;

    public ProductsMaster() {}

    public ProductsMaster(
        String ProductsCode,
        String ProductsName,
        Integer UnitPrice
        ) {
            this.ProductsCode = ProductsCode;
            this.ProductsName = ProductsName;
            this.UnitPrice = UnitPrice;
        }
}

repository作成

DBに接続してSQL文を発行するrepositoryを作成します。

D:\JAVA\Project\bootSample2\src\main\java\com\example\bootSample2\persistence
└─repository
  ├─ProductsMasterRepository.java
  └─ProductsMasterRepositoryImpl.java
ProductsMasterRepository.java
package com.example.bootSample2.persistence.repository;

import java.util.List;

import com.example.bootSample2.persistence.entity.ProductsMaster;

public interface ProductsMasterRepository {
    List<ProductsMaster> productsMasterList();
}
ProductsMasterRepositoryImpl.java
package com.example.bootSample2.persistence.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.bootSample2.persistence.entity.ProductsMaster;

@Repository
public class ProductsMasterRepositoryImpl implements ProductsMasterRepository {
	@Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

	@Override
	public List<ProductsMaster> productsMasterList() {
		String sql = "SELECT * FROM ProductsMaster ORDER BY ProductsCode;";

        List<ProductsMaster> pMList = jdbcTemplate.query(sql,
                (rs, rowNum) -> new ProductsMaster(
                        rs.getString("ProductsCode"),
                        rs.getString("ProductsName"),
                        rs.getInt("UnitPrice")
                        )
                );

        return pMList;
	}
}

service作成

ビジネスロジックとなるserviceを作成します。

D:\JAVA\Project\bootSample2\src\main\java\com\example\bootSample2
└─service
  ├─ProductsMasterService.java
  └─ProductsMasterServiceImpl.java
ProductsMasterService.java
package com.example.bootSample2.service;

import java.util.List;

import com.example.bootSample2.persistence.entity.ProductsMaster;

public interface ProductsMasterService {
    List<ProductsMaster> productsMasterList();
}
ProductsMasterServiceImpl.java
package com.example.bootSample2.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.bootSample2.persistence.entity.ProductsMaster;
import com.example.bootSample2.persistence.repository.ProductsMasterRepository;

@Service
public class ProductsMasterServiceImpl implements ProductsMasterService {
	@Autowired
	ProductsMasterRepository productsMasterRepository;

	@Override
	public List<ProductsMaster> productsMasterList() {
		List<ProductsMaster> pMList = productsMasterRepository.productsMasterList();

        return pMList;
	}
}

form作成

viewでの入出力値を受け取るformを作成します。

D:\JAVA\Project\bootSample2\src\main\java\com\example\bootSample2\web
└─form
  └─ProductsMasterForm.java
ProductsMasterForm.java
package com.example.bootSample2.web.form;

import java.util.List;

import com.example.bootSample2.persistence.entity.ProductsMaster;

import lombok.Data;

@Data
public class ProductsMasterForm {
	private List<ProductsMaster> pmList;
}

controller作成

D:\JAVA\Project\bootSample2\src\main\java\com\example\bootSample2\web
└─controller
  └─ProductsMasterController.java
ProductsMasterController.java
package com.example.bootSample2.web.controller;

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

import com.example.bootSample2.service.ProductsMasterService;
import com.example.bootSample2.web.form.ProductsMasterForm;

@Controller
@RequestMapping("/productsMaster")
public class ProductsMasterController {
	@Autowired
	ProductsMasterService productsMasterService;

	@GetMapping("/index")
	public String indexGet(Model model) {
		ProductsMasterForm productsMasterForm = new ProductsMasterForm();
		productsMasterForm.setPmList(productsMasterService.productsMasterList());

		model.addAttribute("productsMasterForm", productsMasterForm);
		return "productsMaster/index";
	}

}
RootController.java
package com.example.bootSample2.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class RootController {
    @GetMapping("/")
    public String root() {
    	return "redirect:productsMaster/index";
    }
}

view作成

「D:\JAVA\Project\bootSample2\src\main\resources\templates\」に「productsMaster」フォルダを作成します。
productsMasterフォルダにindex.htmlを作成します。

D:\JAVA\Project\bootSample2\src\main\resources\templates
└─productsMaster
  └─index.html
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Spring5 MVC sqlSample01</title>
</head>
<body>
    <h1>Hello Spring Products List</h1>
    <table border="1">
		<thead>
			<tr>
				<th>製品コード</th>
				<th>製品名</th>
				<th>単価</th>
			</tr>
		</thead>
		<tbody>
			<tr th:each="pm : ${productsMasterForm.pmList}" th:object="${pm}">
		    	<td th:text="*{ProductsCode}"></td>
		    	<td th:text="*{ProductsName}"></td>
		        <td th:text="*{UnitPrice}"></td>
			</tr>
		</tbody>
	</table>
</body>
</html>

動作確認

「F5」キーを押して実行します。
http://localhost:8080/
にアクセスして下さい。
自動でhttp://localhost:8080/productsMaster/indexにリダイレクトされることを確認して下さい。

以下のページが表示されればOKです。
springbootsql1.jpg

今回のサンプルソース

GitHubにアップしました。
https://github.com/t-skri1/SpringSample04

まとめ

非Bootとは階層構造とViewの配置場所が違うだけで、ソースの内容はそのままです。
それ以外の違いは

  • DBの接続情報は「application.properties」に記述する
  • pom.xmlでのバージョン指定方法が違う

になります。

DevToolを追加してありますので、hot deploy出来ます。
これは非常に便利です。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?