소스코드
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/>
</parent>
<groupId>com.my</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8180
# 정적 리소스에 변경 시 바로 반영
spring.devtools.livereload.enabled=true
# thymeleaf 경로
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# templates 디렉토리에 파일이 있는지 없는지 체크
spring.thymeleaf.check-template-location=true
# Mysql 연동
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/SPRING_BOOT_DB?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=계정
spring.datasource.password=비밀번호
# 매핑할 모델의 패키지 경로
mybatis.type-aliases-package=com.my.hello.model
# mapper.xml 위치 명시
mybatis.mapper-locations=classpath:mapper/**/*.xml
Controller
package com.my.hello.controller;
@Controller
public class HomeController {
@Autowired
private SalaryService salaryService;
@RequestMapping(value = "/home", method=RequestMethod.GET)
public ModelAndView goHome(HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
List<SalaryModel> salaryList = salaryService.getSalary();
mav.addObject("salaryList", salaryList);
mav.setViewName("content/home.html");
return mav;
}
}
- ModelAndView : Model(DB 정보) + View(이동할 페이지 정보)
- application.properties에 지정한 /templates/content/home.jsp를 찾아 넘긴다.
Service
package com.my.hello.service;
@Service
public class SalaryService {
@Autowired
private SalaryMapper mapper;
public List<SalaryModel> getSalary() {
return mapper.getSalary();
}
}
mapper.java
package com.my.hello.mapper;
@Repository
@Mapper
public interface SalaryMapper {
List<SalaryModel> getSalary();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.my.hello.mapper.SalaryMapper">
<select id="getSalary" resultType="SalaryModel">
SELECT * FROM SALARY;
</select>
</mapper>
- id를 mapper.java와 맞춰줘야 한다.
Model(DTO)
package com.my.hello.model;
public class SalaryModel {
private int id;
private String name;
private String email;
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
}
view(html)
<div layout:fragment="content" class="content">
<h2>This is Content</h2>
<hr>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
</tr>
<th:block th:each="salary : ${salaryList}">
<tr>
<td th:text="${ salary.id }"></td>
<td th:text="${ salary.name }"></td>
<td th:text="${ salary.email }"></td>
</tr>
</th:block>
</table>
</div>