Continuation of the last time. https://qiita.com/YJ2222/items/8c29fc7fc2d886a9b35e
-Create the necessary files. Create by referring to the image below and "Explanation of each file".
templates/userResult.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>User</title>
</head>
<body>
<h1>UserResult</h1>
<table>
<tr>
<!-- th:text can be getAttribute using the function of thymeleaf.-->>
<td>ID:</td><td th:text="${id}"></td>
</tr>
<tr>
<td>nickname:</td><td th:text="${nickname}"></td>
</tr>
</table>
<body>
</html>
controller/UserController.java
package com.ex1.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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ex1.model.User;
import com.ex1.model.UserFindLogic;
@Controller
public class UserController {
@Autowired //Dependency injection.
private UserFindLogic userFindLogic;
@GetMapping("/")
public String getUser() {
return "user";
}
// user.Processing of post from html.@Get the value of name with RequestParam.
@PostMapping("/user/db")
public String postDbRequest(@RequestParam("text") String str, Model model) {
int id = Integer.parseInt(str); //String conversion.
//Without new UserFindLogic@Since the dependency is injected by Autowired, UserFindLogic can be executed as User type.
User user = userFindLogic.findUser(id); // UserFindLogic.Execute the findUser method of java.
// UserFindLogic.After receiving the return value from java, execute the following.
model.addAttribute("id", user.getUserId()); //SetAttribute user information.
model.addAttribute("nickname", user.getNickName());
return "userResult"; // userResult.Forward to html.
}
}
model/UserFindLogic.java
package com.ex1.model;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ex1.dao.UserDAO;
@Service //Annotation given to business logic.
public class UserFindLogic {
@Autowired
private UserDAO userDAO;
public User findUser(int id) { //Execute the method according to the instruction from the controller.
Map<String, Object> map = userDAO.findUser(id); //Execute findUser of UserDAO.
//Receive a user variable from DAO.
int userId = (Integer) map.get("user_id"); //Decompose the received variable with map.Get information with get.
String nickName = (String) map.get("nickname");
User user = new User(); //Create user instance (User.java)
user.setUserId(userId); //Run setter.
user.setNickName(nickName);
return user; //Return the user instance to controller as a return value.
}
}
model/User.java
package com.ex1.model;
public class User {
private int userId;
private String nickName;
public void setUserId(int userId) {
this.userId = userId;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getUserId() {
return userId;
}
public String getNickName() {
return nickName;
}
}
dao/UserDAO.java
package com.ex1.dao;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository //Annotation given to DAO class.
public class UserDAO {
@Autowired
private JdbcTemplate jdbcTemplate; //Instance that can connect to DB.
public Map<String, Object> findUser(int id) { // UserFindLogic.Execute the method according to the instruction of java.
//Generate SELECT statement
String query = "SELECT "
+ " * "
+ "FROM account "
+ "WHERE user_id=?";
//Execute SQL for DB by the function of jdbcTemplate.
Map<String, Object> user = jdbcTemplate.queryForMap(query, id);
return user; //UserFindLogic for the above processing result.Return to java as a return value.
}
}
templates/userResult.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>User</title>
</head>
<body>
<h1>UserResult</h1>
<table>
<tr>
<td>ID:</td><td th:text="${id}"></td>
</tr>
<tr>
<td>nickname:</td><td th:text="${nickname}"></td>
</tr>
</table>
<body>
</html>
src/main/resources/application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasouce.username=sa
spring.datasouce.password=
spring.datasource.sql-script-encoding=UTF-8
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
src/main/resources/data.sql
INSERT INTO account (user_id, nickname, password) VALUES (1, 'user1', 'user1+');
src/main/resources/schema.sql
CREATE TABLE IF NOT EXISTS account (
user_id INT PRIMARY KEY,
nickname VARCHAR(50),
password VARCHAR(50)
);
-The process flow is easy to imagine by referring to the comment section of the code and the image below.
that's all. Now you can even connect to the DB in your local environment.
Recommended Posts