참조자료
https://github.com/bitcocom/cloudai
이전 버전
변형 버전 (spring)
노랑색 객체는 자동으로 만들어줌
https://ittrue.tistory.com/237
// spring framework
implementation 'org.springframework:spring-webmvc:5.3.10'
// json 핸들링을 위해 추가함
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
https://docs.spring.io/spring-framework/reference/
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 아래 내용 root-context.xml 경로로 추가하기 -->
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 아래 내용 servlet-context.xml 경로로 추가하기 -->
<param-value>WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 아래 내용 root-context.xml 경로로 추가하기 -->
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 아래 내용 servlet-context.xml 경로로 추가하기 -->
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
https://docs.spring.io/spring-framework/reference/
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<!-- POJO -->
<context:component-scan base-package="org.example.controller"/>
<!-- ViewResolver -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Handler Mapping X-->
</beans:beans>
package org.example.controller;
import org.example.entity.Board;
import org.example.repersitoty.BoardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller // servlet-context에서 컨트롤러임을 식별. -> POJO로 인식함
public class BoardController {
// POJO -> new BoardController() -> Spring Controller -> DI(의존성 주입)
@Autowired // 밐 클래스 타입을 보고
private BoardMapper mapper;
// 리스트 가져오기 요청받기
@RequestMapping("/list") // Hendler Mapping
public String list(Model model){
List<Board> list = mapper.boardList();
// 객체바인딩
model.addAttribute("list", list);
// view의 논리적인 이름 리턴 -> 프론트컨트롤러에게 리턴
return "list";
}
// 등록하기 요청
}
package org.example.repersitoty;
import org.example.entity.Board;
import java.util.List;
public interface BoardMapper { // public class ? implements BoardMapper
// SqlSessionFactory
// 전체 게시물을 가져오는 기능 (TDD)
public List<Board> boardList();
}
// mybatis-spring
implementation 'org.mybatis:mybatis-spring:2.0.7'
implementation 'com.zaxxer:HikariCP:5.0.1'
implementation 'org.springframework:spring-jdbc:5.3.10'
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- CP : HikariCP -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ai"/>
<property name="username" value="root"/>
<property name="password" value="12345"/>
</bean>
<!-- Hikari 데이터 소스 -->
<bean id="hikariDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"/>
</bean>
<!-- mapper.xml 찾기 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="hikariDataSource"/>
<property name="mapperLocations" value="classpath:mybatis-config/mapper/**.xml"/>
</bean>
<mybatis-spring:scan base-package="org.example.repersitoty"/>
</beans>
컨트롤러와 레스트컨트롤러 차이
https://backendcode.tistory.com/213#google_vignette
BoardController 복사 > 붙여넣기 > BoardRestController으로 생성
package org.example.controller;
import org.example.entity.Board;
import org.example.repersitoty.BoardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController // @RestController (JSON 데이터 처리할때 사용)
public class BoardRestController {
// POJO -> new BoardController() -> Spring Controller -> DI(의존성 주입)
@Autowired // 연결된 메소드을 보고 연결
private BoardMapper mapper;
//http://localhost:8081/myweb/rspring
@RequestMapping("/rspring")
public String index(){
return "Hello Spring"; // WEB-INF/views/template.jsp
}
// 리스트 가져오기 요청받기
@RequestMapping("/rlist") // Hendler Mapping
public List<Board> list(Model model){
List<Board> list = mapper.boardList();
return list; // list -> JSON Array : [{ },{ }...];
}
// 등록하기 요청
}
다음 항목에서 선택가능
서버쪽에 아래 데이터를 날리겠다.
데이터 Send
404뜸. (jsp가 없기때문)
@PostMapping("rinsert")
public int register(@RequestBody Board board){ // 자동으로 파라메터가 수집
System.out.println(board);
return mapper.boardInsert(board);
}
<insert id="boardInsert" parameterType="org.example.entity.Board">
INSERT INTO BOARD(title, content, writer)
VALUES (#{title}, #{content}, #{writer})
</insert>