Il s'agit de la version Spring Boot de l'application Web de connexion SQL Server créée avec ici.
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
Je l'ai créé avec les paramètres suivants dans spring initializr (https://start.spring.io/).

Développez le modèle créé vers "D: \ JAVA \ Project".
pom.xml Le JDBC de SQL Server est la version jre8, alors changez-le en version jre11. Ajoutez "mssql-jdbc.version" dans les propriétés. (Cela a fonctionné sans aucun changement, mais juste au cas où)
<properties>
	<java.version>11</java.version>
	<mssql-jdbc.version>8.2.2.jre11</mssql-jdbc.version>
</properties>
application.properties Décrivez les informations de connexion à la base de données.
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
Créez une entité qui reçoit des données de la base de données.
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;
        }
}
Créez un référentiel qui se connecte à la base de données et émet des instructions SQL.
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;
	}
}
Créez un service qui sera la logique métier.
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;
	}
}
Créez un formulaire qui reçoit les valeurs d'entrée / sortie dans la vue.
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;
}
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";
    }
}
Créez un dossier "productsMaster" dans "D: \ JAVA \ Project \ bootSample2 \ src \ main \ resources \ templates ". Créez index.html dans le dossier productsMaster.
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>Code produit</th>
				<th>nom du produit</th>
				<th>Prix unitaire</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>
Appuyez sur la touche "F5" pour exécuter. http://localhost:8080/ Veuillez accéder. Assurez-vous que vous êtes automatiquement redirigé vers [http: // localhost: 8080 / productsMaster / index](http: // localhost: 8080 / productsMaster / index).
C'est OK si la page suivante est affichée.

Téléchargé sur GitHub. https://github.com/t-skri1/SpringSample04
Le contenu de la source reste le même, seule la structure hiérarchique et l'emplacement de la vue sont différents de ceux non-Boot. L'autre différence est
Sera.
DevTool a été ajouté, vous pouvez donc déployer à chaud. C'est très pratique.
Recommended Posts